943,576 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1612
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 19th, 2008
-1

JPAnel need clean sweaping

Expand Post »
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
Java Syntax (Toggle Plain Text)
  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?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Nov 19th, 2008
0

Re: JPAnel need clean sweaping

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
Java Syntax (Toggle Plain Text)
  1. Dimension d = mainPanel.getSiez();
  2. mainPanel.removeAll();
  3. mainPanel.add(addEP);
  4. mainPanel.setPreferredSize(d);
  5. mainPanel.setSize(d);
  6. validate();
or
Java Syntax (Toggle Plain Text)
  1. mainPanel.removeAll();
  2. mainPanel.add(addEP);
  3. mainPanel.getParent().validate();
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 19th, 2008
-1

Re: JPAnel need clean sweaping

Unfortunately both of them still gives same result of "forgotten" content
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Nov 19th, 2008
0

Re: JPAnel need clean sweaping

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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 19th, 2008
-1

Re: JPAnel need clean sweaping

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
Java Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Nov 19th, 2008
0

Re: JPAnel need clean sweaping

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:

Java Syntax (Toggle Plain Text)
  1. Dimension d = mainPanel.getSiez();
  2. mainPanel.removeAll();
  3. mainPanel.add(addEP);
  4. addEP.setPreferredSize(d);
  5. addEP.setSize(d);
  6. mainPanel.setPreferredSize(d);
  7. mainPanel.setSize(d);
  8. validate();
(and, if that works, you should probably be able to remove the two mainPanel.set*Size calls, but try including them, first).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 19th, 2008
-1

Re: JPAnel need clean sweaping

No change

OK, here is construction of the JFrame
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Nov 19th, 2008
1

Re: JPAnel need clean sweaping

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 19th, 2008
-1

Re: JPAnel need clean sweaping

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.)
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Nov 19th, 2008
0

Re: JPAnel need clean sweaping

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java Excel connectivity code
Next Thread in Java Forum Timeline: Is there an API I can use to program a flow diagram?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC