Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Essentially, you're going to need to supply a "renderer" for the tab. Take a look at <a href="http://docs.oracle.com/javase/6/docs/api/javax/swing/JTabbedPane.html#setTabComponentAt%28int,%20java.awt.Component%29" rel="noreferrer">JTabbedPane.setTabComponentAt(...)</a> for more information.</p> <p>The basic idea is to supply a component that will be laid out on the tab.</p> <p>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.</p> <pre><code>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); </code></pre> <p>Now somewhere else, I establish the action handler...</p> <pre><code>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 ;) } } } </code></pre> <p>Now, you just as easily use any component you like and attach a mouse listener to it and monitor the mouse clicks...</p> <p><strong>Updated</strong></p> <p>The above example will only remove the currently active tab, there are a couple of ways to fix this.</p> <p>The best is to probably provide some means for the action to find the tab it's associated with...</p> <pre><code>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 &gt;= 0) { tabPane.removeTabAt(index); // It would probably be worthwhile getting the source // casting it back to a JButton and removing // the action handler reference ;) } } } </code></pre> <p>This uses the name of tab (as used with <code>JTabbedPane#addTab</code>) to find and then remove the tab and its associated component...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload