3

I tried to access a JInternalFrame in my JDesktopPane and use getAllFrames method.

I just want to access the JInternalFrame in the order that i added into the JDesktopPane. for example, i add a,b,c

frames[0] contain a
frames[1] contain b
frames[2] contain c 

But i found out that the content in the array will change in case that i change my selection. Every time I change my selection. The selected JInternalFrame in the array will move to the top one.

For example , I select b The array will become

frames[0] contain b
frames[1] contain a
frames[2] contain c 

Are there any other ways to get the internal frame in the order i add it into desktoppane??

mKorbel
  • 109,525
  • 20
  • 134
  • 319
code4j
  • 4,208
  • 5
  • 34
  • 51

1 Answers1

1
    package org.app;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;


public class MainFrame extends JFrame{
    private static final long serialVersionUID = 1L;    
    private JDesktopPane theDesktop;
    private List<JInternalFrame> frameList=new ArrayList<>();

public MainFrame()  {
    super("Internal Frame Demo");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800,600);
    this.setJMenuBar(setMenubar());
    theDesktop=new JDesktopPane();
    this.add(theDesktop);
    this.setVisible(true);
}

public JMenuBar setMenubar()    {
    JMenuBar bar=new JMenuBar();
    JMenu addMenu=new JMenu("Add");
    JMenuItem newFrame=new JMenuItem("Internal Frame");
    newFrame.addActionListener(new MenuAction());
    addMenu.add(newFrame);
    bar.add(addMenu);
    return bar;
}

public JInternalFrame addInternalFrame()    {
    JInternalFrame jif=new JInternalFrame("Internal frame",true,true,true,true);
    jif.setSize(new Dimension(240, 300));       
    jif.addInternalFrameListener(new InternalFrameAdapter() {
        @Override
        public void internalFrameClosing(InternalFrameEvent e){             
            frameList.remove(e.getInternalFrame());                 
            System.out.println("from frame closing event");         
        }
    });
    jif.show();
    return jif;
}   
public JInternalFrame getInternalFrame(int index)   {
    return frameList.get(index);
}   
class MenuAction implements ActionListener  {
    @Override
    public void actionPerformed(ActionEvent e) {            
        JInternalFrame f=addInternalFrame();
        theDesktop.add(f);
        frameList.add(f);           
        System.out.println("from menu action");
    }           
}   
public static void main(String[] args){
    new MainFrame();
}

}

Manoj
  • 36
  • 1
  • See also [*Initial Threads*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html), and [*How to Use Actions*](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html). – trashgod May 20 '12 at 19:55