JTabbedPane: setTabComponentAt(int index, Component component)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Main {
private JFrame frame = new JFrame();
private JTabbedPane tabbedPane = new JTabbedPane();
private JButton addTabButton = new JButton("Add Tab");
private ImageIcon closeXIcon = new ImageIcon("C:/CloseX.gif");
private Dimension closeButtonSize;
private int tabCounter = 0;
public Main() {
addTabButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
add();
}
});
closeButtonSize = new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2);
frame.add(tabbedPane, BorderLayout.CENTER);
frame.add(addTabButton, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setMinimumSize(new Dimension(300, 300));
frame.setVisible(true);
}
public void add() {
final JPanel content = new JPanel();
JPanel tab = new JPanel();
tab.setOpaque(false);
JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
JButton tabCloseButton = new JButton(closeXIcon);
tabCloseButton.setPreferredSize(closeButtonSize);
tabCloseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int closeTabNumber = tabbedPane.indexOfComponent(content);
tabbedPane.removeTabAt(closeTabNumber);
}
});
tab.add(tabLabel, BorderLayout.WEST);
tab.add(tabCloseButton, BorderLayout.EAST);
tabbedPane.addTab(null, content);
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
}
public static void main(String[] args) {
Main main = new Main();
}
}
Related examples in the same category
1. | JTabbedPane.SCROLL_TAB_LAYOUT | | |
2. | JTabbedPane: addChangeListener(ChangeListener l) | | |
3. | JTabbedPane: addTab(String title, Component component) | | |
4. | JTabbedPane: addTab(String title, Icon icon, Component component, String tip) | | |
5. | JTabbedPane: getComponentAt(int index) | | |
6. | JTabbedPane: getDisabledIconAt(int index) | | |
7. | JTabbedPane: getDisplayedMnemonicIndexAt(int tabIndex) | | |
8. | JTabbedPane: getIconAt(int index) | | |
9. | JTabbedPane: getMnemonicAt(int tabIndex) | | |
10. | JTabbedPane: getTitleAt(int index) | | |
11. | JTabbedPane: getToolTipTextAt(int index) | | |
12. | JTabbedPane: indexOfComponent(Component component) | | |
13. | JTabbedPane: indexOfTab(String title) | | |
14. | JTabbedPane: insertTab(String title, Icon icon, Component component, String tip, int index) | | |
15. | JTabbedPane: isEnabledAt(int index) | | |
16. | JTabbedPane: remove(Component component) | | |
17. | JTabbedPane: removeAll() | | |
18. | JTabbedPane: removeTabAt(int index) | | |
19. | JTabbedPane: setBackgroundAt(int index, Color background) | | |
20. | JTabbedPane: setDisabledIconAt(int index, Icon disabledIcon) | | |
21. | JTabbedPane: setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex) | | |
22. | JTabbedPane: setEnabledAt(int index, boolean enabled) | | |
23. | JTabbedPane: setForegroundAt(int index, Color foreground) | | |
24. | JTabbedPane: setMnemonicAt(int tabIndex, int mnemonic) | | |
25. | JTabbedPane: setSelectedIndex(int index) | | |
26. | JTabbedPane: setTabLayoutPolicy(int tabLayoutPolicy) | | |
27. | JTabbedPane: setTabPlacement(int tabPlacement) | | |
28. | JTabbedPane: setToolTipTextAt(int index, String toolTipText) | | |