0

I am designing an elevator for my software engineering class. I am using toggle buttons on the elevator cars inside panel for floor selection. I cannot figure out how to toggle the button off if the current floor equals the button being toggled. I have included a code snippet.

package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

public class test {

public JPanel createContentPane() {

    // We create a bottom JPanel to place everything on.
    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    // Creation of a Panel to contain the buttons
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setLocation(10, 80);
    buttonPanel.setSize(500, 500);
    totalGUI.add(buttonPanel);

    final JToggleButton floor3 = new JToggleButton("3");
    floor3.setSize(50, 50);
    floor3.setLocation(68, 100);
    buttonPanel.add(floor3);
    floor3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (3 == 3) {
                AbstractButton abstractButton = (AbstractButton) e
                        .getSource();
                boolean selected = abstractButton.getModel().isSelected();
                System.out.println("Action - selected=" + selected + "\n");
                floor3.setSelected(!selected);
                // code to toggle the third floor button off
            }

        }
    });
    totalGUI.setOpaque(true);
    return totalGUI;
}

static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Elevator");

    // Create and set up the content pane.
    test Welp = new test();
    frame.setContentPane(Welp.createContentPane());

    frame.setSize(250, 500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}
user1184034
  • 71
  • 3
  • 11
  • You want to disable the button or return it back to an "unselected" state? – MadProgrammer Oct 27 '14 at 00:00
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Oct 27 '14 at 00:02
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Oct 27 '14 at 00:02
  • @MadProgrammer I want to return the button to the "unselected" state as well as the panel to reflect that change. – user1184034 Oct 27 '14 at 00:13
  • `floor3.setSelected(false)` should do it – MadProgrammer Oct 27 '14 at 00:14
  • @MadProgrammer I used that line of code prior to posting. That code does not repaint/update the button if the frame. – user1184034 Oct 27 '14 at 00:21
  • Runnable example, without it, not much more that could be done... – MadProgrammer Oct 27 '14 at 00:22
  • Seems to work for me (or I'm missing something) - I press the button and it's state resets to "unselected"... – MadProgrammer Oct 27 '14 at 00:48

0 Answers0