user2318083
user2318083

Reputation: 587

JButton toggle disable and enable ItemListener

I'm trying to make a button that will enable or disable another button. I'm not sure why it's not entering the ItemListener or is ItemListener only used for toggle events? Here's my code.

This is in the main method:

        resumePause = new JButton ("Pause/Resume") ;
        leftPanel.add(resumePause) ;
        deleteObj = new JButton ("Delete");
        leftPanel.add(deleteObj);

        //sets delete button to disabled and resume to enable
        resumePause.setEnabled(true);
        deleteObj.setEnabled(false);
        // register the frame as a listener for Item events 
        resumePause.addItemListener(this);

This is the ItemEvent method:

public void itemStateChanged (ItemEvent e) {
        System.out.println("In ItemStateChanged");
        if (playPause.isSelected()) {
            System.out.println("Delete enabled");
            deleteObj.setEnabled(true);
        }
        else{
            System.out.println("Delete disabled");
            deleteObj.setEnabled(false);
        }
    }

I added those println() to check if it ever enters itemStateChanged but it never does.

Upvotes: 1

Views: 475

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Just use a JToggleButton and then use the original listener, an ItemListener on it.

Upvotes: 3

Related Questions