/** * Creates a close all tabs button that closes all tabs in the given Tabbed Panel when the button * is selected * * @param tabbedPanel the tabbed panel that the button will close all tabs in * @return the close button */ private JButton createCloseAllTabsButton(final TabbedPanel tabbedPanel) { final JButton closeButton = createXButton(); closeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // Iterate over all tabs and remove them. int tabCount = tabbedPanel.getTabCount(); for (int i = 0; i < tabCount; i++) tabbedPanel.removeTab(tabbedPanel.getTabAt(0)); } }); // Add a tab listener to the tabbed panel so that we know when tabs are // added // and removed so that the close button can be hidden when the tabbed // panel // doesn't contain any tabs. tabbedPanel.addTabListener( new TabAdapter() { public void tabAdded(TabEvent event) { closeButton.setVisible(true); } public void tabRemoved(TabRemovedEvent event) { // Hide the close button when there are no tabs in the tabbed // panel closeButton.setVisible(event.getTabbedPanel().getTabCount() > 0); } }); return closeButton; }
/** Constructor */ private SimpleTabbedPanelExample() { frame.setSize(600, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(createMenuBar()); frame.getContentPane().add(tabbedPanel, BorderLayout.CENTER); // Set the "close all tabs" button as a tab area component tabbedPanel.setTabAreaComponents(new JComponent[] {createCloseAllTabsButton(tabbedPanel)}); // Create 6 titled tabs and add them to the tabbed panel for (int i = 0; i < 6; i++) tabbedPanel.addTab(createTab()); // Apply the default theme tabbedPanel.getProperties().addSuperObject(themes[0].getTabbedPanelProperties()); titledTabProperties.addSuperObject(themes[0].getTitledTabProperties()); activeTheme = themes[0]; frame.setVisible(true); }
/** * Applies properties from a theme to tabbed panel and all the titled tabs. * * @param theme theme to apply */ private void applyTheme(TabbedPanelTitledTabTheme theme) { // Remove the previous theme. If we hadn't added the DefaultTheme the // first time we wouldn't have any theme to remove and thus we would // have had to implement a flag or theme reference to tell us if we // needed to remove a theme or not. tabbedPanel.getProperties().removeSuperObject(activeTheme.getTabbedPanelProperties()); titledTabProperties.removeSuperObject(activeTheme.getTitledTabProperties()); // Adding a super object means that any properties that are set in the // super // object will be used instead of the same properties in the default // properties. Note that if you have explicitly set a property, for // example setTabAreaOrientation in the properties for the tabbed // panel, then that property will not be affected by the super object // i.e. it will still return the same value after adding the super // object. tabbedPanel.getProperties().addSuperObject(theme.getTabbedPanelProperties()); titledTabProperties.addSuperObject(theme.getTitledTabProperties()); activeTheme = theme; }