I'm trying to create a simple GUI with a menu bar at the top of the jwindow using java. I'm able to get the menu bar to show and I'm able to implement the actionlistener class, however, how do I get the ok and ye tab, once clicked, to print out information? In other words how do I use the actionlistener class to create an action to print information out once ok or ye has been clicked by the user?
// Creates a menu bar
z.setJMenuBar(b);
File.add(ok);
File.add(ye);
Help.add(laws);
bar.add(HELP);
bar.add(FILE);
// Listener objects created
ok.addActionListener(this);
ye.addActionListener(this);
if you are adding this like action listener the you should implement abstract method actionPerformed. You can do everything inside that method.
Related
Pretty simply, how and where do I create the ActionListener and action for a button if I created that button in the GUI builder (meaning there is no code in the main java class for it).
All you have to do is implement the action to the ActionListener interface.
yourButton.addActionListener(e -> System.out.println("button pressed"));
For the old GUI builder there is this.
In the new GUI builder just select the button then select the component properties, select events and click the action event. Then you can just bind it in GUI as such:
I have a class that extends JPanel and implements MouseListener. In this class, if the panel is clicked the following two functions are executed:
#Override
public void mouseClicked(MouseEvent e) {
displayExitPopup();
}
private void displayExitPopup() {
JPopupMenu exitPopup = new JPopupMenu();
exitPopup.add("Exit Game");
exitPopup.add("Cancel");
exitPopup.show(this, this.getWidth(), this.getHeight());
}
This all works fine and the popup displays the 2 options as it should.
Now I am trying to perform actions when either of the two options in the popup menu are clicked - System.exit(0) if Exit Game is clicked, and the popup menu to close if Cancel is clicked. How can this be done?
If you take a look at the JavaDocs for JPopupMenu#add(String), you will find that it's a convenience method which returns a JMenuItem, you should then be able to add an ActionListener to it
Take a look at Bringing up a PopupMenu for more details
Having said that, I would instead, encourage you to make use the Action API which will allow you to create self contained units of work, which also provide the information needed to create the JMenuItem used by the JPopupMenu
Have a look at How to Use Actions for more details
You should also be using JComponent#setComponentPopupMenu instead of trying to use a MouseListener, as different platforms have different triggers for a popup menu and it's complicated and ugly real quick
When adding a item to JPopupMenu, you get a JMenuItem. On this object, you can call addActionListener to add a action listener, like you have with a JTextField or a JButton.
exitPopup.add("Cancel").addActionListener(e-> {
// do something
});
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.
I have 3 conditions (3 menu items in menu). In those 3 items, how to get one menu item selected as soon as the frame opens and displays the content of that item by default?
Later on if we select other menu items, then corresponding contents on the frame will be displayed as usual. I have written very lengthy code for this GUI, so unable to paste here. But got stuck up at this point.
Simply invoke the actionPerformed() method of your ActionListener after your GUI is fully constructed. This is particularly easy if you have implemented the Action interface, as shown in How to Use Actions.
Using this example, add the following line near setVisible() to simulate adding a few random nodes to the graph:
gp.control.random.actionPerformed(new ActionEvent(gp, 0, null));
To simulate clicking a button, this line simulates adding a selected node:
gp.control.defaultButton.doClick();
By default all menu items are not selected when they are created. To make a menu item selected before you show it in your application you should change the state of the model. For the JMenu items it's easy by setSelected(true) and setPopupMenuVisible(true). For the JMenuItem items you have to setArmed(true). You can return back to the default state in the actionPerformed.
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