8

Is it possible to add a button to a tabbed pane like in firefox.

enter image description here

The plus-button is what I want.

Thanks

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

4 Answers4

9

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" :)

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • 1
    Using a custom UI is the way to go. I've done something similar before. For the "X" on the tab to close it, you can override the painting of the tab itself to include the X and install a mouse listener that checks if the user has selected that area with the X. – Jeff Storey Dec 28 '09 at 21:15
  • 2
    Sun's "Tabbed Panes" tutorial includes an example of just such a feature: http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html – trashgod Dec 28 '09 at 22:09
3

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);
}

Screenshot

Benjamin
  • 544
  • 5
  • 21
user2287966
  • 257
  • 1
  • 4
  • 10
1

Write Following Code in Default Constructor Of Class

    JPanel panel = new JPanel();
    tabbedPane.addTab("Welcome", null, panel, null);
    tabbedPane.addTab(" + ", null, panel1, null);

    tabbedPane.addChangeListener(new ChangeListener()
    {
        public void stateChanged(ChangeEvent evt)
        {
            JTabbedPane tabbedPane = (JTabbedPane)evt.getSource();

            if(tabbedPane.getSelectedIndex() == tabbedPane.indexOfTab(" + "))
            {
                createTab();
            }
        }
    });

And Create Method to declare and initialized int tab2 = 2; at Starting of main class. Its Worked.

private void createTab()
{
    tabbedPane.addTab("New Tab",new Panel());
    tabbedPane.addTab(" + ",null,panel1,null);
    tabbedPane.setSelectedIndex(tab2);
    tab2++;
}
Bhola
  • 392
  • 1
  • 15
  • what if one of the tabs that gets created is called " + "? Is there a better way to tell if it's the last tab that's selected? – Aaron Esau Dec 17 '22 at 05:55
0

I have tried several solutions and came with this one:

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class TestTab {

    public static void main(String[] args) {
    JFrame parent = new JFrame();

    final JTabbedPane tabEntity = new JTabbedPane();
    tabEntity.addTab("Details", null, new JScrollPane());
    tabEntity.addTab("Context", null, new JScrollPane());
    tabEntity.addTab("", null, new JScrollPane());

    addButtonToTab(tabEntity);

    parent.add(tabEntity);
    parent.setSize(new Dimension(400, 200));
    parent.setVisible(true);
    }

    public static void addButtonToTab(final JTabbedPane tabEntity) {
    tabEntity.setTabComponentAt(tabEntity.getTabCount() - 1, new JButton(
        "+"));
    }
}

So you have:

Button near tab example

Vokail
  • 630
  • 8
  • 27