0

I'm working with JTabbedPane, and I want to add to the tabs a close button.

I created a JButton, with one: ActionListener, for when you click, this removes the tabs is open one by one.

Also, I create a JTabbedPane...

//Tabbed.
JTabbedPane Tabbedr = new JTabbedPane();
File F = new File("HTML1.html");
Frame.getContentPane().add(Tabbedr);
Tabbedr.addTab(F.getName());

//Button.
JButton Close = new JButton();
Close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
Tabbedr.remove(1);}});

So, here all its fine, but I dont know how to add the JButton "Close", to my tabs. As in some famous editors,

To test the button I added this to a one toolbar, but I want to put this inside to the tabs. Example: Hello,txt "X".

¿Someone can help me? Thank you for read and your time.

Sthatyc Soul.
  • 136
  • 10
  • If this is not a duplicate of [`ButtonTabComponent`](https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html), please edit your question to include a [mcve] that shows your current approach. – trashgod Jul 26 '16 at 11:55

1 Answers1

1
public class CustomTabbedPane extends JTabbedPane {

    public CustomTabbedPane() {

        // Add panel to tab screen
        JPanel tab1Panel = new JPanel();
        addTab(null, null, tab1Panel, "");

        // Add label and button to the tab name
        JPanel tab1 = new JPanel();
        temp.add(new JLabel("Tab Name"));
        temp.add(new JButton("x"));
        setTabComponentAt(0, tab1);
    }
}

Of course, you would want to add an ActionListener to the JButton and configure it so that the tab disappears upon button press.

To make it look nice, I suggest you get rid of the margins on the JButton,

button.setMargin(new Insets(0, 0, 0, 0))

remove the border,

button.setBorder(BorderFactory.createEmptyBorder());

and set the tab1 JPanel to a "null" layout to manually place the "x" button and the label.

JYun
  • 311
  • 2
  • 12