Bad event in Java panel

I have a java panel with 4 buttons. When I click on these buttons a new frame appears and the first one is hidden with setVisibile(false)

. In this new window, I have another button, but when I click it I got the event corresponding to the fourth button of the first window. Pressing the button again does the trick, but of course it is not acceptable. Am I missing something? I am just displaying frames with

nameOfTheFrame.setVisible(true);

      

and I have MouseListeners on each button.

The last button code is simple:

System.exit(0);

      

EDIT

Sample code:

    private void btn_joinGamePressed(java.awt.event.MouseEvent evt) {
            GraphicsTools.getInstance().getCreateGame().setVisible(false);
            GraphicsTools.getInstance().getMainPanel().setVisible(false);
            GraphicsTools.getInstance().getRegistration().setVisible(true);
}

      

GraphicsTools is a Singleton.

EDIT 2 More information. I noticed that it works fine on MAC OS. The problem only occurs on Linux and Windows.

+2


a source to share


2 answers


This must be happening because of your mouse listeners. Maybe it is the identification of the old button in your first click, which is in the same place of the new button (this is just my guess).

Change your mouse listeners to action listeners. It's enough for a button if you have an action listener.



Try it.

+1


a source


Try calling revalidate()

on frames when their liveness changes.

Edit:



It could be something with the creation of frames. Make sure you call 'pack ()' on frames.

0


a source







All Articles