Java/JDK 6/JTabbedPane
Add Components to JTabbedPane
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 JDK6TabbedPaneExample {
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 JDK6TabbedPaneExample() {
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) {
JDK6TabbedPaneExample main = new JDK6TabbedPaneExample();
}
}
JTabPane with TextField in the tab
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class JDK6TabSample {
static void addIt(JTabbedPane tabbedPane, String text) {
JLabel label = new JLabel(text);
JButton button = new JButton(text);
JPanel panel = new JPanel();
panel.add(label);
panel.add(button);
tabbedPane.addTab(text, panel);
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, new JTextField(text));
}
public static void main(String args[]) {
JFrame f = new JFrame("Got JTabbedPane?");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
addIt(tabbedPane, "Tab One");
addIt(tabbedPane, "Tab Two");
addIt(tabbedPane, "Tab Three");
addIt(tabbedPane, "Tab Four");
addIt(tabbedPane, "Tab Five");
f.add(tabbedPane, BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
New Methods in the JTabPane Component (Add component to JTabPane)
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
class TabComponent extends JPanel implements ActionListener {
private JTabbedPane pane;
public TabComponent(String title, JTabbedPane pane) {
this.pane = pane;
setOpaque(false);
JLabel label = new JLabel(title);
add(label);
JButton button = new JButton("Close");
button.setPreferredSize(new Dimension(50,10));
button.addActionListener(this);
add(button);
}
public void actionPerformed(ActionEvent e) {
int i = pane.indexOfTabComponent(this);
if (i != -1)
pane.remove(i);
}
}
public class JDK6TabComponentDemo {
public static void main(String[] args) {
JTabbedPane pane = new JTabbedPane();
String title = "Tab";
pane.add(title, new JLabel(title));
pane.setTabComponentAt(0, new TabComponent(title, pane));
JFrame frame = new JFrame("Tab Component Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.setSize(500, 200);
frame.setVisible(true);
}
}