ActionListener called twice, added Once - java

I created one JFrame. This JFrame contains a JLabel, which contains some JButtons. The JButtons have an ActionListener (called MainFrameListener). When the arrowButton Button is clicked a method is executed by the code. This method removes all ActionListeners from the old Buttons with foodButton.removeActionListener(new MainFrameListener());
But although I removed the Listener the Button has still two buttons.
Of course I already searched on the Internet to fix the problem and I found a line of code wich shows the amount of Listeners for one Button.
System.out.println("Count of listeners: " + ((JButton) e.getSource()).getActionListeners().length);
The first time I click on the buttons Java says there are two buttons. When I click on the arrowButton the other menu opens and the buttons are removed. That's all like I want. When I click the arrowBackButton the application sends me back to the MainFrame. That's perfect. But when I click on the arrowButton again the Console says that I have two listeners registered for the Buttons. And the sound which comes on the click is played two times.
I don't understand that because I removed the Listeners. Is there any better method to remove Listeners?

foodButton.removeActionListener(new MainFrameListener()); wont remove anything since you are removing a newly created object that has never been added to foodButton. Keep a reference to your listener and remove it later like this:
MainFrameListener listener = new MainFrameListener();
foodButton.addActionListener(listener);
//and later somewhere else
foodButton.removeActionListener(listener);
But my advice is to avoid adding/removing listeners in the first place.

Related

Java Eclipse, how to make Windowbuilder show all Panels after execution?

I am working with WindowBuilder at the moment but have a problem making it display all panels in the program in the "Components" Window. I have a "StartPanel" for example with a button which when clicked causes the program to switch from "startPanel" to "nextPanel". Everything fine but in this case, "nextPanel" isnt shown in the "components" window, why?
When I however copy all the code which creates the "nextPanel" and write it outside of the "ActionListener" so that I do not have to click a button to create it, it appears in the "components" window. Is there a way to make every panel appear in "Components"? At the moment I have a frame with a getContentPane which has the 2 Panels in it, but only 1 is shown if I add the second one with a button..
I suggest you to create the next panel as a separate component (separate class file) so that you can edit it using window builder any time, and then instantiate it in the action listener.

Replacing contents in a frame using the optionpane-GUI

updating a panel
I am designing a program for my assignment using java.
I have made a log on box where user enters name and password and clicks on submit or register. Once clicked, an optionpane is brought
upEX.(JOptionPane.showMessageDialog(null, "Account registered", "Account",
JOptionPane.INFORMATION_MESSAGE);)
I was wondering how can I add an action listener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Another question:
My java file looks something like this
class MainFrame extends JFrame {
public MainFrame() {
// All the log in panels buttons are here, etc
}
}
I was wondering if it would be efficient to add the new box details in the same area or make a new class?
I was wondering how can I add an actionlistener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Don't. Allow the dialog to close, ascertain the action the user took, show another dialog...
Better yet, create you own JPanel, use a CardLayout, add all your views to it and navigate between them. Place this on an instance of a JFrame...
See How to Use CardLayout for more details
I was wondering if it would be efficent to add the new box details in the same area or make a new class?
It would depend. A Class should be a self contained unit of work, focused on accomplishing it's designed task...if your main class is doing more work then it should, then yes, separate the logic into separate class

Is removing actionListener necessary when you disable the button?

I'm working on a minesweeper in Java with Swing and I figured it'd be a fast way to get "rid" of a button that was clicked by using
JButton.setEnabled(false); (with a proper icon too, of course).
But do I have to remove all the listeners connected to this button later or is it enough and I can just forget about the said button then?
You have 2 different questions, one in your title, and one in your description.
Is removing actionListener necessary when you disable the button?
As stated in the previous comments, no.
But do I have to remove all the listeners connected to this button later...
Yes, if you have other kinds of listeners. For example, a MouseListener will still fire if the button is disabled. Usually, there is no need for a MouseListener on JButton, but there may be in some corner cases. I'm not sure about the other types of listeners that can be added to a JButton.
Just wanted to clarify.

How do I get a button to align to the right in MigLayout

I am adding a button to a panel using Miglayout, and try what I might, I cannot get it to go to the right end of the panel. It insists on going flush left. Oddly, the demo is kind of short on such on a example (it only shows it in the context of other buttons on the same panel.
I have a panel like this:
dialog
->complex display retrieved from another class
OK Button here.
Except that it always insists on putting it like this:
dialog
->complex display retrieved from another class
OK Button here.
OK, I got the answer to this (finally). When adding the panel that contains the button add the component constraint of "gapbefore push." I had been trying that, but only on the button itself, not the panel.
panel.add(button, "skip(the amount of cells you have), al 100%")

Bad event on java panel

I have a java panel with 4 buttons. When I click on of these buttons, a new frame appears and the first is hidden with setVisibile(false).
On that new window, I have another button, but when i click it, I got the event corresponding to the fourth button of the first window. Clicking the button again does the trick, but of course this is not acceptable.
Am I missing something? I just show the frames with
nameOfTheFrame.setVisible(true);
and I have MouseListeners on every button.
The code of the last button is simply:
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
Some more informations.
I noticed that on MAC OS works fine. The problem happens only on Linux and Windows.
This must be happening because of your mouse listeners. May be it is identifying the old button in your first click which is in the same location of new button (It is just my guess).
Change the mouse listeners to action listeners. For a button, it is sufficient if you have action listener.
Try this.
Try calling revalidate() on the frames as you change their viability.
Edit:
It could be something with the creation of the frames. Make sure you are calling 'pack()` on the frames.

Categories