Reputation: 1423
Basically I have n JButtons. If any of them is clicked, they return a certain number. I have a menu with each of the buttons and when the user clicks one, my menu method returns the number returned by the Button handler. Is it possible?
Something like:
frame.add(button1..)
frame.add(button2..)
frame.add(button3..)
if (button1.isClicked()) {
return button1ActionHandler();
} else if (button2.isClicked()) {
return button2ActionHandler();
} else if (button3.isClicked()) {
return button3ActionHandler();
}
The problem is, the code is not waiting for me to click a button so it won't enter in any of those if's. What can I do for the program to wait for click and how can I check if a button is clicked?
Upvotes: 1
Views: 443
Reputation: 285403
It sounds like you want to present the user with several options, let him choose one of the options, and then have him press a "submit" button to submit that option to the program. If so, then I think that your best bet is to use JRadioButtons, all added to a ButtonGroup -- this allows only one of the radio buttons to be selected at any time, or use a JComboBox. Either way, it would be easy to extract the information regarding which selection the user made. If you use the first option, use of JRadioButtons, ButtonGroup and a "submit" button, you simply get the selected ButtonModel from the ButtonGroup by calling its getSelection()
method, and then extract the actionCommand String from this model by calling getActionCommand()
. If you decide on the second option, use of a JComboBox together with a "submit" button, then simply call getSelectedItem()
on the JComboBox within your submit button's ActionListener.
Below I show you both options. Note that my submit button doesn't use an ActionListener but rather an AbstractAction, which is kind of like an ActionListener on steroids.
import java.awt.event.ActionEvent;
import javax.swing.*;
public class SelectionEg extends JPanel {
private static final String[] SELECTIONS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private ButtonGroup buttonGroup = new ButtonGroup();
private JComboBox<String> selectionComboBox = new JComboBox<>(SELECTIONS);
public SelectionEg() {
for (String selection : SELECTIONS) {
JRadioButton radioButton = new JRadioButton(selection);
radioButton.setActionCommand(selection);
add(radioButton);
buttonGroup.add(radioButton);
}
add(selectionComboBox);
add(new JButton(new SubmitAction("Submit")));
}
private class SubmitAction extends AbstractAction {
public SubmitAction(String name) {
super(name);
putValue(MNEMONIC_KEY, (int) name.charAt(0));
}
@Override
public void actionPerformed(ActionEvent e) {
ButtonModel model = buttonGroup.getSelection();
if (model == null) {
// nothing selected yet, ignore this
return;
}
String message = "The selected radio button is: " + model.getActionCommand();
System.out.println(message);
message = "The selection from the combo box is: " + selectionComboBox.getSelectedItem();
System.out.println(message);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Selelection Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SelectionEg());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 2
Reputation: 347184
Start by having a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners.
Remember, a GUI is an event driven environment, that is, something and then you respond to it.
You need to register an ActionListener
against each button, when the button is triggered, you need to take appropriate action.
There are a number of ways you could achieve this, you could set the actionCommand
of the buttons with appropriate information that you can use to ascertain what should be done when the button is clicked. You could use the source
property of the ActionEvent
to determine the source of the event and take appropriate action, as exampels
Upvotes: 2