Event Handler in javaFx for menu - javafx

I have a question in JavaFX , how can i set an event Handler for menu (not menu bar or menu item) that when i clicked on the menu a popup window appear. i have tried this but when i click on menu nothing happens:
settingsMenu.addEventHandler(MouseEvent.MOUSE_CLICKED,event -> {//To DO});
and even the code below doesn't works:
settingMenu.setOnAction(event -> {//To Do});

Here is a hack. Menu has a constructor Menu(String text, Node graphic). Set the String to empty-string and the Node to Label. Then add a MouseListener to the Label.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author Sedrick
*/
public class JavaFXApplication33 extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Help!");
label.setOnMouseClicked(mouseEvent->{System.out.println("Hello World!");});
Menu menu = new Menu("", label);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(menu);
StackPane root = new StackPane();
root.getChildren().add(menuBar);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Related

Javafx add a dropdown event listener for choicebox

I have a ChoiceBox and I want to refresh it's content whenever user expands it. I haven't found a proper listener for this. All the stuff google gives is related to handling ChangeValue events.
I reckon I should add eventListener<ActionEvent> to ChoiceBox since what I'm handling is click on a ChoiceBox, but my implementation doesn't work.
ActionEvent fires when I click on any List value, not when I click ChoiceBox itself.
Register a listener with the choice box's showingProperty:
choiceBox.showingProperty().addListener((obs, wasShowing, isNowShowing) -> {
if (isNowShowing) {
// choice box popup is now displayed
} else {
// choice box popup is now hidden
}
});
Here is a quick demo:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ChoiceBoxPopupTest extends Application {
private int nextValue ;
#Override
public void start(Stage primaryStage) {
ChoiceBox<Integer> choiceBox = new ChoiceBox<>();
choiceBox.getItems().add(nextValue);
choiceBox.setValue(nextValue);
choiceBox.showingProperty().addListener((obs, wasShowing, isNowShowing) -> {
if (isNowShowing) {
choiceBox.getItems().setAll(++nextValue, ++nextValue, ++nextValue);
}
});
BorderPane root = new BorderPane();
root.setTop(choiceBox);
BorderPane.setAlignment(choiceBox, Pos.CENTER);
root.setPadding(new Insets(5));
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX - Turning off possible focus on TextArea

I have TextArea and TextField in my app. I have managed to give focus to TextField on the start and made TextArea impossible to edit. I want to also somehow turn off possibility to focus it with forexample mouse click or TAB cycling.
Is there any suitable way to do it?
You need to use:
textArea.setFocusTraversable(false);
textArea.setMouseTransparent(true);
Example demonstration :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* #author StackOverFlow
*
*/
public class Sample2 extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = new BorderPane();
// Label
TextArea textArea1 = new TextArea("I am the focus owner");
textArea1.setPrefSize(100, 50);
// Area
TextArea textArea2 = new TextArea("Can't be focused ");
textArea2.setFocusTraversable(false);
textArea2.setMouseTransparent(true);
textArea2.setEditable(false);
// Add the items
pane.setLeft(textArea1);
pane.setRight(textArea2);
// Scene
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
// Show stage
primaryStage.show();
}
/**
* Application Main Method
*
* #param args
*/
public static void main(String[] args) {
launch(args);
}

How to set the ellipsis string to an empty string if it is the only text displayed in a JavaFX Label

I'm trying to keep the Elipsis String (...) on my Label (lbl) but I want it to be set to an empty String when the window gets too small. I'm using the code here to show an example. I'm using the ResizeHelper class found here: Allow user to resize an undecorated Stage.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* #author bparmeter
*/
public class NewFXMain extends Application {
#Override
public void start(Stage primaryStage) {
Label lbl = new Label();
lbl.setText("Hello World");
AnchorPane root = new AnchorPane();
AnchorPane.setBottomAnchor(lbl, 0.0);
AnchorPane.setTopAnchor(lbl, 0.0);
AnchorPane.setRightAnchor(lbl, 0.0);
AnchorPane.setLeftAnchor(lbl, 0.0);
root.getChildren().add(lbl);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
ResizeHelper.addResizeListener(primaryStage);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Not the best solution but it works...
lbl.widthProperty().addListener((ov, o, n) -> {
final Text t = new Text(lbl.getText().charAt(0)+"...");
t.applyCss();
final double width = t.getLayoutBounds().getWidth();
lbl.setEllipsisString(width>=lbl.getWidth() ? "" : "...");
});

How to get Treeview DisclosureNode to right , in Javafx?

In Javafx the DisclosureNode of Treeview is on left by default. How can I I place the Disclosure Node to Right (float right)..??
Any Help would be appreciated.
You only need to set the Node Orientation to get a right to left behaviour. But be aware of, that all childrens of this TreeView will inherit the orientation as a default.
import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TreeViewRightLeft extends Application {
#Override
public void start(Stage primaryStage) {
TreeItem<String> item = new TreeItem<>("Root Node");
item.setExpanded(true);
item.getChildren().addAll(
new TreeItem<>("Item 1"),
new TreeItem<>("Item 2"),
new TreeItem<>("Item 3")
);
TreeView<String> treeView = new TreeView<>(item);
// Here you can select the orientation.
treeView.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
BorderPane root = new BorderPane(treeView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("TreeView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Right to Left
Left to Right (default)

JavaFX How to 'hide' TitledPane back after expanding

I have TitledPane, which i want to hide back (after expanding - un-expand it) after pressing a button. Is there any way to do it? I didn't find any way :( Thanks!
Just do
titledPane.setExpanded(false);
Complete example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TitledPaneExample extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Some content");
Button button = new Button("OK");
VBox content = new VBox(10, label, button);
TitledPane titledPane = new TitledPane("Titled Pane", content);
button.setOnAction(e -> titledPane.setExpanded(false));
VBox root = new VBox(titledPane);
Scene scene = new Scene(root, 250, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Resources