1

User selects either the Admin radio button or the User radio button (2 of which are contained in a group). If admin radio button is selected the user is brought to the adminDashboard, else the user is brought to the userDashboard.

I have been looking for code to validate which radio button is selected although I can't find any correct java code

my current button and listener method code (trying to use isSelected())

    JButton btnEnter = new JButton("Enter");
    btnEnter.addMouseListener(new MouseAdapter() 
    {
        public void mouseClicked(MouseEvent e) 
        {
            if(rdbtnAdminAccount.isSelected())
            {
                AdminDashboard admin = new AdminDashboard();
            }
            else if (rdbtnNormalAccount.isSelected())
            {
                UserDashboard userd = new UserDashboard();
            }

        }
    });

radio button and group code:

    JRadioButton rdbtnNormalAccount = new JRadioButton("Normal Account");
    rdbtnNormalAccount.setBounds(361, 190, 109, 23);
    contentPane.add(rdbtnNormalAccount);

    JRadioButton rdbtnAdminAccount = new JRadioButton("Admin Account");
    rdbtnAdminAccount.setBounds(361, 213, 109, 23);
    contentPane.add(rdbtnAdminAccount);

    accountGroup.add(rdbtnNormalAccount);
    accountGroup.add(rdbtnAdminAccount);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) `rdbtnNormalAccount.setBounds(361, 190, 109, 23);` Use layouts (layout padding, borders etc.) for a robust GUI! – Andrew Thompson May 06 '13 at 03:59
  • It's very confusing for you to mention finding "correct" jQuery code in a question about Java. jQuery is a JavaScript library (JavaScript is unrelated to Java). – Nathaniel Waisbrot May 06 '13 at 04:01
  • 2
    Don't use a `MouseListener` on `JButton`, use an `ActionListener` – MadProgrammer May 06 '13 at 04:05
  • Read the section from the Swing tutorial on [How to Use Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – camickr May 06 '13 at 04:07
  • I agree with @MadProgrammer. Using MouseListener is mainly only for things like JPopupMenu, clicking/dragging/double clicking/mouse actions dealing with the screen. The RadioButton is best used with ActionListener, as is all other forms of buttons. – user2277872 May 06 '13 at 04:09
  • thanks for the input regarding ActionListener and buttons. My code is working at the minute so I will go through it later and change MouseListener sections for best practice – Craig Hogan May 06 '13 at 04:19

1 Answers1

2

You Could...

Add all your buttons into a List of some kind and simply iterate over the list to find the selected button...but then you need some way to determine what action to take...

This could be solved by using a Action mapped to the JRadioButton in some kind of Map...

Basically...

for (JRadioButton btn : listOfButtons) {
    if (btn.isSelected()) {
        Action action = mapOfActions.get(btn);
        action.actionPerformed(new ActionEvent(e, ActionEvent.ACTION_PERFORMED, null));
        break;
    }
}

You don't HAVE to use Action, it's just what is readily available..

You Could...

Create a method that takes a variable list of arguments of JRadioButton that simply returns the selected radio button, but you still have to make a decision about which button it is...see first point...

You Could...

Just keep doing what you're doing...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366