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

    Default Only one Jframe window will be opened at time

    Hi All,
    I would like to get acquinted with mysteries of the inheritance of the OOP. So sorry for my rookie question but I am confused with these mysteries:confused:

    I try to write a swing application which has got a main Jframe object (animal) , and there will be 2 interframes (car and dog) in the main frame.I add menu (animal) and menuitem (cat, dog) to my main frame.

    Then I start to "design" my hierarchy, but my code was quite bizarre:rolleyes:
    Essentially I have a base class which contains properties of the JFrame objects (setvisible, setDefaultCloseOperation etc.), and I have 2 subclass which are derived from the base class.The first subclass includes JInternalFrame properties, and the second contains the main function.

    Now I am starting the program from the second subclass , and if I click the (dog) menuitem , i would like to open the (dog) internal frame only. But my (dog) internalframe opens with an another main frame together. So there are 2 opened windows on my screen.

    I would like to achieve that if one JFrame is already opened other JFrame is not allow to open.

    I try to simplify my code I hope you can understand my problem:)

    I would be very helpful for your help.

    Here is my base class, with the main Jframe settings:
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JMenuBar;
    import javax.swing.JDesktopPane;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class AnimalFrame extends JFrame implements ActionListener{
    	public AnimalFrame()    
    		{		
    			System.out.println("... animalFrame constructor...");
    			init();			    
    		}	
    	public void init()		
    		{			
    			
    			JMenuBar menubar = new JMenuBar();
    			JMenu animal = new JMenu("animal");
    			JMenuItem dog = new JMenuItem ("dog");
    			menubar.add(animal);			
    			animal.add(dog);			
    			dog.addActionListener(this);
    			setJMenuBar(menubar);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			setSize(400,400);
    			setVisible(true);				
    		}
    	public JDesktopPane getCurrentDesktopPane()		
    		{		   
    			JDesktopPane desktop = new JDesktopPane();
    			getContentPane().add(desktop);
    			return desktop;	
    		}
    	public void actionPerformed(ActionEvent e)		
    		{		   	
    		}	
    }
    the following sub class would be call/open the internalframe:

    Java Code:
    import javax.swing.JInternalFrame;
    
    import java.awt.event.ActionEvent;
    
    public class DogInternalFrame extends AnimalFrame{
    		JInternalFrame internal;	
    		public DogInternalFrame()				
    			{	 
    				initJInternalFrame();			
    			}		
    		public void initJInternalFrame()
    		
    			{		 		   
    				internal = new JInternalFrame("dog", true,true,true,true);		   				getCurrentDesktopPane().add(internal);	
    	  		        internal.setSize(200,200);		  
    	  		        internal.setVisible(true);			
    			}
    }
    The following subclass contains the main function:
    Java Code:
    import java.awt.event.ActionEvent;
    
    public class MainDriver extends AnimalFrame 
    {
    
    		public void actionPerformed(ActionEvent e)
    		
    			{		   
    				if (e.getActionCommand().equals("dog"))			
    					{	
    					   new DogInternalFrame();		
    					   System.out.println("dog frame");
    			
    			}			}
    		public static void main(String [] args)		
    			{
    				new MainDriver();		
    			}
    }

  2. #2
    javababa is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    I am editing your DogInternalFrame and MainDriver code just try it............

    DogInternalFrame.java
    Java Code:
    import javax.swing.JInternalFrame;
    
    import java.awt.event.ActionEvent;
    
    public class DogInternalFrame{
    		JInternalFrame internal;	
    		public DogInternalFrame()				
    			{	 
    				initJInternalFrame();			
    			}		
    		public void initJInternalFrame()
    		
    			{		 		   
    				internal = new JInternalFrame("dog", true,true,true,true);
    				//getCurrentDesktopPane().add(internal);	
    	  		        internal.setSize(200,200);		  
    	  		       // internal.setVisible(true);			
    			}
    }
    MainDriver.java
    Java Code:
    import java.awt.event.ActionEvent;
    
    public class MainDriver extends AnimalFrame 
    {
    
    		public void actionPerformed(ActionEvent e)
    		
    			{		   
    				if (e.getActionCommand().equals("dog"))			
    					{	
    					   DogInternalFrame obj=new DogInternalFrame();
    					   this.add(obj.internal);
    					   obj.internal.setVisible(true);
    					   		
    					   System.out.println("dog frame");
    			
    			}			}
    		public static void main(String [] args)		
    			{
    				new MainDriver();		
    			}
    }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    @javababa - Can you comment your code describing what you did and why it does what it does?

  4. #4
    javababa is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    I just remove the "extends AnimalFrame" from the DogInternalFrame class since it is indirectly calling a new JFrame and by the code "getCurrentDesktopPane().add(internal);" loaded the JInternalFrame in that JFrame. But Zamioculcas dont want that. He wants to load the internal frame in the first Jframe.

    Note:-The way I am doing is may not be a good one but it is working.

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

    Default

    Thank you very much javababa, your suggestion is very good for me.
    Now I screwed a bit around my code, and rewrite the this.add(obj.internal); line to the this.getCurrentDesktopPane().add(obj.internal);
    So I add the JInternalFrame to the JDesktopPane. Then I noticed, the closed JInternalFrame window didn't want to reopen on the JDesktopPane. (But it works properly on the JFrame )
    It's quite interesting:)

    Best Regards,

Similar Threads

  1. not getting response from web service within time window
    By pankaj_salwan in forum Advanced Java
    Replies: 0
    Last Post: 09-21-2010, 06:48 AM
  2. JFrame(window) not displaying at all
    By mojisolaowoeye in forum AWT / Swing
    Replies: 1
    Last Post: 07-02-2010, 02:03 PM
  3. Replies: 2
    Last Post: 04-04-2010, 09:57 PM
  4. Creating a time sliding window..urgent
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 04:12 AM
  5. how to check the window has already opened
    By javastuden in forum AWT / Swing
    Replies: 3
    Last Post: 01-29-2010, 08:26 AM

Posting Permissions

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