Can not catch event in the second click - java

Hj. I have a combobox in Javafx.
Initially, I am in AVAILABLE. Then I press the combobox to choose 'AUXUALY' state, it will pop up a dialog to confirm 'AUXUALY' or not. The issue happens when I cancel the pop-up, I can not catch event click to 'AUXUALY' in the next click.
Looking to the attached image, it seems we are still in 'AUXUALY' state because I can see a rectangle covering the 'AUXUALY' button. I can catch event only if I select the LOGOUT state.
Is there anyone here suggest me to overcome this problem?
class MyCombobox extends Combox{
this.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) { // can not catch event here after canceling the dialog
if change = 'AVAILABLE' then
if change = 'AUXUALY' then PopUp.getinstance().showDialog();
if change = 'LOGOUT' then
}
}
class PopUp extends Window{
btOk.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
...
}
btCancel.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
hide();
}
public void showDialog(){
this.show();
}
}

Related

How can I create Enter event when I want to click on button, without using class Robot?

How can I create Enter event when I want to click on button, without using class Robot? I have already created a form and I create button btnMainOk.
I've tried to use keyListener but it's not working.
btnMainOk.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
btnMainOk.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if (e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("Hello");
}
}
});
}
});
The goal is when I click with my mouse device on button, that button should act like I pressed enter key on my keyboard.

Handling nested events

I have a game that displays an archer and I am trying to set it up so that when my button is pressed, the user is then allowed to click anywhere on the screen and then a new archer would be set to where the click happen. My code currently allows me to click the screen and set a new archer regardless of whether the button was pressed or not. Could someone explain what is wrong becuase I though MouseEvent would occur on the scene once the button was pressed.
myButton.setOnMouseClicked(
new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
gc.setFill(color);
gc.fillRect(0,0,width,height);
scene.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent e)
{
archer.setX((int)e.getSceneX());
archer.setY((int)e.getSceneY());
archer.drawCharStand(gc);
}
});
}
});
You could use a ToggleButton, so that you only place the archer when the toggle button is selected:
private ToggleButton myButton = new ToggleButton("Place Archer");
// ...
scene.setOnMouseClicked(e -> {
if (myButton.isSelected()) {
archer.setX((int)e.getSceneX());
archer.setY((int)e.getSceneY());
archer.drawCharStand(gc);
myButton.setSelected(false);
}
});
The last line will unselect the toggle button "automatically" after placing the archer. If you want the user to be able to place multiple archers easily (and have to manually switch off that mode), omit that line.
You will need to change your code slightly. Perhaps try with a variable that tracks if the button was clicked:
boolean archerPlacementMode = false;
....
myButton.setOnMouseClicked(new EventHandler<MouseEvent>(){
public void handle(MouseEvent e)
{
if(!archerPlacementMode) {
archerPlacementMode = true;
gc.setFill(color);
gc.fillRect(0,0,width,height);
archerPlacementMode = true;
return;
}
}
});
scene.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e)
{
if(archerPlacementMode) {
archer.setX((int)e.getSceneX());
archer.setY((int)e.getSceneY());
archer.drawCharStand(gc);
archerPlacementMode = false;
}
}
});

JavaFX button not responding on first click

public void handle(){
submit.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
LoginConnection login = new LoginConnection();
boolean pass = login.login(usernameField.getText(), passwordField.getText());
if(pass)
flip(SceneNames.Main);
else
invalLoginMessage.setOpacity(1.00);
}
});
register.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
flip(SceneNames.Register);
}
});
}
When i click on submit or register, it takes two click for it to do anything. How do i fix this?
What happens is that on first click it adds the handlers specified in the method and on second and consecutive clicks, it uses the handlers. To fix it just create separate methods to add through fxml or scene builder.

Java focusedProperty listener on Comment window JavaFX

