Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't think of it as passing information from one GUI to another, but rather think of it in its most basic OOP form: as passing object state from one object to another. Often we use public accessor methods (i.e., "getter" methods) for this purpose and this can work here too.</p> <p>Your ButtonGroup object will hold the ButtonModel of the selected JRadioButton (or null if none are selected) and so you can get the information from the model and return it from your getter method.</p> <p>As an aside, your code has a lot of redundancies that can be reduced by using arrays and by using appropriate layout managers.</p> <p>edit 1:<br> <strong>For Example</strong><br> Say we create a JPanel that holds a bunch of JRadioButtons:</p> <pre><code>import java.awt.GridLayout; import javax.swing.*; class RadioBtnDialogPanel extends JPanel { private static final String[] BUTTON_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; private ButtonGroup buttonGroup = new ButtonGroup(); public RadioBtnDialogPanel() { setLayout(new GridLayout(0, 1)); // give JPanel a decent layout // create radio buttons, add to button group and to JPanel for (String buttonText : BUTTON_TEXTS) { JRadioButton radioBtn = new JRadioButton(buttonText); radioBtn.setActionCommand(buttonText); // set the actionCommand here buttonGroup.add(radioBtn); add(radioBtn); } } // getter or accessor method to get selected JRadioButton's actionCommand text public String getSelectedButtonText() { ButtonModel model = buttonGroup.getSelection(); if (model == null) { // no radiobutton selected return ""; } else { return model.getActionCommand(); } } } </code></pre> <p>We also give it a public getter method that queries the state of the ButtonGroup to find out which button model has been selected and then return its actionCommand, a String that holds the text that describes the radio button (here it's the same as the text of the radio button).</p> <p>We can then show this JPanel in a JOptionPane in our main GUI and after the JOptionPane is done, query the object above by calling its getSelectedButtonText() method: </p> <pre><code>import java.awt.event.*; import javax.swing.*; public class RadioButtonInfo extends JPanel { private RadioBtnDialogPanel radioBtnDlgPanel = new RadioBtnDialogPanel(); private JTextField textfield = new JTextField(10); public RadioButtonInfo() { JButton getDayOfWeekBtn = new JButton("Get Day Of Week"); getDayOfWeekBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { getDayOfWeekActionPerformed(); } }); textfield.setFocusable(false); add(getDayOfWeekBtn); add(textfield); } private void getDayOfWeekActionPerformed() { // display a JOptionPane that holds the radioBtnDlgPanel int result = JOptionPane.showConfirmDialog(this, radioBtnDlgPanel, "Select Day Of Week", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { // if use presses "OK" get the selected radio button text // here we call the getter method to get the selected button text String selectedButtonText = radioBtnDlgPanel.getSelectedButtonText(); textfield.setText(selectedButtonText); // and put it into a JTextField } } private static void createAndShowUI() { JFrame frame = new JFrame("RadioButtonInfo"); frame.getContentPane().add(new RadioButtonInfo()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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