How can I detect when a MenuButton open its menu in Javafx? - javafx-2

As there is not an OnShowing event in this control (at least in Javafx 2.2), and there is no way to access the underlying menu control, I find no way to populate dynamic menu items here.
I tried using the OnAction event, but it doesn't fire.
I tried using the OnShowing event of the menu inside the MenuButton that contains the dynamic choices, but that doesn't fire either.
There must be some way, but I can't think of any.

Adding dynamic menu items on showing can be this way:
#Override
public void start(Stage stage) throws Exception {
final MenuButton m = new MenuButton("Cities");
m.getItems().addAll(new MenuItem("Moscow"), new MenuItem("Bishkek"), new MenuItem("Istanbul"));
m.showingProperty().addListener(new ChangeListener<Boolean>() {
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(newValue) {
m.getItems().add(new MenuItem("new city item"));
}
}
});
StackPane stackPane = new StackPane();
stackPane.getChildren().add(m);
Scene scene = new Scene(stackPane);
stage.setScene(scene);
stage.show();
}

Related

How to make a text editor with the insertion of images?

I need to implement a text editor JavaFX, but I have a problem with the creation of the function of adding the image in the text editing area.
It's exactly that when you click the button it was possible to add an image as the screenshot:
Now, I tried to use WebView where I put TextArea code below:
private WebView view;
private WebEngine engine;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
view = new WebView();
engine = view.getEngine();
engine.loadContent("<body><textarea id='content'></textarea><p id='ding' style='display:none;'></p></body>");
view.setPrefHeight(240);
Button btn = new Button("Click Me!");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
append();
engine.executeScript(
"document.getElementById('ding').innerHTML=document.getElementById('content').value;"
);
}
});
Button btn2 = new Button("Click Me 2!");
btn2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
append();
engine.executeScript(
"document.getElementById('ding').innerHTML=document.getElementById('content').value;"
);
}
});
StackPane root = new StackPane();
root.getChildren().addAll(view, btn, btn2);
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}
private void append() {
Document doc = engine.getDocument();
Element el = doc.getElementById("ding");
String s = el.getTextContent();
s += "<img src='http://bluebuddies.com/gallery/Historical_Smurfs/jpg/Smurfs_Historical_Figure_20506_Abraham_Lincoln.jpg'/>";
el.setTextContent(s);
System.out.println(s);
}
After clicking the button adds to the text in the text area link to the photo but unfortunately TextArea This is not supported and when I wanted to use some html editor problem so that I will not have the opportunity to extract all the text correctly with the formatting.
I tried solutions such as RichTextFX but unfortunately he does not have the ability to add images. The HTML editor also did not meet my expectations because I have entered text later saved to a file, for example type: docx. extract text from this editor with formatting is unfortunately difficult.
Are there any other solutions to create word processing with advanced features in JavaFX?
Ideally it was that it could be solved with normal controls TextArea without the use of WebView.

How to add ScrollPane to main stage

I'm interested how I can add ScrollPane to the main stage. I want when I reduce the size of the main page to use ScrollPane to move over the main stage. Is there any example?
If your content of the ScrollPane is larger than the allowed size of the ScrollPane, the ScrollPane should automatically attach scrollbars. Here's an example:
public class JFXScroll extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane main = new BorderPane();
ScrollPane scroll = new ScrollPane();
VBox box = new VBox();
// Demo purposes; Wouldn't normally do this - just let the box automatically fit the content
box.setPrefSize(1000, 500);
box.setEffect(new ColorInput(0,0,1000,500,Color.LIME));
scroll.setContent(box);
main.setLeft(new Label("Left Content"));
main.setCenter(scroll);
Scene scene = new Scene(main, 300, 250);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
stage.setScene(new ScrollPane(yourContent)); should do it.
Of course your content should have an appropriate min, max and preferred sizes

How to create modal dialog with image

