1

I have an array of JTogglebuttons and a JButton, when the JButton is pressed it initiates a loop that iterates through all the JTogglebuttons to see if it is pressed, and if it is it should make it disable/unclickable

Here is the snippet of code that is an issue

Public void actionPerformed(ActionEvent e){
        Object pressed = e.getSource(); 
        if (pressed == btnPurchase) {
            for(int row = 0; row<8 ; row++){
                for(int column = 0; column < 4; column++) {     
                    if (seat[row][column].isPressed()) {
                        seat[row][column].setEnabled(false);
                    }
                }
            }
        }

I get this error

cannot find symbol
symbol  : method isPressed()
location: class javax.swing.JToggleButton
                    if (seat[row][column].isPressed()) {

If i use .isSelected

i get

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at cinemaManager.actionPerformed(cinemaManager.java:174)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6375)
h00j
  • 308
  • 1
  • 7
  • 18
  • 2
    Look for methods with 'selected' in the name, in the docs. for [`JToggleButton`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JToggleButton.html). – Andrew Thompson Apr 18 '12 at 14:18

2 Answers2

2

look for ButtonModel, thats returns expected method, including isSelected

import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JToggleButtonChangeListener {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Selecting Toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JToggleButton toggleButton = new JToggleButton("Selecting Toggle / Deselacting Toggle");
        toggleButton.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel buttonModel = toggleButton.getModel();
                boolean armed = buttonModel.isArmed();
                boolean pressed = buttonModel.isPressed();
                boolean selected = buttonModel.isSelected();
                System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
            }
        });
        frame.add(toggleButton);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }
}

notice: change of Backgroung not working for all of Look and Feels

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    There's no need to go into the button model. See [this](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#isSelected%28%29). – user1329572 Apr 18 '12 at 14:27
  • What do you mean? How do i use it? I tried .isSelected it compiles but nothing happens. – h00j Apr 18 '12 at 14:45
  • @Henry Hoggard please see my edit, you have to accept the JToggleButton is more close to the JCheckBox / JRadioButton as to the plaint JButton – mKorbel Apr 18 '12 at 16:55
  • @user1329572 I saw that, but I preffering ButtonModel, including part of custom model that covering all JButtonComponents inc JMenuItem ... – mKorbel Apr 18 '12 at 17:01
  • @Henry Hoggard did you tried code that I posted here, I can see there ActionListener, nothing about ButtonModel, ItemListener again JToggleButton is about JCheckBox / JRadioButton not JButton, check Swing tutorial – mKorbel Apr 19 '12 at 09:37
0

I found that i redefined the array at the wrong position coursing it to lost the existing elements. Fixed now thanks

h00j
  • 308
  • 1
  • 7
  • 18