| | |
JPAnel need clean sweaping
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
In the JFrame I have two components JMenuBar and JPanel. A selection in one of the options in menu bar change the content of panel as
This will replace the content, unfortunately if the previous components in the old one been longer then the new ones I will get new components with bits of old where the content is shorter. Is there any solution to this problem or do I need to swap my JMenuBar+JPanel for JTabbedPane?
Java Syntax (Toggle Plain Text)
JMenuItem jmiAddEmployee = new JMenuItem("Add Employee"); jmiAddEmployee.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { mainPanel.removeAll(); mainPanel.add(addEP); validate(); } });
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
The problem is that the panel will only cover as much area as it needs. Anything beyond this area is "ignored" during a validate on the panel (since that area no longer "belongs" to the Panel. 2 "quick" possibilites (I would try the second first).
Have you tried
or
Have you tried
Java Syntax (Toggle Plain Text)
Dimension d = mainPanel.getSiez(); mainPanel.removeAll(); mainPanel.add(addEP); mainPanel.setPreferredSize(d); mainPanel.setSize(d); validate();
Java Syntax (Toggle Plain Text)
mainPanel.removeAll(); mainPanel.add(addEP); mainPanel.getParent().validate();
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Unfortunately both of them still gives same result of "forgotten" content
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
How is the component containing "mainPanel" constructed? Can you show that (without any of the code behind the constructs, but simply what components are in it and how they get placed in it).
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
The "mainPanel" is just a container in which I drop one of the pre-made JPanels that are constructed separately. I though it will be easier to just drop pre-made component in JPanel and then do the cleaning with removeAll when different option selected. Right now I have only 3 panels
which are placed in "mainPanel" depending on pressed option
Java Syntax (Toggle Plain Text)
private AddEmployeePanel addEP = new AddEmployeePanel(); private EditEmployeePanel editEP = new EditEmployeePanel(); private AllClientsPanel allCP = new AllClientsPanel();
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Yes, but what contains "mainPanel"? And how is mainPanel added to that container. Or is "mainPanel" the contentPane?
I have a more involved possibility but it involves the component that contains "mainPanel" more than it does "mainPanel" itself.
But, since you're saying that your adding "premade" Panels to mainPanel, then try this, as well:
(and, if that works, you should probably be able to remove the two mainPanel.set*Size calls, but try including them, first).
I have a more involved possibility but it involves the component that contains "mainPanel" more than it does "mainPanel" itself.
But, since you're saying that your adding "premade" Panels to mainPanel, then try this, as well:
Java Syntax (Toggle Plain Text)
Dimension d = mainPanel.getSiez(); mainPanel.removeAll(); mainPanel.add(addEP); addEP.setPreferredSize(d); addEP.setSize(d); mainPanel.setPreferredSize(d); mainPanel.setSize(d); validate();
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
No change
OK, here is construction of the JFrame and here is JMenuBar to call the changes
OK, here is construction of the JFrame
Java Syntax (Toggle Plain Text)
public void runBankSystem() { Dimension d = new Dimension(500, 500); setTitle("Bank Managment System"); setSize(d); setLocation(rp.resultPosition(d)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setMenuBar(); setJMenuBar(jmb); getContentPane().add(mainPanel); setVisible(true); }
Java Syntax (Toggle Plain Text)
public void setMenuBar() { jmb = new JMenuBar(); JMenu jmEmployee = new JMenu("Employee"); JMenuItem jmiAddEmployee = new JMenuItem("Add Employee"); jmiAddEmployee.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { mainPanel.removeAll(); mainPanel.add(addEP); validate(); } }); JMenuItem jmiEditEmployee = new JMenuItem("Edit Employee"); jmiEditEmployee.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { mainPanel.removeAll(); mainPanel.add(editEP); validate(); } }); jmiEditEmployee.addActionListener(this); jmEmployee.add(jmiAddEmployee); jmEmployee.add(jmiEditEmployee); jmb.add(jmEmployee); JMenu jmClient = new JMenu("Client"); JMenuItem jmiAllClients = new JMenuItem("All Customers"); jmiAllClients.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { mainPanel.removeAll(); mainPanel.add(allCP); validate(); } }); jmiAllClients.addActionListener(this); jmClient.add(jmiAllClients); jmb.add(jmClient); JMenu jmLogout = new JMenu("Logout"); jmLogout.addActionListener(this); jmb.add(jmLogout); }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
I'm sorry, but with some tests, it seems as though simply calling repaint after calling validate accomplishes your goal. (At least it did for me, although without it I also saw what you were experiencing.)
I would normally assume that a call to repaint() after a call to validate() would essentially be redundant, but seemingly not.
I would normally assume that a call to repaint() after a call to validate() would essentially be redundant, but seemingly not.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Yes in deed, repaint does the magic.
I was under impression that repaint() is called only when you working explicitly with some graphical content (images, lines, shapes etc.)
I was under impression that repaint() is called only when you working explicitly with some graphical content (images, lines, shapes etc.)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
I was under impression that repaint() is called only when you working explicitly with some graphical content (images, lines, shapes etc.)

I was under the impression, however, that validate triggered a repaint, which is the reason I waited until the end to try it. Guess I was wrong. Not the first time, won't be the last time.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Other Threads in the Java Forum
- Previous Thread: Java Excel connectivity code
- Next Thread: Is there an API I can use to program a flow diagram?
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






