Suppose I have added 4 panels to a JFrame. On a perticular action I want to update the components of panel 1 only. Is there any way(method) through which I can repaint the panel 1 after updating its components . I tried with panel1.repaint(), but could not get the results. Or is it necessary to repaint the whole jFrame. Please giude me.
I tried the option specified by you but still could not get the required result.
To make my question more clear let us consider the example given below.
Suppose there are total 4 JPanels : mainPanel,statusInfoPanel,buttonPanel,dataPanel.
My class extends JFrame. The panels statusInfoPanel,buttonPanel and dataPanel are added to mainPanel.
Finally mainPanel is added to the JFrame.
Now, I want to change the contents of the data panel on the click of a specific button present on a buttonPanel . I will retreive the values from the database and will construct the JTable with this data and add this JTable to the dataPanel. I am trying to display the new data but could not do that. Am I missing something here? ,is dataPanel.repaint() sufficient to get the data cahange to be displayed? .
Or else do I have to construct the whole mainPanel and add it to JFrame?
Joined: Jun 13, 2009
and will construct the JTable with this data and add this JTable to the dataPanel.
There is no need to create a new JTable. All you need to do is update the existing table with a new TableModel. Something like:
If you need more help than you should create a SSCCE, that demonstrates the incorrect behaviour.
I have set the model for the JTable . Only table.revalidate();and table.repaint(); are sufficient to get the new data displayed or else we have to add the JTable to the mainPanel and the mainPanel to the JFrame and repaint all the panels. I have tried different ways but could not get the updated data displayed.
Which is the better approach? Thanks.
Joined: Jun 13, 2009
I just gave you the 2 important lines of code. There is no need for a revalidate() or repaint().
And I don't see a SSCCE so I can't help anymore. Good luck.