Java: "Add Tab Button" for JTabbedPane

Is it possible to add a button to a tabbed panel like in Firefox.

enter image description here

The plus button is what I want.

thanks

+6
java button tabs jtabbedpane
source share
4 answers

I think you can manage it by creating your own JTabbedPaneUI and installing it on the JTabbedPane using setUI .

Your ComponentUI has methods for keeping available children. If you specify JButton and a JLabel , then you may be in business.

I myself have not tried this. This is "at your own peril and risk" :)

+9
source share

You can try the following:

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

Screenshothot

+3
source share

I tried several solutions and came up with this:

 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 next to the tab

0
source share

Writing the following code to the default constructor by default

  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 a method for declaring and initializing int tab2 = 2; when starting the main class. His job.

 private void createTab() { tabbedPane.addTab("New Tab",new Panel()); tabbedPane.addTab(" + ",null,panel1,null); tabbedPane.setSelectedIndex(tab2); tab2++; } 
0
source share

All Articles