1

I am currently trying to implement an action listener to my JCheckBox so that when it is selected, it will open a JFileChooser for the user to pick a file they want the GUI to use. For starters how would I get the console to print out "Box clicked!" when a user checks the box?

It has been a while since I've programmed in Swing so any advice helps!

public class RadioPanel extends JPanel implements ActionListener
{

    private static final long serialVersionUID = -1890379016551779953L;
    private JCheckBox box;
    private JLabel label;

public RadioPanel(String message)
{
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.gridx = 0;
    c.gridy = 0;
    box =  new JCheckBox();
    this.add(box,c);
    c.gridx = 1;
    c.gridy = 0;
    label = new JLabel(message);
    this.add(label, c);
}
  • Inside of the ActionListener, you can open your dialog. https://stackoverflow.com/questions/11316778/action-listener-on-a-radio-button – BMasonJ13 Feb 14 '19 at 16:03
  • *"It has been a while since I've programmed in Swing so any advice helps!"* My advice is to ask a question. – Andrew Thompson Feb 14 '19 at 16:07
  • `It has been a while since I've programmed in Swing so any advice helps!` - start by reading the [Swing tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for Swing basics. There are working example on how to use a check box and add a listener to it. – camickr Feb 14 '19 at 16:52

2 Answers2

1
ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
        boolean selected = abstractButton.getModel().isSelected();
        System.out.println("Is selected :" + selected);
      }
    };
box.addActionListener(actionListener);
nullPointer
  • 4,419
  • 1
  • 15
  • 27
1

I think it's because the code does not have an event listener. See my code below.

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


class RadioPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = -1890379016551779953L;
    private JCheckBox box;
    private JLabel label;

    public RadioPanel(String message) {
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.WEST;
        c.gridx = 0;
        c.gridy = 0;
        box = new JCheckBox();

        // here
        box.addActionListener(event -> {
            JCheckBox checkBox = (JCheckBox) event.getSource();
            if (checkBox.isSelected()) {
                System.out.println("Box clicked!");
            }
        });

        this.add(box, c);
        c.gridx = 1;
        c.gridy = 0;
        label = new JLabel(message);
        this.add(label, c);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }
}
Madplay
  • 1,027
  • 1
  • 13
  • 25