0

I have some set of JInternalFrame in a Jpanel. and the JPanel is gridlayout. I need to set only one JInternalFrame to be selected in the container (JPanel). I created the JInternalFrame instance dynamically and add to the panel. I still have the list of JInternalFrame but how to make only one to set selected.

Sign
  • 1,919
  • 18
  • 33
Pratap
  • 11
  • 1
  • 5

1 Answers1

3

As suggested in How to Use Internal Frames, "Usually, you add internal frames to a desktop pane." This allows you to use activateFrame() to indicate that a frame has focus. In this example, a javax.swing.Action is used to select frames from a menu via setSelected(). Additional discussion may be found in this Q&A.

Addendum: If you want to use a JPanel, perhaps to get a nice GridLayout, one aproach is to use an InternalFrameListener as shown below.

enter image description here

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

/** @see https://stackoverflow.com/questions/8389640 */
public class InternalGrid extends JPanel {

    private final List<MyFrame> list = new ArrayList<MyFrame>();

    public InternalGrid() {
        super(new GridLayout(2, 2));
        for (int i = 0; i < 4; i++) {
            MyFrame f = new MyFrame("Frame", i);
            list.add(f);
            this.add(f);
        }
    }

    private void display() {
        JFrame f = new JFrame("InternalGrid");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setSize(320, 240);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new InternalGrid().display();
            }
        });
    }

    class MyFrame extends JInternalFrame {

        MyFrame(String name, int i) {
            super(name + String.valueOf(i), true, true, true, false);
            this.pack();
            this.setVisible(true);
            this.setLayout(new FlowLayout());
            this.add(new JLabel("Hi, I'm " + this.getTitle()));
            this.addInternalFrameListener(new InternalFrameAdapter() {

                @Override
                public void internalFrameActivated(InternalFrameEvent e) {
                    for (MyFrame f : list) {
                        if (f != MyFrame.this) {
                            try {
                                f.setSelected(false);
                            } catch (PropertyVetoException ex) {
                                System.out.println(ex);
                            }
                        }
                    }
                }
            });
        }
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thank u for ur reply but i have a situation to use in a Jpanel here. – Pratap Dec 06 '11 at 03:33
  • 1
    trashgod is right however; either substitute the parent JPanel by a JDesktopPane, or you have a case nobody else uses. You could start placing every JInternalFrame in its own JDesktopPane and on selection change process the previously selected one. – Joop Eggen Dec 06 '11 at 13:25
  • @Joop raises a good point. For example, `iconifiable` becomes meaningless outside of a desktop pane. Code & image updated. – trashgod Dec 06 '11 at 13:35
  • Hi trashgod thanks. If i use JDesktoppane . the Internalframes are draggable. my requirement is the Interframes inside my container should not be dragable or iconalbe. you code make some sense adn i also use the girdlayout in Jpanel, but my Internalframe class will not be local to the Container calss. they are individuals. – Pratap Dec 07 '11 at 06:17
  • hey guys thank u very much helping me. i got the solution by using the custom events. i implement my own event in the JinternalFrame and i listend to those events in the contanier. so i could manage to disable selected the remaining JinternalFrames except the one which rises the event. – Pratap Dec 07 '11 at 12:15