I have this very simple modal dialog:
public class DialogPanels
{
public void initClosemainAppDialog(final Stage primaryStage)
{
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
#Override
public void handle(WindowEvent event)
{
event.consume(); // Do nothing on close request
// Dialog Stage init
final Stage dialog = new Stage();
// If you want to freeze the background during dialog appearence set Modality.APPLICATION_MODAL
// or to allow clicking on the mainstage components set Modality.NONE
// and set dialog.showAndWait();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
// Frage - Label
Label label = new Label("Exit from the program");
// Button "Yes"
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//primaryStage.close();
//dialog.close();
//Platform.exit();
System.exit(0);
}
});
// Button "No"
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
primaryStage.show();
dialog.close();
}
});
// Layout for the Button
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(okBtn);
hbox.getChildren().add(cancelBtn);
// Layout for the Label and hBox
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.getChildren().add(label);
vbox.getChildren().add(hbox);
// Stage
Scene scene = new Scene(vbox, 450, 150, Color.WHITESMOKE);
dialog.setScene(scene);
dialog.show();
}
});
}
}
I want to add image and to make it to look like this:
But I admin that it's too complex for my short knowledge to get the appropriate result. Can you show me how I can split the dialog, add second background and make my code to look the same as this example please?
Have a look at the ControlsFX project, they have some sophisticated dialogs and it's open source, so you can look up how it's done. For example, your dialog looks like this confirmation dialog of ControlsFX:
There is also support for custom dialogs.
€dit:
With the "show Masthead" option enabled it actually looks exactly like it:

Make menubar call method when it is clicked?

I am wanting to add an empty javafx.scene.control.Menu to a MenuBar and have it call a method when it is clicked.
I have tried using menu.setOnShowing(new EventHandler<Event>(){}); with no luck.
Here is what I am currently working with:
public MenuBar createMenuBar() {
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File");
Menu addAccountTab = new Menu("Add Tab");
addAccountTab.setOnShowing(new EventHandler<Event>() {
public void handle(Event e) {
System.out.println("addAccountTab Menu clicked.");
}
});
menuBar.getMenus().add(addAccount);
return menuBar;
}
However, clicking the Menu does not call the onShowing event.
Your Menu needs to contain at least one MenuItem for the event to fire.
public class MenuApplication extends Application {
#Override
public void start(Stage primaryStage) {
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File");
Menu addAccountTab = new Menu("Add Tab");
addAccountTab.setOnShowing(new EventHandler<Event>() {
#Override
public void handle(Event e) {
System.out.println("addAccountTab Menu clicked.");
}
});
MenuItem NewMenuItem = new MenuItem("New");
addAccountTab.getItems().add(NewMenuItem);
menuBar.getMenus().addAll(file, addAccountTab);
StackPane root = new StackPane();
root.getChildren().add(menuBar);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The output I get is shown below:
Although the API suggests otherwise, the onShowing event doesn't get called when there are no MenuItems in the Menu or when they are all hidden.
I was able to solve the issue by using Menu's hide() method inside of the onShown event like this.
public MenuBar createMenuBar() {
MenuBar menuBar = new MenuBar();
Menu addAccount = MenuBuilder.create()
.onShown(new EventHandler<Event>() {
public void handle(Event e) {
((Menu)e.getSource()).hide();
System.out.println("addAccount Clicked");
}
}).items(new MenuItem())
.text("Add Account").build();
menuBar.getMenus().addAll(addAccount);
return menuBar;
}

Popup window with table view in JavaFX 2.0

I want to click a button in order to make a popup window appear with a tableview element inside it. Can anyone tell me how to do it?
Thanks in advance.
This is the code for simple popup window in JavaFX.
Hope this helps.
public class PopupExample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Popup Example");
final Popup popup = new Popup();
popup.setX(300);
popup.setY(200);
popup.getContent().addAll(new Circle(25, 25, 50, Color.AQUAMARINE));
Button show = new Button("Show");
show.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popup.show(primaryStage);
}
});
Button hide = new Button("Hide");
hide.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popup.hide();
}
});
HBox layout = new HBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(show, hide);
primaryStage.setScene(new Scene(layout));
primaryStage.show();
}
}
What kind of popup window do you need? Implemented with using a new Stage or Popup control? JavaFX has a control named Popup, read about it to see does it meet your needs. An entry point for Stage version could be Dialog with CLOSE button.

Resources