Results 1 to 6 of 6
  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default 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:
    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;
    	}
    }
    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.

    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();
    		} }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Please copy and paste here the full text of the error message.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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);

  4. #4
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    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)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    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.

  6. #6
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    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

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Client freezes when method is called
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 07-26-2010, 09:14 PM
  3. how to prevent open folder in run time
    By syedabu in forum NetBeans
    Replies: 0
    Last Post: 03-18-2010, 04:09 AM
  4. InternalFrame and FocusListener
    By raghuylin in forum AWT / Swing
    Replies: 1
    Last Post: 10-17-2008, 08:11 AM
  5. Replies: 1
    Last Post: 04-24-2008, 06:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •