my internalframe freezes with an IllegalArgumentException for its second time open
My "singleton typed" internalframe freezes with an IllegalArgumentException for its second time open
Hi everybody,
I created a singleton window (I try to use the Singleton pattern), which extends a JInternalFrame. Subsequently I add it within a JFrame. If I open the internalframe for second time I would like to get the focus to the already opened JInternalFrame.
But now I get an IllegalArumentException and my internalframe freezes.
What is wrong my code?
Might I made some mistakes, in my singleton class?
I would be very grateful if someone could help me!
Thanks in advance.
the following code contains my singleton class:
Code:
import javax.swing.*;
public class SingletonDemo extends JInternalFrame{
private SingletonDemo()
{
super("ff",true,true,true,true);
setSize(100,100);
setTitle("Singleton");
setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
}
private static SingletonDemo myInstance;
public static SingletonDemo getInstance () throws IllegalArgumentException
{
if (myInstance == null)
{
myInstance = new SingletonDemo();
}
return myInstance;
}
}
here is my class which contains the main frame with the menus:
You try to click the "singleton frame" menuitem again, when the internalframe window is already opened.
Code:
import javax.swing.*;
import java.awt.event.*;
public class MyFrame extends JFrame implements ActionListener{
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("menu");
JMenuItem singleton = new JMenuItem("singleton frame");
final JDesktopPane desk = new JDesktopPane();
public MyFrame()
{
setSize(350,350);
setTitle("Singleton");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(menubar);
menubar.add(menu);
menu.add(singleton);
singleton.addActionListener(this);
getContentPane().add(desk);
setVisible(true);
}
public void actionPerformed(ActionEvent e) throws IllegalArgumentException
{
try
{
if (e.getActionCommand().equals("singleton frame"))
{
System.out.println("open singleton frame");
SingletonDemo sing = SingletonDemo.getInstance();
desk.add(sing);
sing.setVisible(true);
}
}
catch(IllegalArgumentException iae)
{
System.out.println("illegalargumentexception...");
iae.printStackTrace();
}
}
public static void main(String[] args)
{
new MyFrame();
} }