This code creates a tabbed pane interface with a button to dynamically add new tabs. It initializes a JFrame and JTabbedPane. It adds a first tab called "test" and configures a panel with a "+" button to trigger adding new tabs. Each new tab is given a title of "Tab" plus the tab count and contains a JLabel with the same text.
1 of 1
More Related Content
Add tab jtabbedpane
1. public static void main (String[] args) {
JFrame parent = new JFrame ();
final JTabbedPane pane = new JTabbedPane ();
pane.addTab ("test", null);
FlowLayout f = new FlowLayout (FlowLayout.CENTER, 5, 0);
// Make a small JPanel with the layout and make it non-opaque
JPanel pnlTab = new JPanel (f);
pnlTab.setOpaque (false);
// Create a JButton for adding the tabs
JButton addTab = new JButton ("+");
addTab.setOpaque (false); //
addTab.setBorder (null);
addTab.setContentAreaFilled (false);
addTab.setFocusPainted (false);
addTab.setFocusable (false);
pnlTab.add (addTab);
pane.setTabComponentAt (pane.getTabCount () - 1, pnlTab);
ActionListener listener = new ActionListener () {
@Override
public void actionPerformed (ActionEvent e) {
String title = "Tab " + String.valueOf (pane.getTabCount () - 1);
pane.addTab (title, new JLabel (title));
}
};
addTab.setFocusable (false);
addTab.addActionListener (listener);
pane.setVisible (true);
parent.add (pane);
parent.setSize (new Dimension (400, 200));
parent.setVisible (true);