Question

I have made a simple user interface with a JDesktopPane and several JButtons. The program works such that when a button is clicked, a JInternalFrame loads up and the button is disabled to prevent copies of the internal frame from being created. A Cancel button on the JInternalFrame closes the frame and the JButton is enabled again. My code is as follows:

adminAddUser addNew = new adminAddUser();
private javax.swing.JButton newUserButton;

private void newUserButtonActionPerformed(java.awt.event.ActionEvent evt)
{                                              
    AdminPane.add(addNew);
    addNew.setVisible(true);
    newUserButton.setEnabled(false);
}   

The Action Listener for the Cancel button on the adminAddUser class. The dispose() method is used to close the JInternalFrame.

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {                                      
    int option;

    option = JOptionPane.showConfirmDialog(rootPane, "Cancel?", "", JOptionPane.YES_NO_OPTION);

    switch(option)
    {
        case (JOptionPane.YES_OPTION):
            dispose();
            break;

        case (JOptionPane.NO_OPTION):
            break;

        default:
            break;
    }
}                        

And finally, my handling code for re-enabling the JButton when the internal frame is closed.

private void AdminPaneComponentRemoved(java.awt.event.ContainerEvent evt) {                                           
    if(evt.getChild() == addNew)
    {
        newUserButton.setEnabled(true);
    }
}      

The button re-enables fine when the default close button on the JInternalFrame is clicked, but the Cancel button works once, and upon trying to use it a second time, the JInternalFrame closes, but the JButton does not re-enable. How do I re-enable it?

Any help would be greatly appreciated, as I'm new to GUI coding for Java.

Was it helpful?

Solution

Here's a possible solution.

  • Create an interface (I only do this, as no not expose the main JFrame). Have the JFrame form class implement it. It will only have one method, getNewUserButtont();

    public interface GetButtonInterface {
        JButton getUserButton();
    } 
    
    public NewJFrame extends JFrame implements GetButtonInterface {
        private JButton newUserButton;
    
        @Override
        JButton getUserButton() {
            return newUserButton;
        }
    }
    
  • Then in your JInternaFrame class, it should take a GetButtonInterface arg in the constructor. This is so you can get access to the newUserButton

    public AdminAddUser extends JInternalFrame {
        private JButton newUserButton;
    
        public AdminAddUser(GetButtonInterface gbi) {
            newUserButton = gbi.getUserButton();
        }
    }
    
  • Now you can do what you want with that button. Every new AdminAddUser will use the same instance of the newUserButton. So you could 1. Use a InternalFrameListener and override internalFrameClosing() to enable the button when the window is closed. 2. Use the button for the cancelButtonActionPerrfomed

    public AdminAddUser extends JInternalFrame {
        private JButton newUserButton;
    
        public AdminAddUser(GetButtonInterface gbi) {
            newUserButton = gbi.getUserButton();
    
            addInternalFrameListener(new InternalFrameAdapter(){
                @Override
                public void internalFrameClosing(InternalFrameEvent e) {
                    newUserButton.setEnabled(true);
                }
            });
        }
    
        private void cancelButtonActionPerformed(ActionEvent e) {
            dispose();
            newUserButton.setEnabled(true);
        }
    } 
    
  • When you instantiate a new AdminAddUser use pass NewJFrame.this to it. Which is an instance of GetButtonInterface

    AdminAddUser addNew = new AdminAddUser(NewJFrame.this);
    

OTHER TIPS

I don't understand. It seems to me that adminAddUser is created only once. In that case you should not use disposesince the object will be destroyed and could not be reused.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top