Reputation: 749
I have a tab called Status that I've initialized in my main window like so:
private static void setupTabbedPane ( JTabbedPane tabbedPane )
{
//Create tab related components
status = new Summary();
// Create tabs
tabbedPane.addTab ("Status", new WebScrollPane(status.summaryPage));
tabbedPane.setBackgroundAt(0, Color.DARK_GRAY);
tabbedPane.setForegroundAt(0, Color.BLACK);
}
It adds a summaryPage which is simply a JPanel. I've added a feature where when the user right clicks they can launch a scaled down window (new JFrame) with the content in the summary page, now I'm trying to put back that summary page inside that Status tab when the user closes that mini window they launched. Here's what I have:
public class MiniSmryWindow {
public JFrame frame;
public MiniSmryWindow() {
// Initialize Mini Window
frame = new JFrame("Status");
frame.setLayout(new BorderLayout());
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.add(BootWindow.status.summaryPage);
frame.setVisible(true);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.out.println("Closing mini window");
BootWindow.tabbedPane.setTabComponentAt(0, BootWindow.status.summaryPage);
}
});
}
Right now the component gets added where the tab title is not inside the tab's panel, how can I fix this?
Upvotes: 1
Views: 613
Reputation: 17971
Right now the component gets added where the tab title is not inside the tab's panel, how can I fix this?
Yes, this is the expected behavior of setTabComponentAt() method:
"Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon."
You should use insertTab() instead.
"Inserts a new tab for the given component, at the given index, represented by the given title and/or icon, either of which may be null."
For example:
BootWindow.tabbedPane.insertTab("title", null, BootWindow.status.summaryPage, "tool tip", 0);
Note: as the tabbed pane has been already displayed then it probably should be revalidated and repainted after insert the tab (not tested though):
BootWindow.tabbedPane.insertTab("title", null, BootWindow.status.summaryPage, "tool tip", 0);
BootWindow.tabbedPane.revalidate();
BootWindow.tabbedPane.repaint();
Upvotes: 3