Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing one component and replacing it with another

 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning

I have a lil' problem, hope someone can help out....

I have a JPanel (tablePanel) with 4 JLabels placed north, south, east and west (JLabels create a border).

In the centre of tablePanel, I have added JScrollPane (tableScroll) - this holds a JTable.

All good so far! Display shows a splendid scrollable JTable with a smart black border.

My "more details" JButton requires that tableScroll be removed from the centre, taking the JTable with it, and is replaced by a JPanel component.
To accomplish this task, I used :


When "more details" button is clicked, nothing happens until I either maximize or minimize the frame, whereupon the table dissappears and the panel is displayed as planned.

When I want to return to the JTable view, another button event causes only the header to display until the frame is maximised or minimized, then the whole JTable appears.

Does anyone know why this happens? - I am puzzled.

Regards,
J
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tablePanel.repaint() should do the trick after you have removed and added your components.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this post
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If tablePanel.repaint() does not work...trying doing the following:

tablePanel.revalidate();
tablePanel.repaint();

Thanks,
Cathy.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you add/remove components you may have to ask swing to do a new layout. You do this with either the JComponent method revalidate or the Container method validate. Sometimes you may have to follow this with a call to repaint. You just have to play with this until you get it to work with the minimum number of calls.

The main clue for the need to validate the layout is that nothing appears until you resize the container.
 
J Chandler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg, David, Cathy and Craig,

thank you all for your comments.

I have altered my code and now everything is working beautifully -

Regards,

Happy J
 
Haroldo Level
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have a similar problem. I'm trying to remove the whole content of a JPanel that I have polacved inside an applet. I tried everything, even what you posted and when I get to the part when I modify the panel the whole applet just crashes. I hope you hasve a solution for this. thanks in advance
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic