JTabbedPane: addTab(String title, Icon icon, Component component, String tip)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class Main extends JFrame {
private JTextField textfield = new JTextField();
public static void main(String[] args) {
Main that = new Main();
that.setVisible(true);
}
public Main() {
setSize(450, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(textfield, BorderLayout.SOUTH);
JMenuBar mbar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new JCheckBoxMenuItem("Check Me"));
menu.addSeparator();
JMenuItem item = new JMenuItem("Exit");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(item);
mbar.add(menu);
setJMenuBar(mbar);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Button", new TabIcon(), new JButton(""), "Click here for Button demo");
}
}
class TabIcon implements Icon {
public int getIconWidth() {
return 16;
}
public int getIconHeight() {
return 16;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.black);
g.fillRect(x + 4, y + 4, getIconWidth() - 8, getIconHeight() - 8);
g.setColor(Color.cyan);
g.fillRect(x + 6, y + 6, getIconWidth() - 12, getIconHeight() - 12);
}
}
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: getComponentAt(int index) | | |
5. | JTabbedPane: getDisabledIconAt(int index) | | |
6. | JTabbedPane: getDisplayedMnemonicIndexAt(int tabIndex) | | |
7. | JTabbedPane: getIconAt(int index) | | |
8. | JTabbedPane: getMnemonicAt(int tabIndex) | | |
9. | JTabbedPane: getTitleAt(int index) | | |
10. | JTabbedPane: getToolTipTextAt(int index) | | |
11. | JTabbedPane: indexOfComponent(Component component) | | |
12. | JTabbedPane: indexOfTab(String title) | | |
13. | JTabbedPane: insertTab(String title, Icon icon, Component component, String tip, int index) | | |
14. | JTabbedPane: isEnabledAt(int index) | | |
15. | JTabbedPane: remove(Component component) | | |
16. | JTabbedPane: removeAll() | | |
17. | JTabbedPane: removeTabAt(int index) | | |
18. | JTabbedPane: setBackgroundAt(int index, Color background) | | |
19. | JTabbedPane: setDisabledIconAt(int index, Icon disabledIcon) | | |
20. | JTabbedPane: setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex) | | |
21. | JTabbedPane: setEnabledAt(int index, boolean enabled) | | |
22. | JTabbedPane: setForegroundAt(int index, Color foreground) | | |
23. | JTabbedPane: setMnemonicAt(int tabIndex, int mnemonic) | | |
24. | JTabbedPane: setSelectedIndex(int index) | | |
25. | JTabbedPane: setTabComponentAt(int index, Component component) | | |
26. | JTabbedPane: setTabLayoutPolicy(int tabLayoutPolicy) | | |
27. | JTabbedPane: setTabPlacement(int tabPlacement) | | |
28. | JTabbedPane: setToolTipTextAt(int index, String toolTipText) | | |