I created this popup window which will show up in response to a button being clicked in my gui. I have two questions regarding this.
- How do I get rid of the text field below the radio buttons?
- I need to check which radio button was selected once the ok button is clicked but I didn't create that button. So how would I implement the actionPerformed function for that?
My code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(evt.getSource() == jButton2)
optionPopup();
}
private void optionPopup(){
JPanel panel = new JPanel();
JRadioButton undergraduateButton = new JRadioButton();
JRadioButton graduateButton = new JRadioButton();
ButtonGroup group = new ButtonGroup();
undergraduateButton.setText("Option A");
graduateButton.setText("Option B");
group.add(undergraduateButton);
group.add(graduateButton);
panel.add(undergraduateButton);
panel.add(graduateButton);
JOptionPane.showInputDialog(panel);
Use
JOptionPane.showMessageDialog
instead ofJOptionPane.showInputDialog
if you still want to have
?
icon instead of!
one, useyou can also remove icon by using
JOptionPane.PLAIN_MESSAGE
If you want to make sure that client pressed OK button use
If
response
will be-1
it means window was closed byX
button, if it was0
user pressedOK
.More info at: https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
undergraduateButton.isSelected()
andgraduateButton.isSelected()
to see if one of them was selected.