Results 1 to 8 of 8
- 03-30-2010, 09:52 AM #1
Member
- Join Date
- Mar 2010
- Location
- Belgrade, Serbia
- Posts
- 27
- Rep Power
- 0
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.
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.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; }
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.
- 03-30-2010, 09:55 AM #2
You're doing too much here, use a CardLayout and switch between the panels.
How to Use CardLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-30-2010, 10:24 AM #3
Member
- Join Date
- Mar 2010
- Location
- Belgrade, Serbia
- Posts
- 27
- Rep Power
- 0
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.
- 03-30-2010, 10:27 AM #4
You're welcome.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-30-2010, 06:47 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
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:
You only call setContentPane() once and don't touch the JFrame anymore; all your additions and removals are now done to rootPanel.Java Code:JPanel rootPanel = new JPanel (); // set it up, add all your components to it frame.setContentPane (rootPanel);
[/code]
- 03-30-2010, 08:00 PM #6
Member
- Join Date
- Mar 2010
- Location
- Belgrade, Serbia
- Posts
- 27
- Rep Power
- 0
Thank you for your advice.
- 03-31-2010, 01:55 AM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
There is nothing wrong with doing that. A JFrame always has the same default layout manager.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?).
- 03-31-2010, 08:39 AM #8
Member
- Join Date
- Mar 2010
- Location
- Belgrade, Serbia
- Posts
- 27
- Rep Power
- 0
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
-
Launch native app inside a JFrame???
By benedums in forum Advanced JavaReplies: 6Last Post: 02-25-2011, 03:23 PM -
How to add JFrame inside JPanel
By niteshwar.bhardwaj in forum Java 2DReplies: 8Last Post: 12-13-2009, 08:41 PM -
Centering inside a JFrame
By kahaj in forum AWT / SwingReplies: 9Last Post: 09-23-2009, 07:23 PM -
Switch JPanels in a single JFrame
By atom86 in forum AWT / SwingReplies: 8Last Post: 09-23-2009, 09:30 AM -
Can't synchronize multiple JPanels in a JFrame
By vassil_zorev in forum AWT / SwingReplies: 0Last Post: 12-30-2007, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks