Is it possible to add a button to a tabbed pane like in firefox.
The plus-button is what I want.
Thanks
To create a tabbed pane, instantiate JTabbedPane , create the components you wish it to display, and then add the components to the tabbed pane using the addTab method.
You should use the method JTabbedPane. setSelectedIndex(int index) with the index of the tab you want. Save this answer.
Each tab has a name. To add a tab to the JTabbedPane , simply call addTab() . You'll need to specify the name of the tab as well as a component that supplies the tab's contents. Typically, it's a container holding other components.
Tabs/components are added to a TabbedPane object by using the addTab and insertTab methods.
I think you should be able to manage it by building your own JTabbedPaneUI
and setting it on the JTabbedPane
using setUI
.
Your ComponentUI
has methods to get a hold of the accessible children. If you specify a JButton
and a JLabel
then you may be in business.
I haven't attempted this myself though. This is "at your own risk" :)
You can try this:
public static void main (String[] args) {
JFrame parent = new JFrame ();
final JTabbedPane pane = new JTabbedPane ();
pane.addTab ("test", null);
FlowLayout f = new FlowLayout (FlowLayout.CENTER, 5, 0);
// Make a small JPanel with the layout and make it non-opaque
JPanel pnlTab = new JPanel (f);
pnlTab.setOpaque (false);
// Create a JButton for adding the tabs
JButton addTab = new JButton ("+");
addTab.setOpaque (false); //
addTab.setBorder (null);
addTab.setContentAreaFilled (false);
addTab.setFocusPainted (false);
addTab.setFocusable (false);
pnlTab.add (addTab);
pane.setTabComponentAt (pane.getTabCount () - 1, pnlTab);
ActionListener listener = new ActionListener () {
@Override
public void actionPerformed (ActionEvent e) {
String title = "Tab " + String.valueOf (pane.getTabCount () - 1);
pane.addTab (title, new JLabel (title));
}
};
addTab.setFocusable (false);
addTab.addActionListener (listener);
pane.setVisible (true);
parent.add (pane);
parent.setSize (new Dimension (400, 200));
parent.setVisible (true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With