RSS Forums RSS

JPAnel need clean sweaping

Please support our Java advertiser: Programming Forums
Thread Solved
Reply
Posts: 3,465
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

JPAnel need clean sweaping

  #1  
Nov 19th, 2008
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
  1. JMenuItem jmiAddEmployee = new JMenuItem("Add Employee");
  2. jmiAddEmployee.addActionListener(new ActionListener()
  3. {
  4. public void actionPerformed(ActionEvent ae)
  5. {
  6. mainPanel.removeAll();
  7. mainPanel.add(addEP);
  8. validate();
  9. }
  10. });
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?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,957
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: JPAnel need clean sweaping

  #2  
Nov 19th, 2008
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
Dimension d = mainPanel.getSiez();
mainPanel.removeAll();
mainPanel.add(addEP);
mainPanel.setPreferredSize(d);
mainPanel.setSize(d);
validate();
or
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
Reply With Quote  
Posts: 3,465
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: JPAnel need clean sweaping

  #3  
Nov 19th, 2008
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, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote  
Posts: 1,957
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: JPAnel need clean sweaping

  #4  
Nov 19th, 2008
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
Reply With Quote  
Posts: 3,465
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: JPAnel need clean sweaping

  #5  
Nov 19th, 2008
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
  1. private AddEmployeePanel addEP = new AddEmployeePanel();
  2. private EditEmployeePanel editEP = new EditEmployeePanel();
  3. private AllClientsPanel allCP = new AllClientsPanel();
which are placed in "mainPanel" depending on pressed option
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote  
Posts: 1,957
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: JPAnel need clean sweaping

  #6  
Nov 19th, 2008
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:

Dimension d = mainPanel.getSiez();
mainPanel.removeAll();
mainPanel.add(addEP);
addEP.setPreferredSize(d);
addEP.setSize(d);
mainPanel.setPreferredSize(d);
mainPanel.setSize(d);
validate();
(and, if that works, you should probably be able to remove the two mainPanel.set*Size calls, but try including them, first).
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
Reply With Quote  
Posts: 3,465
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: JPAnel need clean sweaping

  #7  
Nov 19th, 2008
No change

OK, here is construction of the JFrame
  1. public void runBankSystem()
  2. {
  3. Dimension d = new Dimension(500, 500);
  4. setTitle("Bank Managment System");
  5. setSize(d);
  6. setLocation(rp.resultPosition(d));
  7. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8. setMenuBar();
  9. setJMenuBar(jmb);
  10. getContentPane().add(mainPanel);
  11. setVisible(true);
  12. }
and here is JMenuBar to call the changes
  1. public void setMenuBar()
  2. {
  3. jmb = new JMenuBar();
  4. JMenu jmEmployee = new JMenu("Employee");
  5. JMenuItem jmiAddEmployee = new JMenuItem("Add Employee");
  6. jmiAddEmployee.addActionListener(new ActionListener()
  7. {
  8. public void actionPerformed(ActionEvent ae)
  9. {
  10. mainPanel.removeAll();
  11. mainPanel.add(addEP);
  12. validate();
  13. }
  14. });
  15. JMenuItem jmiEditEmployee = new JMenuItem("Edit Employee");
  16. jmiEditEmployee.addActionListener(new ActionListener()
  17. {
  18. public void actionPerformed(ActionEvent ae)
  19. {
  20. mainPanel.removeAll();
  21. mainPanel.add(editEP);
  22. validate();
  23. }
  24. });
  25. jmiEditEmployee.addActionListener(this);
  26. jmEmployee.add(jmiAddEmployee);
  27. jmEmployee.add(jmiEditEmployee);
  28. jmb.add(jmEmployee);
  29. JMenu jmClient = new JMenu("Client");
  30. JMenuItem jmiAllClients = new JMenuItem("All Customers");
  31. jmiAllClients.addActionListener(new ActionListener()
  32. {
  33. public void actionPerformed(ActionEvent ae)
  34. {
  35. mainPanel.removeAll();
  36. mainPanel.add(allCP);
  37. validate();
  38. }
  39. });
  40. jmiAllClients.addActionListener(this);
  41. jmClient.add(jmiAllClients);
  42. jmb.add(jmClient);
  43. JMenu jmLogout = new JMenu("Logout");
  44. jmLogout.addActionListener(this);
  45. jmb.add(jmLogout);
  46. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote  
Posts: 1,957
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: JPAnel need clean sweaping

  #8  
Nov 19th, 2008
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.
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
Reply With Quote  
Posts: 3,465
Reputation: peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold peter_budo is a splendid one to behold 
Solved Threads: 412
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: JPAnel need clean sweaping

  #9  
Nov 19th, 2008
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.)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote  
Posts: 1,957
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: JPAnel need clean sweaping

  #10  
Nov 19th, 2008
Originally Posted by peter_budo View Post
I was under impression that repaint() is called only when you working explicitly with some graphical content (images, lines, shapes etc.)


Well, it is a "Graphical User Interface".

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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Java Forum
Views: 731 | Replies: 10 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:42 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC