JInternalFrame quesiton

i have a main class that has a jdesktoppane and ihave 3 other classes these ara jinternal freames. now i can add and call my jinternalframes but i want to access from one jinternalframe to oher. when i click a button on jinternalframe it must open another (that is already exist) jinternal frame. you can download source code from www.nugaturkiye.com/program.zip
LVL 4
orhanbabaAsked:
Who is Participating?
 
CEHJCommented:
Try

        // what will i write here
                  JInternalFrame[] frames = this.getDesktopPane().getAllFrames();
                  frames[1].setLocation(getLocation().x + getWidth(), getLocation().y + getHeight());
                  frames[1].setVisible(true);
0
 
CEHJCommented:
You should probably keep a collection of JIF in another class. Have the JIF making a request call that class and get it from the collection
0
 
TimYatesCommented:
frame.getDesktopPane()

will get you the JDesktopPane they are in

then you can call:

getAllFrames()

to get all the InternalFrames, etc...

Or, just store a reference to all your JInternalFrames in your main class, and access them like normal...

Can you post the relevant code here?  Off-site linking to code isn't 100% within the rules ;-)

Tim
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
TimYatesCommented:
hehehe...  and again with the same time posting ;-)
0
 
orhanbabaAuthor Commented:
while i am testing your comments can you post a link or sth about JInternalFrame
0
 
orhanbabaAuthor Commented:
main class

public class test extends javax.swing.JFrame {
   
    /** Creates new form test */
    public test() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jd = new javax.swing.JDesktopPane();
        jf = new javax.swing.JInternalFrame();
        btnCallInternal = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jf.setClosable(true);
        jf.setVisible(true);
        btnCallInternal.setText("jButton1");
        btnCallInternal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCallInternalActionPerformed(evt);
            }
        });

        jf.getContentPane().add(btnCallInternal, java.awt.BorderLayout.CENTER);

        jf.setBounds(110, 30, 130, 140);
        jd.add(jf, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jd, java.awt.BorderLayout.CENTER);

        pack();
    }

    private void btnCallInternalActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    public static void main(String args[]) {
        new test().show();
    }
   
    // Variables declaration - do not modify
    private javax.swing.JButton btnCallInternal;
    private javax.swing.JDesktopPane jd;
    private javax.swing.JInternalFrame jf;
    // End of variables declaration
   
}

--------external JInternalFrame :) --------
/*
 * InternalFrame2.java
 *
 * Created on 26 Nisan 2005 Salı, 17:24
 */

/**
 *
 * @author  orhan
 */
public class InternalFrame2 extends javax.swing.JInternalFrame {
   
    /** Creates new form InternalFrame2 */
    public InternalFrame2() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        ok = new javax.swing.JButton();

        ok.setText("jButton1");
        getContentPane().add(ok, java.awt.BorderLayout.CENTER);

        pack();
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JButton ok;
    // End of variables declaration
   
}

now i want to call jinternalframe2 when i clicked the buton on main class
0
 
CEHJCommented:
// TODO add your handling code here:
jd.add(new InternalFrame2(), javax.swing.JLayeredPane.DEFAULT_LAYER);
0
 
orhanbabaAuthor Commented:
not working
0
 
CEHJCommented:
InternalFrame2 newFrame = new InternalFrame2();
jd.add(newFrame, javax.swing.JLayeredPane.DEFAULT_LAYER);
newFrame.setVisible(true);
0
 
objectsCommented:
> InternalFrame2 newFrame = new InternalFrame2();

Thats creating a new frame, which is not what is required.
0
 
orhanbabaAuthor Commented:
!!!!!!!!emergency situation!!!!!!!!!!!!!!!!!!!!!
i think there is a misunderstanding here.
i think couldnt tell the whole story correctly.
I want to call second internal frame
when i clicked the button in jifOne i want to see
jifTwo.  can you fix the code
Sample source is here

/*
 * MainForm.java
 */
public class MainForm extends javax.swing.JFrame {
   
    /** Creates new form MainForm */
    public MainForm() {
        initComponents();
        loadFrames();
    }
    jifOne j1=new jifOne();
    jifTwo j2=new jifTwo();
    private void loadFrames(){
        jd.add(j1);
        jd.add(j2);
    }
    private void initComponents() {
        jd = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuOne = new javax.swing.JMenuItem();
        mnuTwo = new javax.swing.JMenuItem();

        getContentPane().setLayout(new java.awt.GridLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        getContentPane().add(jd);

        jMenu1.setText("Menu");
        mnuOne.setText("Show first frame");
        mnuOne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuOneActionPerformed(evt);
            }
        });

        jMenu1.add(mnuOne);

        mnuTwo.setText("Show second frame");
        mnuTwo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuTwoActionPerformed(evt);
            }
        });

        jMenu1.add(mnuTwo);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }

    private void mnuTwoActionPerformed(java.awt.event.ActionEvent evt) {
    j2.show();
    }

    private void mnuOneActionPerformed(java.awt.event.ActionEvent evt) {
     j1.show();
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new MainForm().show();
    }
   
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JDesktopPane jd;
    private javax.swing.JMenuItem mnuOne;
    private javax.swing.JMenuItem mnuTwo;
    // End of variables declaration
   
}
//now jifOne source
import javax.swing.*;

public class jifOne extends javax.swing.JInternalFrame {
   
    /** Creates new form jifOne */
    public jifOne() {
        initComponents();
    }
   
   
    private void initComponents() {
        jbShowSecond = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        jbShowSecond.setText("show second");
        jbShowSecond.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbShowSecondActionPerformed(evt);
            }
        });

        getContentPane().add(jbShowSecond, java.awt.BorderLayout.CENTER);

        pack();
    }

    private void jbShowSecondActionPerformed(java.awt.event.ActionEvent evt) {
        // what will i write here
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JButton jbShowSecond;
    // End of variables declaration
   
}
//jifTwo source
/*
 * jifTwo.java
 */
public class jifTwo extends javax.swing.JInternalFrame {
   
    /** Creates new form jifTwo */
    public jifTwo() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        jLabel1.setText("second frame");
        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        pack();
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
   
}
0
 
orhanbabaAuthor Commented:
sth wrong this code you post CEHJ if you have 3 more jinternalframe when one or two of them open you cant find it index is changing . and i cant give parameter its construct with the code you post
0
 
objectsCommented:
I'd suggest using an inner class for your action listner and have it store a reference to particular frame that it is dealing with.
0
 
orhanbabaAuthor Commented:
i will try another way to fix it. and i think best answer is CEHJ's post. Thank you all
0
 
CEHJCommented:
:-)

I would revert to my original comment. You can keep a Map of JIFs and then get the frames by name from the Map
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.