Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think if you are asking how to stop the action even once you hit the condition, all you need to do is add a return statement to your code. For example, if button 1 is pushed, display message, then add a return statement, that will result in the code exiting that method and not getting to the other if statement.</p> <p>Maybe you want to use something like a Hash, or List to store your choices and each time a choice is made, add it to the List. Then you can always find the last choice made by getting our last Element of the list. I'm not real clear on what you are trying to make here, but maybe that will give you some ideas.</p> <hr> <p>I wrote a small program here that has 2 radio buttons and a submit. When you click submit it updates the text in the window based on the radio button selection.</p> <pre><code>import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; public class MyWindow extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MyWindow frame = new MyWindow(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MyWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); final JRadioButton rdbtnNewRadioButton = new JRadioButton("Choice 1"); rdbtnNewRadioButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); rdbtnNewRadioButton.setSelected(true); rdbtnNewRadioButton.setBounds(5, 7, 424, 23); contentPane.add(rdbtnNewRadioButton); JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Choice 2"); rdbtnNewRadioButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); rdbtnNewRadioButton_1.setBounds(5, 33, 109, 23); contentPane.add(rdbtnNewRadioButton_1); ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(rdbtnNewRadioButton); buttonGroup.add(rdbtnNewRadioButton_1); final JTextArea textArea = new JTextArea(); textArea.setBounds(57, 80, 321, 94); contentPane.add(textArea); JButton btnNewButton = new JButton("Submit"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (rdbtnNewRadioButton.isSelected()) { textArea.setText("Choice 1 Selected."); } else { textArea.setText("Choice 2 Selected."); } } }); btnNewButton.setBounds(135, 212, 89, 23); contentPane.add(btnNewButton); } } </code></pre> <hr> <p>Check out the Java Tutorials on using buttons. Specifically look at the How to use Radio Buttons section to see if that sheds any light on how you would design it.</p> <p><a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html" rel="nofollow">How to use Radio Buttons</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload