简体   繁体   中英

Have the selected radio button to display in text area

I'm making a program in netbeans that allows users to import their computer specs and I have 2 radio buttons and I want it when you would select the choice and when pressed display it would display your choice in the text area. I already have other text fields which the person can enter information in that get displayed into the text area, but how would I do this for a radio button.

  private void displayActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        textarea.append("Processor: " + processorTextField.getText() + "\nGraphics Card: "
           + graphicsCardTextField.getText() + "\nRam: " + ramTextField.getText() + "\nHard Drive: " + 
                   hardDriveTextField.getText() + "\nOperating System: " + operatingSystemTextField.getText()
                   + "\nMonitor Size: " + monitorTextField.getText());
    }  

this is the code I already have for the other text fields to go into the text area when the display button is pressed

If you've added the JRadioButtons to a ButtonGroup, then the ButtonGroup can give you the ButtonModel from the selected JRadioButton by calling getSelection() on it. And then you can get the model's actionCommand String (which has to be explicitly set for JRadioButtons). For instance, assuming a ButtonGroup called buttonGroup:

private void displayActionPerformed(java.awt.event.ActionEvent evt) { 

    // 1st get the ButtonModel for the selected radio button
    ButtonModel buttonModel = buttonGroup.getSelection();

    // if a selection has been made, then model isn't null
    if (buttonModel != null) {  
        // again actionCommand needs to be set for each JRadioButton
        String actionCommand = buttonModel.getActionCommand();
        // TODO: use actionCommand String as needed
    }
}

For example:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class RadioEg extends JPanel {
   private static final String[] RADIO_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
   private ButtonGroup buttonGroup = new ButtonGroup();

   public RadioEg() {
      JPanel radioPanel = new JPanel(new GridLayout(0, 1));
      for (String radioText : RADIO_TEXTS) {
         JRadioButton radioButton = new JRadioButton(radioText);
         radioButton.setActionCommand(radioText); // set this!
         radioPanel.add(radioButton); // add to JPanel
         buttonGroup.add(radioButton); // add to button group
      }

      JPanel southPanel = new JPanel();
      southPanel.add(new JButton(new AbstractAction("GetSelection") {

         @Override
         public void actionPerformed(ActionEvent e) {
            ButtonModel buttonModel = buttonGroup.getSelection();
            if (buttonModel != null) {
               String actionCommand = buttonModel.getActionCommand();
               System.out.println("Selected Button: " + actionCommand);
            }
         }
      }));

      setLayout(new BorderLayout());
      add(radioPanel, BorderLayout.CENTER);
      add(southPanel, BorderLayout.PAGE_END);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("RadioEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new RadioEg());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM