Results 1 to 6 of 6
- 07-23-2011, 10:55 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
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:
here is my class which contains the main frame with the menus:Java 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; } }
You try to click the "singleton frame" menuitem again, when the internalframe window is already opened.
Java 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(); } }
- 07-24-2011, 12:19 AM #2
Please copy and paste here the full text of the error message.
- 07-24-2011, 12:27 AM #3
For a test try adding sing twice. You'll get the same error quicker:
Java Code:desk.add(sing); desk.add(sing); // For a test add it again - to demo the error!! sing.setVisible(true);
- 07-24-2011, 12:49 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
Hi Norm,
thanks your promp reflection. I inserted a new line to my code, that you mentioned before.
And now I get a same exception message at the first try.
here is my full exception message, I hope this helps:
java.lang.IllegalArgumentException: illegal component position
at java.awt.Container.addImpl(Unknown Source)
at javax.swing.JLayeredPane.addImpl(Unknown Source)
at javax.swing.JDesktopPane.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MyFrame.actionPerformed(MyFrame.java:28)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unk nown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(Unknown
Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 07-24-2011, 01:23 AM #5
What my simple test demonstrates, I think, is that you can NOT add the same instance of a component to a container more than once.
Your design is flawed.
- 07-25-2011, 08:26 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
Thanks for your quick response again. I investigated a bit more what you demonstrated in your comments.
I took into account what you mentioned, so we can't add the instance of the component to the container more than once, thanks to my homebrew singleton class.
I scratched my head and It took me a long time but I have solved the problem:)
I placed into the problematic line "desk.add(sing);" into a IF condition. If the internalframe is invisible, it would be added to the container.
So I avoid adding an instance of component to a container more than once.
I declared my singleton objects outside the function:
SingletonDemo sing = SingletonDemo.getInstance();
And here is my code snippet, which contains the my solution:
Java Code:public void actionPerformed(ActionEvent e) throws IllegalArgumentException { try { if (e.getActionCommand().equals("singleton frame")) { if (sing.isVisible()) { System.out.println("frame is already opened..."); } else { System.out.println("open singleton frame"); desk.add(sing); sing.setVisible(true); } } } catch(IllegalArgumentException iae) { System.out.println("illegalargumentexception..."); iae.printStackTrace(); } }
Similar Threads
-
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
Client freezes when method is called
By chyrl in forum Advanced JavaReplies: 9Last Post: 07-26-2010, 09:14 PM -
how to prevent open folder in run time
By syedabu in forum NetBeansReplies: 0Last Post: 03-18-2010, 04:09 AM -
InternalFrame and FocusListener
By raghuylin in forum AWT / SwingReplies: 1Last Post: 10-17-2008, 08:11 AM -
How do I modify a specific component within a active internalframe?
By LearningJavaASAP in forum AWT / SwingReplies: 1Last Post: 04-24-2008, 06:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks