Is there any Mouse click event for TableColumn in JavaFX - javafx

Is there any Mouse click event for TableColumn in Java FX ?
I can see it in tableView but not in TableColumn. Please suggest.

There's no particularly nice way to do this. Probably the best option is not to set text on the column, but to set the graphic to a Label containing the desired text instead. Then register the mouse listener with the Label.
Using the standard Oracle tutorial as an example, you could do:
TableColumn<Person, String> firstNameColumn = new TableColumn<>();
Label firstNameColHeader = new Label("First Name");
firstNameColHeader.setOnMouseClicked(e -> System.out.println("Click on header"));
firstNameColumn.setGraphic(firstNameColHeader);
Note that this will break the table menu button, if you use it, because the options in the menu use the text from the TableColumn, which is no longer present.

Related

How to play a video in a alert using JavaFX?

I'm trying to play a video in a alert dialog using JavaFX. The problem is that I can't find how to display the video or more how to insert it in the alert ?
Here is my alert code
MediaPlayer player = new MediaPlayer( new Media(getClass().getResource("video.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
private void alert(){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
alert.setContentText("Do you really think your time is correct ?");
Optional<ButtonType> result = alert.showAndWait();
}
An Alert extends from Dialog, which means you can customize its DialogPane. If you want to add a video to your alert, the best place is probably the dialog pane's content. But note that setting the content will replace the contentText (which you set in your example code):
In addition to the header and content properties, there exists header text and content text properties. The way the *Text properties work is that they are a lower precedence compared to the Node properties, but they are far more convenient for developers in the common case, as it is likely the case that a developer more often than not simply wants to set a string value into the header or content areas of the DialogPane.
This means, if you still want to display "Do you really think your time is correct?", you'll have to add your own Label to the content as well. For example:
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Don't be a fool");
alert.setHeaderText("");
Label label = new Label("Do you really think your time is correct?");
VBox content = new VBox(10, label, mediaView);
content.setAlignment(Pos.CENTER);
alert.getDialogPane().setContent(content);
alert.setOnShowing(e -> player.play());
alert.showAndWait();

JavaFX: Why that does not connect between label and textfield with setLabelFor?

I have the following code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setLabelFor(vSuchenTextfield);
when I click on the vLblSuchfeld label with the mouse,
the text field vSuchenTextfield is not activated. What am I doing wrong?
If you want to bring focus to the textfield when you click on the label, I suggest you implement the following (untested) code:
TextField vSuchenTextfield = new TextField("");
vLblSuchfeld.setMnemonicParsing(true);
vSuchenTextfield.setPromptText("Suchbegriff eingeben");
vLblSuchfeld.setOnMouseClicked(event -> {
vSuchenTextfield.requestFocus();
});

JavaFx add context menu from controller

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());

javafx TitledPane click event only for Title

Is there any way I can have an event that only triggers if I click the Title of a TitledPane?
I have several Nodes in a Graph Editor and currently they are draggable.
But I want them only to drag when i drag the Title not if I click anywhere on the pane.
the mouseClick event seems not to work for me.
Does anyone have suggestions?
Don't set the text on the titled pane, but instead create a label and set it as the graphic for the titled pane. Then you can register a mouse handler with the label:
private TitledPane createClickableTitledPane(String text) {
Label label = new Label(text);
label.setOnMouseClicked(e -> System.out.println("Click on "+text));
TitledPane titledPane = new TitledPane();
titledPane.setGraphic(label);
return titledPane ;
}
StackPane titleRegion = (StackPane) titledPane.lookup(".title");
titleRegion.setOnMouseClicked(System.out::println);
EDIT:
Sometimes titledPane.lookup(".title") returns null which means CSS is not applied to the node. To resolve this issue, you need to use applyCss() and layout() on the pane that contains the TitledPane.
See:
JavaFX TitledPane lookup(.title) returns null

How to creat a button programmatically with JavaFX?

I want to make a button on an AnchorPane without drag it from the library in the FXML file I want to do it progammatically: if the search button clicked, should show a new button not existed in the AnchorPane before I did this code but I don't know what is wrong with it:
private void searchButton(ActionEvent evt) {
Button tab = new Button();
tab.setLayoutX(147);
tab.setLayoutY(102);
tab.setText("Tab1");
tab.setPrefHeight(27);
tab.setPrefWidth(69);
WebView wb = new WebView();
wb.setLayoutX(-1);
wb.setLayoutY(128);
wb.setPrefWidth(1604);
wb.setPrefWidth(700);
}
I am assuming that you searchButton method is in controller attached to some FXML. Then all you need to do is this:
yourAnchorPane.getChildren().add(tab);
If you don't have already published reference to anchorPane in your controller, then add this into your controller
#FXML
AnchorPane yourAnchorPane;
And in SceneBuilder select your anchorPane, go to code tab and enter "yourAnchorPane" as fx:id.
Further info on working with anchorpane is javadoc.
You probably also want to set some constraints on the tab to locate it at a position within the AnchorPane. For instance, the following code will locate your button tab relative to the top left corner of the AnchorPane: Ten pixels down and fifteen pixels to the right.
AnchorPane.setTopAnchor(tab, 10.0);
AnchorPane.setLeftAnchor(tab, 15.0);

Resources