Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add close button to a JTabbedPane Tab?

I'm working in with a JTabbedPane, I need to add a close button in the tabs to close the current one.

I have been searching and as I understand I must extend from JPanel and add the close button as they say here But, is there a way to add the close buttons extending JTabbedPane or is there a easier way to do it?

Thanks in advance, I really appreciate your time and your help.

like image 218
Hermandroid Avatar asked Jul 19 '12 02:07

Hermandroid


People also ask

How do you switch tabs in JTabbedPane by clicking a button?

You should use the method JTabbedPane. setSelectedIndex(int index) with the index of the tab you want. Show activity on this post. JTabbedpane.

Which method is used to add tabs in JTabbedPane?

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.


3 Answers

Essentially, you're going to need to supply a "renderer" for the tab. Take a look at JTabbedPane.setTabComponentAt(...) for more information.

The basic idea is to supply a component that will be laid out on the tab.

I typically create a JPanel, onto which I add a JLabel (for the title) and, depending on what I want to display, some kind of control that acts as the close action.

tabPane.addTab(title, tabBody); int index = tabPane.indexOfTab(title); JPanel pnlTab = new JPanel(new GridBagLayout()); pnlTab.setOpaque(false); JLabel lblTitle = new JLabel(title); JButton btnClose = new JButton("x");  GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1;  pnlTab.add(lblTitle, gbc);  gbc.gridx++; gbc.weightx = 0; pnlTab.add(btnClose, gbc);  tabPane.setTabComponentAt(index, pnlTab);  btnClose.addActionListener(myCloseActionHandler); 

Now somewhere else, I establish the action handler...

public class MyCloseActionHandler implements ActionListener {      public void actionPerformed(ActionEvent evt) {          Component selected = tabPane.getSelectedComponent();         if (selected != null) {              tabPane.remove(selected);             // It would probably be worthwhile getting the source             // casting it back to a JButton and removing             // the action handler reference ;)          }      }  } 

Now, you just as easily use any component you like and attach a mouse listener to it and monitor the mouse clicks...

Updated

The above example will only remove the currently active tab, there are a couple of ways to fix this.

The best is to probably provide some means for the action to find the tab it's associated with...

public class MyCloseActionHandler implements ActionListener {      private String tabName;      public MyCloseActionHandler(String tabName) {         this.tabName = tabName;     }      public String getTabName() {         return tabName;     }      public void actionPerformed(ActionEvent evt) {          int index = tabPane.indexOfTab(getTabName());         if (index >= 0) {              tabPane.removeTabAt(index);             // It would probably be worthwhile getting the source             // casting it back to a JButton and removing             // the action handler reference ;)          }      }  }    

This uses the name of tab (as used with JTabbedPane#addTab) to find and then remove the tab and its associated component...

like image 76
MadProgrammer Avatar answered Sep 29 '22 23:09

MadProgrammer


I found a tab example (from the java site) that appears to do that, at least in theirs. (Though I thought, when I tried it in the past, that it also closed the currently selected tab, though it works properly when you run their example, though I think when I updated it to work on a tabbed java notepad, it was closing the currently selected tab, though maybe I did it wrong.

http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TabComponentsDemoProject/src/components/ButtonTabComponent.java

Yes, my thing is working now! This WILL work for the actual tab, rather than the currently selected tab!

like image 31
MongooseLover Avatar answered Sep 30 '22 00:09

MongooseLover


Hopefully you have got the answer to your question. I want to give a link that was very useful for me.

JTabbedPane with a close button

Here is some code as well.

public static void createAndShowGUI()
{
    JFrame frame = new JFrame("Tabs");
    frame.setMinimumSize(new Dimension(500, 200));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane tabbedPane = new JTabbedPane();

    JPanel panel = new JPanel();
    panel.setOpaque(false);
    tabbedPane.add(panel);
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel), getTitlePanel(tabbedPane, panel, "Tab1"));

    JPanel panel1 = new JPanel();
    panel1.setOpaque(false);
    tabbedPane.add(panel1);
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel1), getTitlePanel(tabbedPane, panel1, "Tab2"));

    JPanel panel2 = new JPanel();
    panel2.setOpaque(false);
    tabbedPane.add(panel2);
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel2), getTitlePanel(tabbedPane, panel2, "Tab3"));

    JPanel panel3 = new JPanel();
    panel3.setOpaque(false);
    tabbedPane.add(panel3);
    tabbedPane.setTabComponentAt(tabbedPane.indexOfComponent(panel3), getTitlePanel(tabbedPane, panel3, "Tab4"));

    frame.add(tabbedPane);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}
like image 23
Sardar Faisal Avatar answered Sep 29 '22 22:09

Sardar Faisal