Java: "Add Tab Button" for a JTabbedPane

10,194

Solution 1

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

Solution 2

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

Solution 3

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++;
}
Share:
10,194

Related videos on Youtube

Martijn Courteaux
Author by

Martijn Courteaux

I'm writing Java, C/C++ and some Objective-C. I started programming in 2007 (when I was 11). Right now, I'm working on my magnum opus: an iOS, Android, OS X, Linux, Windows game to be released soon on all relevant stores. The game is written in C++ using SDL and OpenGL. A couple of seeds for my name (for java.util.Random, radix 26): 4611686047252874006 -9223372008029289706 -4611685989601901802 28825486102

Updated on April 17, 2022

Comments

  • Martijn Courteaux
    Martijn Courteaux about 2 years

    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