I need to disable the close event using Alt + F4 keyboard shortcut. For now, I'm trying to filter the events in my Scene for this keypress and consume it, but didn't have any success, the close event happens anyway. Follows bellow part of my code:
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.isAltDown() && event.getCode().equals(KeyCode.F4)) {
event.consume();
}
});
primaryStage.setOnCloseRequest((ev) -> System.exit(0));
You can try to disable the implicit exit:
Platform.setImplicitExit(false);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
event.consume();
}
});
and then create a button which will close the app on click:
Button btn = new Button();
btn.setText("Close");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
Related
Is it possible to simply check if a key is currently pressed in JavaFX without listening for a KeyEvent? In my case I want to check if a key is down while a button is being pressed.
button.setOnAction(e -> {
// This is where I want to check if a specific key is currently down.
if(keyIsDown) {
// do something
} else {
// do something else
}
});
I can think of a couple of workarounds. I'm just wondering if there is a right way to do this.
Here you go this code works like a charm with me when I'm pressing any button on keyboard then click a button prints key down
SimpleBooleanProperty simpleBooleanProperty = new SimpleBooleanProperty();
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
simpleBooleanProperty.setValue(true);
}
});
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
simpleBooleanProperty.setValue(false);
}
});
done.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if(simpleBooleanProperty.get()){
System.out.println("Key Down");
}else { //key released
System.out.println("Key Up");
}
}
});
I have a context menu which activated on right click of a node in my JavaFX application. Originally I just had one menu item, but I decided to add another. When I added another, the second menu item worked but the first menu item stopped working. Here was my code:
final ContextMenu contextMenu = new ContextMenu();
MenuItem delete = new MenuItem("Delete");
MenuItem hyperlink = new MenuItem("Hyperlink...");
contextMenu.getItems().addAll(delete, hyperlink);
//handles deletion
webView.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e){
if (e.isSecondaryButtonDown()){
contextMenu.show(workspace, e.getScreenX(), e.getScreenY());
delete.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
//do stuff
}
});
}
}
});
//handles hyperlink
webView.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e){
if (e.isSecondaryButtonDown()){
contextMenu.show(workspace, e.getScreenX(), e.getScreenY());
hyperlink.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
//do stuff
}
});
}
}
});
}
I assumed this was because the listeners were overriding each other, so I changed it to add event handlers. Here's how I changed the code:
final ContextMenu contextMenu = new ContextMenu();
MenuItem delete = new MenuItem("Delete");
MenuItem hyperlink = new MenuItem("Hyperlink...");
contextMenu.getItems().addAll(delete, hyperlink);
//handles deletion
webView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e){
if (e.isSecondaryButtonDown()){
contextMenu.show(workspace, e.getScreenX(), e.getScreenY());
delete.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
//do stuff
}
});
}
}
});
//handles hyperlink
webView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e){
if (e.isSecondaryButtonDown()){
contextMenu.show(workspace, e.getScreenX(), e.getScreenY());
hyperlink.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
//do stuff
}
});
}
}
});
Now, NOTHING is happening when I right-click the node. Can someone explain why, and what my solution might be?
There is a number of things:
Yes, in your first version, the handlers were overriding each other.
Direct translation to addEventHandler style would use event type MouseEvent.MOUSE_PRESSED, not MOUSE_CLICKED, since MOUSE_CLICKED occurs only after the mouse button has been released, thus the isSecondaryButtonDown() test always returns false—that's why nothing is happening.
Instead of detecting mouse presses, you can listen to CONTEXT_MENU_REQUESTED events:
webView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override
public void handle(ContextMenuEvent e) {
contextMenu.show(workspace, e.getScreenX(), e.getScreenY());
}
}
You should set the menu item's action right after its creation, not on each right click:
MenuItem delete = new MenuItem("Delete");
delete.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
//do stuff
}
});
The only thing you need to do on right click is show the context menu, as shown above.
Why are you duplicating code. Just follow your first approach and do both things inside the same EventHandler.
One more note: Define your ActionEvents for the MenuItems outside of the webView handlers. What you are doing now is every time you click a menuItem you are resetting the ActionEvents. From your code, I can only see that you need to detect the MouseEvent just to show the Context Menu. Which can be done in one webView.setOnMousePressed(...).
Here is my code :
Button start = new Button("Start");
start.setPrefHeight(100d);
start.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent arg0) {
//ss.startServer();
System.out.println("adfasddasfrwgafsdasdf");
}
});
The message does not get printed on the console at eclipse! I want the event to trigger when the mouse is released. What am I doing wrong?
Upon furthet investagation I realised that if I have another mouseReleased event on another node , it will fire more events!... why is that?
For example , here is the code I am using, when I press the start button , it prints Start and Reload!
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent arg0) {
ss.startServer();
System.out.println("Start");
arg0.consume();
}
});
Button stop = new Button("Stop");
stop.setPrefHeight(100d);
stop.setMaxWidth(Double.MAX_VALUE);
stop.setOnMouseReleased(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
sc.stopServer();
System.out.println("Stop");
event.consume();
}
});
Button reload = new Button("Reload");
reload.setPrefHeight(100d);
reload.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent some) {
sc.reloadServer();
System.out.println("Reload");
some.consume();
}
});
Stop button works properly!
Anyway , if the other piece of code works , here it is(http://pastebin.com/4f55R2gN)
You typo'd your code. You set your MouseReleased handler on the start object instead of reload.
Button reload = new Button("Reload");
reload.setPrefHeight(100d);
reload.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
Try this
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
System.out.println("button click and mouse released");
}
});
The below works for me fine.
public class FxmlSample extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws IOException{
JFrame frame = new JFrame();
JFXPanel jfxPanel = new JFXPanel();
Pane pane = new Pane();
VBox vBox = new VBox();
Button button = new Button("Click me");
button.setMinSize(50.0, 50.0);
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
System.out.println("11111111111111");
}
});
Button button_2 = new Button("Click me 2");
button_2.setMinSize(50.0, 50.0);
button_2.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
System.out.println("2222222222222");
}
});
vBox.setPadding(new Insets(20));
vBox.getChildren().addAll(button, button_2);
pane.getChildren().addAll(vBox);
Scene scene = new Scene(pane);
jfxPanel.setScene(scene);
frame.add(jfxPanel);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(700, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
my layout like :
i want focus at Company Name Texfield when i press TAB button at first time , but right now i get focus at Add Button how can i manage it ?
i try code like
ChangeFocus(mTextFieldCompanyName, mTextAreaAboutUs);
ChangeFocus(mTextAreaAboutUs, mTextAreaContactUs);
ChangeFocus(mTextAreaContactUs, mButtonVideo);
ChangeFocus(mButtonVideo, mButtonImage);
ChangeFocus(mButtonImage, mButtonSave);
public void ChangeFocus(Control mControlFrom,final Control mControlTo)
{
mControlFrom.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if (event.getCode() == KeyCode.TAB)
{
System.out.println("TAB pressed");
mControlTo.requestFocus();
event.consume(); // do nothing
}
}
});
}
Try wrapping your mTextFieldCompanyName inside Platform.runLater()
Platform.runLater(new Runnable() {
#Override
public void run() {
mTextFieldCompanyName.requestFocus();
}
});
Hope it helps :)
I have a ListView with table names in it and an add button.
tablesListView.setEditable(true);
tablesListView.setCellFactory(TextFieldListCell.forListView());
tablesListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
addTableButton.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent evt) {
tablesListView.getItems().add("");
tablesListView.getSelectionModel().selectLast();
tablesListView.edit(tablesListView.getSelectionModel().getSelectedIndex());
}
});
So when i click on some cell in the list it switches its state to the TextField, but that's not working with the button, the cell becomes selected, but not switched. Any help?
Try
tablesListView.setEditable(true);
tablesListView.setCellFactory(TextFieldListCell.forListView());
tablesListView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
final Timeline animation = new Timeline(
new KeyFrame(Duration.seconds(.1),
new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
tablesListView.edit(tablesListView.getSelectionModel().getSelectedIndex());
}
}));
animation.setCycleCount(1);
addTableButton.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent evt) {
tablesListView.getItems().add("");
tablesListView.getSelectionModel().selectLast();
animation.play();
}
});
Actually Platform.runLater() works, but only occasionally, cause it runs the runnable in "some unspecified time in the future". So you need to specify the time when the edit command should run by for instance the timeline in my code example. The edit command postponed for 0.1 sec.