I'm trying to build a calculator, and I have looked all over internet and the examples don't help me, so I have buttons created and everything, I'm trying to display on:
TextField Result = new TextField();
Result.setEditable(false);
Result.setAlignment(Pos.CENTER_RIGHT);
Result.setMinSize(210, 30);
Result.textProperty().bind(Bindings.format("%.0f" , value));
pane.getChildren().add(Result);
the number of the button pressed, how do I do that? Let's say the button was:
Button uno = new Button("1");
uno.setMinSize(40, 40);
pane.getChildren().add(uno);
How do I make the text field Result show number 1?
BIG Thanks!
The simplest way is to add a listener for each your button and change your TextField dynamically:
uno.pressedProperty().addListener((o, old, newValue) -> Result.setText("1"));
dos.pressedProperty().addListener((o, old, newValue) -> Result.setText("2"));
Note, when the user pressed and unpressed a button your listener will react twice with newValue = true/false. Do the additional checking if needed:
uno.pressedProperty().addListener((o, old, newValue) -> {
if (newValue) {
Result.setText("1")
}
});
UPDATE:
Don't forget to remove this line as wrong solution:
Result.textProperty().bind(Bindings.format("%.0f" , value));
Related
I have a contextMenu and I added it to a button. I want the contextMenu to be displayed when I left-click(PRIMARY) on the button. How can I do this? Because by default just right-clicking does this.
I tried this way but it did not work
Button sortBy = new Button();
ContextMenu sortByMenu = new ContextMenu();
sortByMenu.addEventFilter(MouseEvent.MOUSE_PRESSED, ev -> {
if (ev.getButton() == MouseButton.PRIMARY) {
//does not do anything
}
});
sortBy.setContextMenu(sortByMenu);
MenuButton
MenuButton is a button which, when clicked or pressed, will show a ContextMenu.
Example code from the javadoc:
MenuButton m = new MenuButton(
"Eats"
);
m.getItems().addAll(
new MenuItem("Burger"),
new MenuItem("Hot Dog")
);
ChoiceBox
You could also consider a ChoiceBox, depending on what you are trying to do.
The ChoiceBox is used for presenting the user with a relatively small set of predefined choices from which they may choose.
ChoiceBox<String> cb = new ChoiceBox<>();
cb.getItems().addAll(
"item1",
"item2",
"item3"
);
To select a sortBy sorting field from a list of choices, a ChoiceBox would probably be a good fit.
Essentially, what I'm trying to do is something like to a text-based RPG using JavaFX. Right now, to display some text, I've got this:
final IntegerProperty i = new SimpleIntegerProperty(0);
Timeline timeline = new Timeline();
KeyFrame keyFrame = new KeyFrame(
Duration.millis(70),
event -> {
if (i.get() > info.getText().length()) {
timeline.stop();
} else {
text.setText(info.getText().substring(0, i.get()));
i.set(i.get() + 1);
}
});
timeline.getKeyFrames().add(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
timeline.setOnFinished(a -> {
hb_start.getChildren().clear();
hb_start.getChildren().addAll(start_left,start_right);
hb_start.setAlignment(Pos.CENTER);
});
Because the length of the animation depends on the size of the text, the cyclecount is set to indefinite. Unless there's some other way I'm missing to make the animation play once and then stop, I'd like it so that when you press enter (or some other key that I decide on later) for it to call timeline.stop(); but I can't figure out how to add any sort of listener. Trying to implement keyListenerseems to come with all sorts of stuff that I don't need, and it also doesn't work with a TextField, and instead wants a JTextField, which might be fine, except that I don't have a clue how to do anything with Swing.
Currently, the text is being displayed in aTextFlow from the text of Text. I'm assuming the listener would be added to the TextFlow, or even the scene itself, honestly, I'm at a loss for what to do. It sounds simple, but I can't seem to figure it out.
KeyListener is a AWT class, not a JavaFX class. Unless you're embedding a Swing component in your JavaFX application or a JavaFX node in a Swing application, you should use JavaFX's equivalent EventHandler<KeyEvent> instead. Furthermore there is no need to include a TextField (or Swing's JTextField) in your application just for the sake of receiving key events. You could add the listener directly to the Scene:
final KeyCode stopKey = KeyCode.ENTER;
EventHandler<KeyEvent> handler = event -> {
if (event.getCode() == stopKey) {
timeline.stop();
}
};
scene.setOnKeyPressed(handler);
Note that events can be consumed by nodes before they reach the scene, e.g. by a TextField that has the focus. In this case you could make sure you get the event by registering a the listener as a event filter instead:
// scene.setOnKeyPressed(handler);
scene.addEventFilter(KeyEvent.KEY_PRESSED, handler);
I tried a few methods:
tabPane.getSelectionModel().selectedItemProperty().addListener((e, o, n)
if (tabPane.getTabs().isEmpty()) someButton.setDisable(false);
and this when creating the tab:
tab.setOnCloseRequest(e ->
if (tabPane.getTabs().isEmpty()) someButton.setDisable(false);
But both are not working. The second method is definitly wrong as it is checking if there are tabs before actually closing the tab. Any solutions? Many thanks
Create isNotEmpty BooleanBinding on TabPane ObservableList<Tab>.
TabPane tp = new TabPane(new Tab("A"),new Tab("B"));
final BooleanBinding empty = Bindings.isNotEmpty(tp.getTabs());
Button someButton = new Button();
someButton.disableProperty().bind(empty);
I'm not sure if the below is what you want, but you can check that the size of the matched tab list in the tabpane <= 1 rather than empty.
tab.setOnCloseRequest(event -> {
TabPane tabPane = tab.getTabPane();
if (tabPane.getTabs().size() <= 1) {
// don't allow the last tab to be closed.
event.consume();
return;
}
});
Consuming the close request will prevent closure, but you could do other work in that event as well or instead (such as manipulating the disable property of your button) if you wish.
Usually button disable properties are well controlled via a binding, so perhaps something like MBec's solution might be a good idea in that case if that is all you need to accomplish.
I have a PopOver ControlsFX and I want to create an event every time disappears. You searched information and I think it is done with the method onHiddenProperty but I cannot apply it correctly.
You can check if it has lost the focus which means that the popover disappears:
popOver.focusedProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue){
// oldValue is true -> current value is false-> no focus
// TODO your code here
}
});
I have a SplitMenuButton, and I can't seem to find a way to trigger an event when the user clicks the arrow next to the button.
I would like to select item from dropdown(when mouse clicked on that item), display that in splitmenubutton and store value of that item in string to send to database.
I am not sure which event can do that, and I can not find any info on this either.
You can do something like.
for (final MenuItem item : smb.getItems()) {
item.setOnAction((event) -> {
System.out.println("Selected!");
});
}