Reputation: 1216
I have a JTabbedPane with 5 tabs. I have a JPanel added to each tab say 5 JPanel's, which has several components on each tab. If i want to update a tab content dynamically depending on a flag variable say to replace the content on tab 4, i need to replace existing panel with new Jpanel, how do i achieve this dynamic update on a particular tab?
Upvotes: 1
Views: 4025
Reputation: 1604
I fought this silly problem for a day before I was able to tell apart JTabbedPane's getComponentAt and getTabComponentAt methods. FYI, the first gets the content of the tab and the latter the tab's title bar component -.- I used something like this:
//create new content for tab
JPanel tabPanel = generateTab();
//assign new content to tab
tabbedPane.setComponentAt(index, tabPanel);
Upvotes: 1
Reputation: 2575
There are more possibilities to achieve that.
The first possibility is to replace the content of the pane. Assuming you have added the pane you want to update as follows:
this.content = new JPanel();
this.content.add(new JLabel("Content1"));
myPane.addTab("Tab1", this.content);
Then you can change the content of the tab:
this.content.removeAll();
this.content.add(new JLabel("Content2"));
this.content.revalidate();
this.content.repaint();
The second possibility is to completely remove the tab and afterwards add a new one with the new content.
myPane.removeTabAt(index);
myPane.addTab("New Tab", newContent);
Generally I would prefer the first possibility.
Upvotes: 2