calling an actionlistener from another actionlistener - java

I have an actionlistener that fires when I click a button lets call it box, I have another actionlistener for another button call it restart. What I am trying to do is when I click a box button besides calling its own actionlistener inside of that actionlistener I want to call the actionlistener for restart when a certain condition is met. Any ideas?

You could invoke
restart.doClick();
which will click the button which in turn will invoke the ActionListener.
Otherwise you can invoke the ActionListener directly by doing:
restartActionListener.actionPerformed( new ActionEvent(...) );

Related

Change a label as a result of an action listener

item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
createbox();
heroname.setText("Name of the hero: " + CharName);
}
});
This is inside a JMenuItem, where if I click it, it creates a createbox which is another frame and panel that allows me to input the character name in a text field.
Everything works as expected (Charname is a static variable that changes within createbox when the user clicks the button "finish" that closes that frame and sets CharName to whatever was inside the text field)
BUT how do I make the main frame/panel "sleep" while createbox() is doing its thing?
Basically: how can I get a Thread.sleep(x) to work so it ONLY updates the label heroname once createbox() is closed again (only when closing it via ActionListener on the finish button of course)
Or, if possible: How do I change the label of another frame with a press of a button?
I cannot change heroname within createbox() and if I try to make it sleep, the whole program is sleeping and doesn't open the 2nd frame.
You should keep the instance of other frame at createbox frame.
finish button Just should hide the createbox frame.
since you have the instance of other frame, when you click on finish, you can Just call the changeLabel method of instance.
This is a solution but maybe you can find a better one.

How to stay in the same frame with Java swing

I am trying to make a Quiz game and my idea is that i have a main frame class with buttons and a label like this
So when I click the Start button i want to stay in the same frame just delete all these buttons and label and replace them with the question and answers to it. How can I accomplish this? Read something about repaint() method and removeAll() but what is the best practice. I come up with this(dunno is it the right thing to do in the situation) but call removeAll() in the actionListener of the start button and create the new frame in this listener? Or can I make another class e.g called StartFrame and then in the actionListener of the start button call removeAll() and then MainFrame.this = new StartFrame() assuming that my class holding the initial state of the game like in the pic is called MainFrame

How to assign the enter key on my keyboard to a JButton

I have created a Jframe with a JButton for a specific action.
Now please I want a situation whereby anytime I hit my enter key on my keyboard it will perform the action I have coded in my Jbutton.
My Jframe was designed with Netbeans 7.3.
Now please I want a situation whereby anytime I hit my enter key on my keyboard it will perform the action I have coded in my Jbutton.
Make your JButton the default button of the JRootPane.
You do this by calling setDefaultButton(myJButton) on the root pane.
And you can get the JRootPane by calling getRootPane() on your JFrame.
Note that if you want this action when a JTextField has focus, then the solution is different; here you'll want to add the same ActionListener given to the JButton to the JTextField.
Implement ActionListener Interface
The listener interface for receiving action events. The class that is
interested in processing an action event implements this interface,
and the object created with that class is registered with a component,
using the component's addActionListener method. When the action event
occurs, that object's actionPerformed method is invoked.

Implementing interfaces

I know what interface is and how to build/use one. Lets take the ActionListener interface for example.
My question is what happens after I click on a button, which class calls the actionPerformed method?what is the process from the part that I click the button to the part that the actionPerformed is executed?
The JButton calls the ActionListener.
Internally, it listens to keyboard and mouse events. When it receives a mouse click or a keypress which means "click the button", it creates an ActionEvent instance, loops through all the ActionListener instances that have been added to itself, and calls each one with the ActionEvent as argument.

Determine which button called an actionPerformed method

I am making a maze program in Java which consists of a grid of MazeButtons which extend JButton and have a field for State (which is the location of the button and some other information about how the maze should work). Another class, MazeFrame, extends JFrame and implements ActionListener. When I construct the GUI in the setup class, I add the MazeFrame ActionListener to each button. I want the actionPerformed method in MazeFrame to be able to check to see if the action the user attempted is allowed, but in order to do that I need to know which button was clicked.
How can you know which button called a given actionPerformed method?
Use the getSource method on the event and it will return the object that fired it

Categories