Nonstopbrain
Nonstopbrain

Reputation: 57

Button.isSelected() does not return true

I'm creating a JFrame with 3 buttons, 3 checkboxes, and 3 radio buttons. I'm having a problem with a function that is excecuted when 1 of the 3 buttons is pressed. This is the function:

private void setCenterColors() {

    if(redB.isSelected()) {
        center.setBackground(Color.RED);
    } else if (greenB.isSelected()) {
        center.setBackground(Color.GREEN);
    } else if (blueB.isSelected()){
        center.setBackground(Color.BLUE);   
    }
    System.out.println(center.getBackground());
}

redB, greenB, and blueB are all JButtons.

The event handler for the buttons is:

class ChoiceListenerButton implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            setCenterColors();
            repaint();
        }
    }

    listenerButton = new ChoiceListenerButton();

When i excecute the program and press the buttons, all of them returns false, when one of them should return true. What should i do to see which button is pressed? Any help is appreciated, if you need full code, please ask and i will replay as soon as is see the notification. Thank you.

Upvotes: 2

Views: 453

Answers (2)

WJS
WJS

Reputation: 40064

There is nothing wrong doing it the way you are. But you might find this useful in the future.

Since the actionListener is a Functional Interface you can use a lambda to specify the actionListener. In your case an example would be:

        JButton red = new JButton("Set Red");
        red.addActionListener((ae) -> {
            panel.setBackground(Color.red);
            panel.repaint();
        });

For more complicated listeners, you can create a private inner class and pass the necessary information via the constructor. Since it is an inner class, it has access to the instance field panel defined in the outer, containing class.

      private class ButtonListener implements ActionListener {
        private Color color;

        public ButtonListener(Color color) {
            this.color = color;
        }

        public void actionPerformed(ActionEvent ae) {
            panel.setBackground(color);
            panel.repaint();
        }
      }

You would use it as follows:

       button.addActionListener(new ButtonListener(Color.red));

The idea is that you can use multiple action listeners to handle different siutations or requirements. In this case to set the appropriate color.

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79115

Do it as follows:

class ChoiceListenerButton implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        setCenterColors(event);
        repaint();
    }

    private void setCenterColors(ActionEvent event) {
        if(event.getSource() == redB) {
            center.setBackground(Color.RED);
        } else if (event.getSource() == greenB) {
            center.setBackground(Color.GREEN);
        } else if (event.getSource() == blueB){
            center.setBackground(Color.BLUE);   
        }
        System.out.println(center.getBackground());
    }
}

Also, make sure you have added listenerButton to buttons e.g. redB.addActionListener(listenerButton).

Upvotes: 1

Related Questions