Reputation: 62
I have a JDialog with two JTextFields, one ButtonGroup with two RadioButtons and an OK-Button. The button has to be disabled until the TextFields are filled and at least one of the RadioButtons clicked. I'm not sure how to do this.
It works for with the JTextFields using this code:
public class Test {
public static void main(String... args) {
ButtonTest.show();
}
}
class ButtonTest {
private ButtonTest() {
JFrame frame = new JFrame("Button Test");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
JPanel mainPanel = new JPanel(new GridLayout(4, 1));
JTextField field1 = new JTextField(20);
JTextField field2 = new JTextField(20);
JLabel text = new JLabel();
JButton printButton = new JButton("Print");
printButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
text.setText(field1.getText() + " - " + field2.getText());
}
});
printButton.setEnabled(!field1.getText().isEmpty() && !field2.getText().isEmpty());
for (JComponent c : Arrays.asList(field1, field2, text, printButton)) {
mainPanel.add(c);
}
setDocumentListener(field1, field2, printButton);
setDocumentListener(field2, field1, printButton);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
private void setDocumentListener(JTextField field, JTextField other, JButton button) {
field.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
changed();
}
@Override
public void insertUpdate(DocumentEvent e) {
changed();
}
@Override
public void changedUpdate(DocumentEvent e) {
changed();
}
private void changed() {
setButtonStatus(button, field.getText(), other.getText());
}
});
}
private void setButtonStatus(JButton button, String field1, String field2) {
button.setEnabled(!field1.isEmpty() && !field2.isEmpty());
}
public static void show() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ButtonTest();
}
});
}
}
But what about the RadioButtons? I guess I have to somehow implement an ItemListener?
Greetings
Upvotes: 1
Views: 3016
Reputation: 324197
Check out: Validation of text fields and contact no text field
It provides a general solution for enabling a button when data is entered in all text fields.
You can enhance that solution to also support radio buttons.
But what about the RadioButtons? I guess I have to somehow implement an ItemListener?
You can modify the DataEntered
class found in the link above to do the following:
isDataEntered()
method.addButtonGroup(...)
method. This method would save the ButtonGroup
and then iterate through all the radio buttons in the group to add the ItemListener
to the radio button.isDataEntered()
method to iterate through each ButtonGroup
and invoke the getSelection()
method on the ButtonGroup
. If the value is null
that means no radio button has been selected and you just return false
.Upvotes: 1
Reputation: 285430
You need two tests:
!textField.getText().trim().isEmpty()
, andbuttonGroup.getSelection() != null
Combined, it could look something like:
private void testToActivateButton() {
boolean value = !textField.getText().trim().isEmpty() && buttonGroup.getSelection() != null;
submitButton.setEnabled(value);
}
Then simply call the above method in ActionListeners added to the JRadioButtons and your JTextField's DocumentListener. For example:
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class ActivateButton extends JPanel {
private static final String[] RADIO_TEXTS = {"Hello", "Goodbye"};
private ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField = new JTextField(10);
private JButton submitButton = new JButton("Submit");
public ActivateButton() {
textField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
testToActivateButton();
}
@Override
public void insertUpdate(DocumentEvent e) {
testToActivateButton();
}
@Override
public void changedUpdate(DocumentEvent e) {
testToActivateButton();
}
});
for (String radioText : RADIO_TEXTS) {
JRadioButton radioButton = new JRadioButton(radioText);
radioButton.addActionListener(e -> testToActivateButton());
buttonGroup.add(radioButton);
add(radioButton);
}
submitButton.setEnabled(false);
add(textField);
add(submitButton);
}
private void testToActivateButton() {
boolean value = !textField.getText().trim().isEmpty() && buttonGroup.getSelection() != null;
submitButton.setEnabled(value);
}
private static void createAndShowGui() {
ActivateButton mainPanel = new ActivateButton();
JFrame frame = new JFrame("ActivateButton");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Upvotes: 2
Reputation: 6872
You can use isSelected()
to check whether the radio button is selected.
Eg:
printButton.setEnabled(!field1.getText().isEmpty() && !field2.getText().isEmpty() && (radioBtn1.isSelected() || radioBtn2.isSelected()) );
Upvotes: 1