I am trying to implement a listener for a comment window (stage) that I have designed that contains a textbox and two active buttons.
When "OK" button is pressed, the program takes the input from the
textbox, closes the window and parses the text on another stage.
The "Cancel" button just closes the stage ignoring the text.
"OK" Button method:
// Ok Button listener.
okButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
try
{
//Close the stage
Stage stage = (Stage) okButton.getScene().getWindow();
stage.close();
//Parse the textbox text to a singleton
TOPGun_Site_Archive_Singleton.getInstance().setAddNewCommentText(siteArchive_Comments_TextArea.getText());
}
catch (Exception ex)
{Logger.getLogger(Project_TOP_GUN.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
This is the "Cancel" button method:
//Cancel Button listener
cancelButton.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
try
{
//Close the stage
Stage stage = (Stage) okButton.getScene().getWindow();
stage.close();
}
catch (Exception ex)
{
Logger.getLogger(Project_TOP_GUN.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
When the user clicks entirely away from the comment stage, I want to fire the "OK" button event method that I have implemented in the comment stage's controller.
I tried attaching the focus listener on the comment window's textbox, but if the user tries to click anywhere else, the textbox loses focus and the "OK" method is triggered.
siteArchive_Comments_TextArea.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Textfield on focus");
//do nothing
}
else
{
System.out.println("Textfield out focus");
//If the Textfield gets out of focus, run the OK method. i.e copy the contents of the Textfield to another stage and close the comment window
okButton.fire();
}
}
});
I also tried attaching the focus listener to the parent stage that is calling this comment window, but when the user clicks away from the comment stage I can't access the "OK" method inside the comment stage's controller.
// Call and open the comment stage from here
// and place a listener on the comment stage itself
floating_TextArea_Stage.setScene(floating_TextArea_Scene);
floating_TextArea_Stage.initStyle(StageStyle.TRANSPARENT);
floating_TextArea_Stage.show();
// add the focus listener on the stage
floating_TextArea_Stage.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Stage on focus");
//no need to do anything here
}
else
{
System.out.println("Stage out of focus");
//this doesnot work
//okButton.fire();
// This doesn't work either
floating_TextArea_Stage.okButton.fire()
}
}
});
I have been trying to find the best way to do it for DAYS! Have you got any ideas?

GWT prevent popup panel from opening

I have a MenuItem that has a ScheduledCommand attached. When the user clicks on the menu, a new PopupPanel appears that has autoHide enabled. Now when the user clicks on the MenuItem while the popup is open, the panel gets closed, but immediately opens again as the PopupPanel's close event fires as a click event on the menu item. Can somebody tell me how can I prevent the PopupPanel from opening in this case?
My code is something like this:
#UiField
protected MenuItem menuItem;
....
menuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
PopupPanel window = new PopupPanel();
window.init();
window.addCloseHandler(new CloseHandler<PopupPanel>() {
#Override
public void onClose(final CloseEvent<PopupPanel> event) {
// TODO Maybe something here?
}
});
window.show();
}
});
Create a single instance for the popup and use PopupPanel#isShowing method to hide or show the popup.
public class MyMenuClass{
private PopupPanel window;
....
....
menuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
if(window==null){
window = new PopupPanel(true);
window.add(new Label("Hello close me!!!"));
}
if(window.isShowing()){
window.hide();
}else{
window.show();
}
}
}
OK, I managed to do this by checking whether the last hovered element on the Menubar was the menuItem that opens the window. To do this, I had to subclass the default MenuBar class and exposing the getSelectedItem() method (it was protected by default, why?)
#UiField
MyMenuBar myMenuBar;
....
menuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
if (!wasHoveredWhenClosed) {
window.init();
window.addCloseHandler(new CloseHandler<PopupPanel>() {
#Override
public void onClose(final CloseEvent<PopupPanel> event) {
wasHoveredWhenClosed = myMenuBar.getSelectedItem() != menuItem;
}
});
window.show();
} else {
wasHoveredWhenClosed = false;
}
}
});

Categories