1

For Example:

When JButton1 click JInternalFrame1 Show on the JDesktopPane And when JButton2 Click JInternalFrame1 Close and JInternalFrame2 Show on the JDesktopPane.

thx before

Edit: with code from comment

if (JInternalFrame1 == null) { 
    JInternalFrame1 = new FJInternalFrame(); 
    Desktop.add(JInternalFrame1); 
    JInternalFrame1.toFront();
} else { 
    JInternalFrame1.dispose();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Schreiner
  • 57
  • 1
  • 1
  • 10
  • Doesn't seem like that difficult of a task. Can you please show the code you've tried? – Paul Samsotha Dec 25 '13 at 16:13
  • if (JInternalFrame1 == null) { JInternalFrame1 = new FJInternalFrame(); Desktop.add(JInternalFrame1); JInternalFrame1.toFront(); } else { JInternalFrame1.dispose();} – Schreiner Dec 25 '13 at 16:16
  • Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. BTW - Why would you close the 1st internal frame as opposed to simply bringing the new internal frame in front of it? Can you explain this to me in a way that would make sense to an end user? It really seems like a single component using a `CardLayout` would work better. – Andrew Thompson Dec 25 '13 at 16:26
  • maybe it's look like singleton method in continuous action, help me please – Schreiner Dec 25 '13 at 16:55
  • I try another way, but not work, can anybody help me? public int Check() { JInternalFrame JIF[] = Desktop.getAllFrames(); if (JIF.length > 0) { JInternalFrame1.dispose(); return 1; } else { return 0; } } – Schreiner Dec 25 '13 at 17:02
  • Tips: 1) Add @peeskillet (or whoever - the `@` is important) to *notify* them of a new comment. 2) Stop putting code in comments, where it is unreadable. Instead [edit the question](http://stackoverflow.com/posts/20774628/edit). 3) *"look like singleton method in continuous action"* Was that intended to answer my question? It doesn't.. – Andrew Thompson Dec 25 '13 at 17:10

1 Answers1

6

Take a look at this example. I created a custom JInternalFrame that has a different title every time you create a new frame. when you click on the button, a new one is created and the old one disapears

Here is the important code that may help you out. I add a new frame if the desktop size is equal to 0, other wise I remove the previous one, add a new frame, and revalidate

   button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (desktop.getAllFrames().length == 0) {
                desktop.add(new MyInternalFrame());

            } else {
                desktop.remove(0);
                desktop.add(new MyInternalFrame());
                revalidate();
                repaint();
            }
        }
    });

Here is the complete code. It's two different files.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class InternalFrameDemo1 extends JPanel {

    JDesktopPane desktop;
    JButton button;

    public InternalFrameDemo1() {
        desktop = new JDesktopPane();
        button = new JButton("Get Next Frame");

        setLayout(new BorderLayout());
        add(desktop, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (desktop.getAllFrames().length == 0) {
                    desktop.add(new MyInternalFrame());

                } else {
                    desktop.remove(0);
                    desktop.add(new MyInternalFrame());
                    revalidate();
                    repaint();
                }
            }
        });
    }

    public static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new InternalFrameDemo1());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

import javax.swing.JInternalFrame;

public class MyInternalFrame extends JInternalFrame {
    static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;

    public MyInternalFrame() {
        super("Document #" + (++openFrameCount),
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

        setSize(300,300);
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
        setVisible(true);
    }
}

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720