Results 1 to 8 of 8
  1. #1
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default Switching JPanels inside JFrame attempt

    I am converting an application I developed in C++ Qt4 to Java. I would like for one part of application to do what this toy example is trying to do.

    Java Code:
    /**
     * 
     */
    package practice;
    
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * @author ivan
     *
     */
    public class Start
    {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
    	{
    		MainFrame frame = new MainFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    
    }
    
    class MainFrame extends JFrame
    {
    	public MainFrame()
    	{
    		setSize(300, 200);
    		
    		flag = false;
    		
    		panel1 = new JPanel();
    		label1 = new JLabel("Panel 1");
    		panel1.add(label1);
    		
    		panel2 = new JPanel();
    		label2 = new JLabel("Panel 2");
    		panel2.add(label2);
    		
    		add(panel1);
    		
    		JMenuBar menuBar = new JMenuBar();
    		JMenu menu = new JMenu("Menu");
    		JMenuItem item = new JMenuItem("Change panel");
    		
    		menu.add(item);
    		menuBar.add(menu);
    		setJMenuBar(menuBar);
    		
    		item.addActionListener(new ActionListener()
    		 {
    			public void actionPerformed(ActionEvent event)
    			{
    				//JOptionPane.showMessageDialog(MainFrame.this, "In action listener");
    				
    				if (flag)
    				{
    					flag = false;
    					MainFrame.this.remove(panel2);
    					MainFrame.this.add(panel1);
    					MainFrame.this.repaint();
    				}
    				else
    				{
    					flag = true;
    					MainFrame.this.remove(panel1);
    					MainFrame.this.add(panel2);
    					MainFrame.this.repaint();
    				}
    			}
    		 });
    		
    	}
    	
    	private boolean flag;
    	private JPanel panel1, panel2;
    	private JLabel label1, label2;
    }
    User is suppose to select an option from the menu. Based on his selection central panel of the frame need to be changed for the appropriate one. Here I have two panels which I try to alternate when to user click on the menuItem.

    When I start the program I see the panel1 with its label1. I click the menu item I see the message box indication I am inside Action listener but after I click ok frame is empty. When I click the item in the menu again I get the message box again and after I click ok I see the panel1 with its label1. If I put under comment the line with JOptionPane same thing happens. I start the program, I see the panel1 I click the item I see the empty frame I click the item again I see the panel1.

    I can't get panel2 to show.

    I would appreciate any advice you can give me. Problem is probably trivial but I failed to solve it.

    Second question. Is what I am trying to do considered good programing practice under Java. Or is there a better, smarter way to achieve this goal.

    Thanks in advance.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default

    Thank you. I tried to find a Java equivalent for QScrollView class which did this work for my application developed under Qt4. This seems to be the answer to my problem. I will try both CardLayout and TabbedPane and see which suits my need better.

    Thanks again.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You're welcome.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Another problem "might" be that you aren't suppose to add to JFrame directly (cause basically you have no idea what you're adding to - what's the default layout of JFrame, for example?). You're supposed to do this:

    Java Code:
    JPanel rootPanel = new JPanel ();
    // set it up, add all your components to it
    frame.setContentPane (rootPanel);
    You only call setContentPane() once and don't touch the JFrame anymore; all your additions and removals are now done to rootPanel.
    [/code]

  6. #6
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default

    Thank you for your advice.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Another problem "might" be that you aren't suppose to add to JFrame directly (cause basically you have no idea what you're adding to - what's the default layout of JFrame, for example?).
    There is nothing wrong with doing that. A JFrame always has the same default layout manager.

  8. #8
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default

    After reading JFrame api and tutorial Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) it seems that making some container populating it and then setting that container as the ContentPane is the recommended way. But according to both api and tutorials other ways are also ok.

    Anyhow thank you all for your help. CardLayout seems to be exactly what I need to implement functionality I was looking for from my application.

Similar Threads

  1. Launch native app inside a JFrame???
    By benedums in forum Advanced Java
    Replies: 6
    Last Post: 02-25-2011, 03:23 PM
  2. How to add JFrame inside JPanel
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 8
    Last Post: 12-13-2009, 08:41 PM
  3. Centering inside a JFrame
    By kahaj in forum AWT / Swing
    Replies: 9
    Last Post: 09-23-2009, 07:23 PM
  4. Switch JPanels in a single JFrame
    By atom86 in forum AWT / Swing
    Replies: 8
    Last Post: 09-23-2009, 09:30 AM
  5. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 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
  •