I am trying my way through JavaFX and still have many - probably silly - beginner questions.
My problem of the day is the following:
I am creating, in Scene builder and Controller, a FlowPane to which I want to add a right-click option, that opens a Context Menu.
Through the scene builder I have added the function OnContextMenuRequested and defined it in the Controller.
To check, I have added a print commend and a Dialog Box to the function, which work well.
Yet, the Context Menu does not work..
Anybody could help and tell me what am I missing???
Thanks in advance...
public void contextMenu(ContextMenuEvent contextMenuEvent) {
// working fine ..
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information");
alert.setHeaderText("Look");
alert.setContentText("Message");
alert.showAndWait();
// working fine
System.out.println("Hello");
// Context Menu ......... not working
ContextMenu contextMenu = new ContextMenu();
MenuItem quit = new MenuItem("quit");
MenuItem hello = new MenuItem("hello");
contextMenu.getItems().addAll(quit, hello);
contextMenu.setX(10.0);
contextMenu.setY(10.0);
contextMenu.show();
????.setContextMenu(????)
}
Unless you have a control, you need to show the ContextMenu "manually" using one of the methods defined in ContextMenu:
// contextMenu.setX(10.0);
// contextMenu.setY(10.0);
contextMenu.show((Node) contextMenuEvent.getSource(), contextMenuEvent.getScreenX(), contextMenuEvent.getScreenY());
Related
Can somebody explain how to use Dialogue box from above library through XML. Without coding the dialog box using java, Can't I just design it from scene builder by dragging and dropping component.
I have already tried it but when I run the program dialog box is not visible.
Would be really helpful if someone can share a working example or even a link
without using this method.
How to create a dialog using JFXDialog of JFoenix in JavaFX
Double height = posCenterAnchor.getHeight();
Double width = posCenterAnchor.getWidth();
StackPane stackPane = new StackPane();
AnchorPane.setTopAnchor(stackPane, 20.0); // adding anchor pane margins
AnchorPane.setLeftAnchor(stackPane, 20.0);
AnchorPane.setRightAnchor(stackPane, 20.0);
AnchorPane.setBottomAnchor(stackPane, 20.0);
posCenterAnchor.getChildren().add(stackPane);
JFXDialogLayout jfxDialogLayout = new JFXDialogLayout();
Parent parent;
try {
parent = FXMLLoader.load(getClass().getResource("/Views/SelectCustomer.fxml"));
jfxDialogLayout.getChildren().add(parent);
JFXDialog jfxDialog = new JFXDialog(stackPane, jfxDialogLayout, JFXDialog.DialogTransition.CENTER, true);
jfxDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
just figured it out..all i had to do is ,just crate the content of the dialog box in a separate layout file and through code by creating a new dialog object then adding that layout to a dialog box...simple...wonder where went wrong..
Are AX dialog buttons limited to OK and Cancel?
Is it possible to add a custom button to the dialog?
I have the following code for my dialog:
static void mitTabPage(Args _args)
{
Dialog dialog;
DialogGroup dialoggroup, dialoggroup2;
DialogField dialogfield, dialogfield2;
;
dialog = new Dialog ("A new Dialog");
dialog.addTabPage("Brand Id's");
dialoggroup = dialog.addGroup("Brand Id's");
dialogfield = dialog.addField(extendedTypeStr(SYCCarBrandId));
dialog.addTabPage("Owners");
dialoggroup2 = dialog.addGroup("Owners");
dialogfield2 = dialog.addField(extendedTypeStr(SYCOwner));
dialog.run();
}
I'd like to add another button to the dialog. How can I do that?
The Dialog framework is a simple framework for prompting users to obtain some data/settings then performing some action or canceling.
For what you're trying to do, it most likely doesn't make sense to use the dialog framework and instead you could/should create another form if you need additional functionality.
However, if you do insist on using the Dialog framework for this, you would add a runtime button and use registerOverrideMethod.
See following links:
https://msdn.microsoft.com/en-us/library/dialogfield.registeroverridemethod.aspx
https://blogs.msdn.microsoft.com/axsupport/2015/06/07/using-x-to-add-a-control-at-runtime/
I'm creating my UI from Qt Designer and it generares this code:
toolBar = new QToolBar(MainWindow);
QIcon icon;
icon.addFile(QStringLiteral(":/main"), QSize(), QIcon::Normal, QIcon::Off);
MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);
actionConvert = new QAction(MainWindow);
actionConvert->setObjectName(QStringLiteral("actionConvert"));
actionConvert->setIcon(icon);
toolBar->addAction(actionConvert);
Now, back in my frame code:
QMenu *menuAdd = new QMenu (this);
menuAdd->addAction (tr("&Files..."));
menuAdd->addAction (tr("&Directory..."));
ui->actionConvert->setMenu (menuAdd);
When I run the application I can see the qaction in the toolbar even the arrow pointing down, which indicates that there is a menu, but when I click it, the menu doesn't appear...any ideas?
There does not seem to be anything wrong with your example code.
It's possible that the reason you aren't seeing the menu is that you need to press and hold the button for a few seconds in order for the menu to appear. A single click will just execute the button's normal action.
See: QToolButton::ToolButtonPopupMode.
You should add menu with menuBar() method as in my case:
void MainWindow::ueInitMenu()
{
this->ueSetCodeRegisterPlacesAction(new QAction(tr("Places"),
this));
this->ueCodeRegisterPlacesAction()->setShortcut(tr("Ctrl+P"));
this->ueCodeRegisterPlacesAction()->setStatusTip(tr("Shows places code register"));
connect(this->ueCodeRegisterPlacesAction(),
SIGNAL(triggered()),
this,
SLOT(ueSlotShowPlacesView()));
this->ueSetCodeRegisterMenu(this->menuBar()->addMenu(tr("Code register")));
this->ueCodeRegisterMenu()->addAction(this->ueCodeRegisterPlacesAction());
} // ueInitMenu
especialy the line:
this->ueSetCodeRegisterMenu(this->menuBar()->addMenu(tr("Code register")));
so in your case:
this->menuBar()->addMenu(tr("System menu");
and then add actions. Also take a look at Menus Example.
I am creating a JavaFX Dialog and want to use the default icons for Info/Warning/Error.
In Swing, I can get the Information icon this way:
UIManager.getIcon("OptionPane.informationIcon")
How can I do the same in JavaFX?
I asked for this some days ago. I use this code to make labels with default icons:
Label img = new Label();
img.getStyleClass().addAll("alert", "error", "dialog-pane");
dialog.setGraphic(img);
If you get a copy of the images you can use these ideas.
Alert example:
#FXML void handleHelpButton(ActionEvent event){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Help");
alert.setHeaderText("Help");
alert.setGraphic(new ImageView(this.getClass().getResource("img/help.png").toString()));
alert.setContentText("Place the cursor over a button for hint.");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(new Image(this.getClass().getResource("img/help.png").toString()));
alert.showAndWait();
}
Dialog example:
ChoiceDialog<String> dialog = new ChoiceDialog<>(currentFullscreenSetting, choices);
dialog.setTitle("Settings");
dialog.setHeaderText("Settings");
dialog.setContentText("Fullscreen on startup: ");
dialog.setGraphic(new ImageView(this.getClass().getResource("img/settings.png").toString()));
Stage stage2 = (Stage) dialog.getDialogPane().getScene().getWindow();
stage2.getIcons().add(new Image(this.getClass().getResource("img/settings.png").toString()));
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
This part of both examples is tricky. I noticed that this would not work unless I had the image in the same folder as the .fxml and controller.java files or in a folder that is in the same folder has the files mentioned. You might have to play with you file location. In my example it appears that my setGraphic and getIcons images are in the same folder, but they are not.
stage.getIcons().add(new
Image(this.getClass().getResource("img/help.png").toString()));
My file structure looks like:
PlanningChart
css
img
planningchart
img
The second img folder holds the images for stage.getIcons.add(). The images could also be probably use to for setGraphic. I did not try it.
You do not need to create custom JavaFX dialogs for Info/Warning/Error, since JavaFX already have created Alerts for you.
The Alert class subclasses the Dialog class, and provides support for a number of pre-built dialog types that can be easily shown to users to prompt for a response.
You can create different types of Alerts, depending on the AlertType, it will embed the necessary image.
For Information alert use :
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("An Information Dialog");
alert.setContentText("Information Message");
alert.showAndWait();
Similarly, for Warning alert, you can use
AlertType.WARNING
and for Error alert, you can use :
AlertType.ERROR
I have two layouts in JavaFX app. The first one contains table view whereas the second one is a simple dialog to input data. The problem is that I want to refresh data after closing the dialog. Now I have a button on the first layout which refreshes data:
data.removeAll(data);
loadDataToTable();
But I don't want to invoke methods shown above with the button but automatically right after closing the dialog. I don't know how to make this, let's say, connection between those controllers.
thanks in advance
The new Dialog, if I am not wrong must be a new Stage ! Let us consider the new Stage to be modifyStage. We can call the onSetCloseRequest of the new Stage and put your code in it.
modifyStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent paramT) {
data.removeAll(data);
loadDataToTable();
}
});