简体   繁体   中英

basic radiobutton issue - java

 package pkg411project;

 import javax.swing.ButtonGroup;

 public class CrudMain extends javax.swing.JInternalFrame {

public CrudMain() {
    initComponents();
}

@SuppressWarnings("unchecked")

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
}                                             

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   BankRecordFrame bdFrame = new BankRecordFrame(); 
    bdFrame.setVisible(true);
    this.jDesktopPane2.add(bdFrame); 

}                                        

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane2;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
// End of variables declaration                   

//ButtonGroup group = new ButtonGroup();
//group.


}

This is a simple question I think. I have four radiobuttons and a regular submit button below the radiobuttons. I am trying to lauch a Jframe when the user clicks on the submit button (one of the radioButton is selected at this point) The Jframe is launched depending on the radiobutton selected. How do you put that into code ? any ideas?

In the action listener of the button, check which radio button is checked:

private void jButton1ActionPerformed(ActionEvent evt) {                                         
    if (jRadioButton1.isSelected()) {
        // show frame 1
    }
    else if (jRadioButton2.isSelected()) {
        // show frame 2
    }
    else if (jRadioButton3.isSelected()) {
        // show frame 3
    }
    else if (jRadioButton4.isSelected()) {
        // show frame 4
    }
}                    

You'll want to use a ButtonGroup to control the selection behavior (so only one radio button is selected at a time). Add an ActionListener to the JButton, and inside the listener, retrieve the selected button from your ButtonGroup.

You can do this in multiple ways. I'll try and post one of them out here.

This is not necessarily the best option but it might just work for you. (I didn't test this)

public class CrudMain extends javax.swing.JInternalFrame {

public CrudMain() {
    initRadioButtons();
    initOtherStuff();
}

private JRadioButton[] radioButtons = new JRadioButton[4];
private JRadioButton btn1 = new JRadioButton();
private JRadioButton btn2 = new JRadioButton();
private JRadioButton btn3 = new JRadioButton();
private JRadioButton btn4 = new JRadioButton();
private JButton submitBtn = new JButton("Submit"); 

public void initRadioButtons() {
    radioButtons[0] = btn1;
    radioButtons[1] = btn2;
    radioButtons[2] = btn3;
    radioButtons[3] = btn4;
}

public void initOtherStuff() {
    //add stuff to your frame
    .......
    submitBtn.addActionListener(this)
}

public void actionPerformed(ActionEvent e) {
    for(int i =0; i < radioButtons.length; i++){
        if(radioButtons[i].isSelected()){
            //Open your frame here
            break; //Place break if you only want one radiobutton to be checked.
        } else {
            //This button was not selected
        }
    }
}

So let's take a look at this piece of code. I put all the buttons inside an array so that you can easily loop through the contents to check whether they have been selected. I put an actionListener on the button which will fire when someone clicks on it. When the actionListener fires it will loop through the array. Inside every iteration every buttons state gets checked. If one has been selected it will fire an action.

And that's about it. If you need help with this code please let me know!!

Good luck!

EDIT :

Just noticed that with this method you can't specify an action per radiobutton. That's something you will have to add to this :) good luck!

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