Java JTabbedPane extend

Description

Java JTabbedPane extend

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

class TabPlacementAction implements ActionListener {
  private String action;
  private JTabbedPane tabbedPane;

  public TabPlacementAction(JTabbedPane tabbedPane, String action) {
    this.action = action;
    this.tabbedPane = tabbedPane;
  }/*ww  w  . j  av a 2s .c om*/

  @Override
  public void actionPerformed(ActionEvent e) {
    if ("left".equalsIgnoreCase(action)) {
      tabbedPane.setTabPlacement(JTabbedPane.LEFT);
    } else if ("right".equalsIgnoreCase(action)) {
      tabbedPane.setTabPlacement(JTabbedPane.RIGHT);
    } else if ("top".equalsIgnoreCase(action)) {
      tabbedPane.setTabPlacement(JTabbedPane.TOP);
    } else if ("bottom".equalsIgnoreCase(action)) {
      tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
    }
  }
}

public class Main extends JTabbedPane {

  public Main() {

    for (int i = 0; i < 10; i++) {
      JPanel tabPane = new JPanel();
      tabPane.add(new JLabel("Tab" + i));
      addTab("Tab " + i, null, tabPane, "Tab" + i);
    }
    setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  }

  public static void main(String[] args) {
    Main m = new Main();
    
    // create frame for Main
    JFrame frame = new JFrame("java2s.com");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    
    JMenu menu = new JMenu("Tabbed Panels");
    JMenuItem left = new JMenuItem("Left", null);
    left.addActionListener(new TabPlacementAction(m, "left"));
    menu.add(left);

    JMenuItem right = new JMenuItem("Right", null);
    right.addActionListener(new TabPlacementAction(m, "right"));
    menu.add(right);

    JMenuItem top = new JMenuItem("Top", null);
    top.addActionListener(new TabPlacementAction(m, "top"));
    menu.add(top);
    
    JMenuItem bottom = new JMenuItem("Bottom", null);
    bottom.addActionListener(new TabPlacementAction(m, "bottom"));
    menu.add(bottom);
    
    menuBar.add(menu);
    
    frame.setJMenuBar(menuBar);
    

    frame.add(m);
    frame.setSize(300, 210);
    frame.setVisible(true);
  }
}



PreviousNext

Related