Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

Allow only one JInternalFrame to open per Instance

Thread view: 
Jeff - 18 Jul 2005 05:39 GMT
Hello,
    I am creating a program where I have differnt JInternalFrame classes
and I would only like 1 frame per class to be displayed at a time.
Example is having a cardfile frame and a sales frame open at the same
time but not be able to create another cardfile frame if one is already
opened. Below is the code to set up a generic JDesktop and one JInternal
frame. If someone knows how to do this thanks in advance.

InternalFrameDemo.java

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;

import java.awt.event.*;
import java.awt.*;

public class InternalFrameDemo extends JFrame {
    JDesktopPane desktop;

    public InternalFrameDemo() {
        super("InternalFrameDemo");

        //Make the big window be indented 50 pixels from each edge
        //of the screen.
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset,
                  screenSize.width - inset*2,
                  screenSize.height-inset*2);

        //Quit this app when the big window closes.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //Set up the GUI.
        desktop = new JDesktopPane(); //a specialized layered pane
        createFrame(); //Create first window
        setContentPane(desktop);
        setJMenuBar(createMenuBar());

        //Make dragging faster:
        desktop.putClientProperty("JDesktopPane.dragMode", "outline");
    }

    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu menu = new JMenu("Document");
        menu.setMnemonic(KeyEvent.VK_D);
        JMenuItem menuItem = new JMenuItem("New");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createFrame();
            }
        });
        menu.add(menuItem);
        menuBar.add(menu);

        return menuBar;
    }

    protected void createFrame() {
        MyInternalFrame frame = new MyInternalFrame();
    frame.setVisible(true); //necessary as of 1.3; OK to use before
        desktop.add(frame);
        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }

    public static void main(String[] args) {
        InternalFrameDemo frame = new InternalFrameDemo();
        frame.setVisible(true);
    }
}

MyInternalFrame.java - allow only 1 MyInternal Frame open per instance
but I will have another class called MyInternalFrame2 and would like it
possible to have one each open per instance.

import javax.swing.JInternalFrame;

import java.awt.event.*;
import java.awt.*;

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

        //...Create the GUI and put it in the window...

        //...Then set the window size or call pack...
        setSize(300,300);

        //Set the window's location.
        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
    }
}
Andrew Thompson - 18 Jul 2005 08:23 GMT
>     I am creating a program where I have differnt JInternalFrame classes
> and I would only like 1 frame per class to be displayed at a time.

Use a Singleton pattern.

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
Not Affiliated With Futurama Brass Knuckle Co.

Vova Reznik - 18 Jul 2005 19:58 GMT
>>    I am creating a program where I have differnt JInternalFrame classes
>>and I would only like 1 frame per class to be displayed at a time.
>
> Use a Singleton pattern.

He wants to have one frame to be displayed, not created.
Thomas Hawtin - 18 Jul 2005 14:41 GMT
> Hello,
>     I am creating a program where I have differnt JInternalFrame classes
[quoted text clipped - 3 lines]
> opened. Below is the code to set up a generic JDesktop and one JInternal
> frame. If someone knows how to do this thanks in advance.

Don't think of them as classes. Think of them as objects. When creating
them keep a reference to the new frame. If it already exists just show
it instead of creating a new one.

> public class InternalFrameDemo extends JFrame {

Avoid inheritance if you don't need it. It makes the code far too
specific. You may also override methods by accident.

>     JDesktopPane desktop;

I'd suggest (almost) always using private for fields.

>         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

To be very picky you should pick the toolkit off the frame, not get the
default one. In fact the frame may, in general, not be on the default
screen so you should check the frame's graphic enivronment.

>         //Quit this app when the big window closes.
>         addWindowListener(new WindowAdapter() {
>             public void windowClosing(WindowEvent e) {
>                 System.exit(0);
>             }
>         });

You can just set the default close operation.

>         } catch (java.beans.PropertyVetoException e) {}

Always explain why you are discarding an exception. The explanation
might take the form of a comment sent to a logger.

>     public static void main(String[] args) {
>         InternalFrameDemo frame = new InternalFrameDemo();
>         frame.setVisible(true);
>     }

This nees to become:

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() { public void run() {
                    InternalFrameDemo frame = new InternalFrameDemo();
                    frame.setVisible(true);
        }});
    }

Tom Hawtin
Signature

Unemployed English Java programmer



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.