javaswingjcheckboxjinternalframe

How to link JCheckBoxMenuItem to JInternalFrames controlled by the checkbox


I have a JFrame which creates a JInternalFrame which in turn creates JInternalFrames inside itself. The 'outer' JIF has an 'Add Frames' button with a checkbox menu so each 'inner' JIF type can only be created once. There may be up to 6 'inner' JIFs (code example restricted to 2, FRAME A & B).

Creating the inner JIFs works fine, BUT when user deselects a checkbox, how do I find the right inner JIF to close? And if user closes an inner JIF, how do I link that back to unchecking the right checkbox?

Methods I've tried end up closing ALL the inner JIFs, or if I try to search the list of open JIFs and match their title to the checkbox field, compiler says the info is not available at this time.

Simplified code for the Outer & Inner JIF creation is as shown. Don't tell me I need a layout manager - the JIFs have to be user-movable and resizeable without restraint.

    class OUTJIF extends JInternalFrame {
OUTJIF() {
    JInternalFrame outerJIF = new JInternalFrame("Outer JInternalFrame", true, true, true, true);
    outerJIF.setBounds(50, 50, 600, 400);  
    outerJIF.getContentPane().setLayout(null);

    JButton btnAddFrames = new JButton("Add Frames");
    btnAddFrames.setBounds(10, 11, 125, 23);
    outerJIF.getContentPane().add(btnAddFrames);

    JPopupMenu popMenu = new JPopupMenu();
    JCheckBoxMenuItem boxFrameA = new JCheckBoxMenuItem("Frame A");
    JCheckBoxMenuItem boxFrameB = new JCheckBoxMenuItem("Frame B");
    popMenu.add(boxFrameA);
    popMenu.add(boxFrameB);
    btnAddFrames.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
               popMenu.show(e.getComponent(), e.getX(), e.getY());
            }
    });
    Demo.mainPane.add(outerJIF);  // add to invoking JFrame
    outerJIF.setVisible(true); 

   // Class for internal JIF 
class intJIF extends JInternalFrame {
intJIF(String intType, int x, int y, int h, int w) {
    JInternalFrame innerJIF = new JInternalFrame(intType, true, true, true, true) ;
    innerJIF.setBounds(new Rectangle(x, y, h, w));
    outerJIF.getContentPane().add(innerJIF);
    innerJIF.setVisible(true);  
    // ISSUE #2 - IF USER CLOSES ONE OF THESE, HOW TO CHANGE CHECKBOX MENU?
    }       
};

    // LISTENERS FOR outerJIF MENU ITEMS 
    ActionListener listFrameA = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton boxFrameA = (AbstractButton) event.getSource();
            boolean selected = boxFrameA.getModel().isSelected();
            if (selected) { new intJIF("Inner Frame A", 0, 100, 250, 250); }
            else {      // ISSUE #1 - HOW TO FIND THE RIGHT INTERNAL JIF TO CLOSE?
                }
            }   };
    boxFrameA.addActionListener(listFrameA);

    ActionListener listFrameB = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton boxFrameB = (AbstractButton) event.getSource();
            boolean selected = boxFrameB.getModel().isSelected();
            if (selected) { new intJIF("Inner Frame B", 50, 50, 250, 250); }
            else {      // ISSUE #1 - HOW TO FIND THE RIGHT INTERNAL JIF TO CLOSE?
                }
            }   };
    boxFrameB.addActionListener(listFrameB);
    }
}

Solution

  • I define GUI components as class members when I need to add listeners to them. So if I make the JInternalFrame and the JCheckBox class members, then in the ActionListener of the JCheckBox I can close the JInternalFrame. Similarly I can add an InternalFrameListener to the JInternalFrame and update the JCheckBox in the internalFrameClosing() method. It appears - from the code you posted - that you are unfamiliar with InternalFrameListener. If that is the case, then I suggest reading How to Write an Internal Frame Listener. If you would like more specific help, then for me, you need to post more of your code so that I can download it and run it.