This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding and removing components on JPanels  RSS feed

 
Wallace O'Connell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JPanel with a GridLayout of 3, 1 - i.e. 3 high and 1 wide. Within each grid I have a line chart. In a sparate panel I have a table and when the user clicks on cells of the table I want to layout of the panel with the charts to change.
Here is some code I have to remove the charts:

rightPanel.remove(lineC.getChartPanel());
rightPanel.remove(chart.getChartPanel());
rightPanel.repaint();

This works fine and the 2 charts disappear from the bottom 2 grids of the panel. However, I then proceed to create 2 new line charts and I want to add them on to the panel in place of the 2 just removed. Here is the code:

lineC = new LineChart("test", chartData, times);
ChartPanel lineChartPanel = lineC.getChartPanel();
rightPanel.add(lineChartPanel);
chart = new BarChart("test", chartData, hits);
ChartPanel barChartPanel = chart.getChartPanel();
rightPanel.add(barChartPanel);
rightPanel.repaint();

The problem is, is that the 2 new charts do not appear on the JPanel. I have tried just adding 2 JLabels and 2 JButtons to make sure that the problem is not with the charts but the labels or buttons do not appear either.
Can anybody help me with this problem? It would be much appreciated if you could.
Thanks in advance,
Wallace
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of calling repaint(), you should be calling invalidate() or validate(). Repaint() tells the component to paint istself again. Validate() and invalidate() tell the component to run their layout manager again ( and then repaint() ). Whenever you add() or remove() components you should always re-layout your container afterwards.
 
Garandi Garandi
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate,
I have added a table to a frame, then the Thread sleeps for 1000 mil. removes the frame from the table and then the thread sleeps for 1000 mil and addes it again, but when it removes the table I don't see a blank frame, it looks like it does not removes the frame at all.

Thank you
Garandi
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two problems -

call frame.getContentPane().remove( coverage ); not frame.remove( coverage );

Also, after calling frame.validate(); call frame.repaint();... it looks like you have to specifically specify a repaint.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!