Example usage for javax.swing JTabbedPane JTabbedPane

List of usage examples for javax.swing JTabbedPane JTabbedPane

Introduction

In this page you can find the example usage for javax.swing JTabbedPane JTabbedPane.

Prototype

public JTabbedPane() 

Source Link

Document

Creates an empty TabbedPane with a default tab placement of JTabbedPane.TOP.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tabbedPane = new JTabbedPane();
    for (int i = 0; i < 5; i++) {
        tabbedPane.add("Tab " + i, new JLabel("Label " + i, SwingConstants.CENTER));
    }//from ww w .j a  va2 s. c  o  m

    tabbedPane.getModel().addChangeListener(e -> {
        JLabel label = (JLabel) tabbedPane.getSelectedComponent();
        System.out.println(label.getText());
    });

    tabbedPane.setPreferredSize(new Dimension(500, 300));

    JFrame frame = new JFrame();
    frame.getContentPane().add(tabbedPane);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setSize(800, 600);//from   w w w.  j  a v  a  2  s  .  com
    f.setLocationRelativeTo(null);

    JTabbedPane p = new JTabbedPane();
    p.addTab("T1", new JPanel());
    p.addTab("T2", new JPanel());
    p.addTab("T3", new JPanel());

    p.addChangeListener(e -> {
        System.out.println("" + p.getSelectedIndex());
        // Index starts at 0, so Index 2 = Tab3
        if (p.getSelectedIndex() == 2) {
            // do your stuff on Tab 3
        }
    });
    f.add(p);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.setUI(new SpacedTabbedPaneUI());
    pane.addTab("One", new JPanel());
    pane.addTab("Two", new JPanel());
    pane.addTab("Threeeeeee", new JPanel());
    pane.addTab("Four", new JPanel());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(pane);//from  w w w .j a  v  a  2  s.  com
    frame.setSize(500, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static final void main(String[] args) {
    JFrame frame = new JFrame();
    JTabbedPane tabbedPane = new JTabbedPane();

    frame.add(tabbedPane);/*from w w  w.  j  a v  a 2 s.  c  o  m*/

    JButton addButton = new JButton("Add tab");
    addButton.addActionListener(e -> {
        JPanel newTabComponent = new JPanel();
        int tabCount = tabbedPane.getTabCount();
        newTabComponent.add(new JLabel("I'm tab " + tabCount));
        tabbedPane.addTab("Tab " + tabCount, newTabComponent);
    });
    frame.add(addButton, BorderLayout.SOUTH);
    addButton.doClick();

    frame.setSize(800, 300);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
}

From source file:ProxyTester.java

public static void main(String[] args) {
    JTabbedPane tabbedPane = new JTabbedPane();
    for (String name : imageNames) {
        JLabel label = new JLabel(new ImageProxy(name));
        tabbedPane.add(name, label);/*from  ww w  . j  a  va2 s. c o m*/
    }

    JFrame frame = new JFrame();
    frame.add(tabbedPane);

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JTabbedPane jTabbedPane = new JTabbedPane();
    jTabbedPane.addTab("Red", new JLabel("Roses"));
    jTabbedPane.addTab("Blue", new JLabel("Skies"));
    jTabbedPane.addTab("Green", new JLabel("Grass"));

    for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
        final JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i));

        tabComponent.addMouseMotionListener(new MouseMotionAdapter() {
            @Override/* w  w w . j  a  v  a 2  s .  c  o m*/
            public void mouseDragged(MouseEvent e) {
                System.out.println("tabComponent dragging");
            }
        });

        tabComponent.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                int x = tabComponent.getLocationOnScreen().x - jTabbedPane.getLocationOnScreen().x;
                int y = tabComponent.getLocationOnScreen().y - jTabbedPane.getLocationOnScreen().y;
                MouseEvent me = new MouseEvent((JLabel) e.getSource(), e.getID(), e.getWhen(), e.getModifiers(),
                        x, y, e.getLocationOnScreen().x, e.getLocationOnScreen().y, e.getClickCount(),
                        e.isPopupTrigger(), e.getButton());
                jTabbedPane.getMouseListeners()[0].mousePressed(me);
                System.out.println("tabComponent mousePressed e=" + e);
            }
        });
        jTabbedPane.setTabComponentAt(i, tabComponent);
    }
    JFrame jFrame = new JFrame();
    jFrame.add(jTabbedPane);
    jFrame.setSize(400, 500);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:TabbedPaneTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JTabbedPane");
    final Container contentPane = frame.getContentPane();
    JTabbedPane jtp = new JTabbedPane();
    contentPane.add(jtp, BorderLayout.CENTER);
    for (int i = 0; i < 5; i++) {
        JButton button = new JButton("Card " + i);
        jtp.add("Btn " + i, button);
    }//from w  w w . j a  v  a  2s  .  co m
    frame.setSize(300, 200);
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tab = new JTabbedPane();
    tab.addTab("A", new JPanel());
    tab.addTab("+", new JPanel());
    tab.getModel().addChangeListener(new ChangeListener() {
        private int lastSelected;
        private boolean ignore = false;

        @Override/*w w  w  .  jav a  2s .  com*/
        public void stateChanged(ChangeEvent e) {
            if (!ignore) {
                ignore = true;
                try {
                    int selected = tab.getSelectedIndex();
                    String title = tab.getTitleAt(selected);
                    if ("+".equals(title)) {
                        JPanel pane = new JPanel();
                        tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1);
                        tab.setSelectedComponent(pane);
                    } else {
                        lastSelected = selected;
                    }
                } finally {
                    ignore = false;
                }
            }
        }
    });
    final JButton btn = new JButton("Add");
    btn.addActionListener(e -> System.out.println(tab.getTabCount()));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(tab);
    frame.add(btn, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setLayout(new OverlayLayout(panel));

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.add("1", new JTextField("one"));
    tabbedPane.add("2", new JTextField("two"));
    tabbedPane.setAlignmentX(1.0f);/*from   w ww .j av  a 2 s  .c om*/
    tabbedPane.setAlignmentY(0.0f);

    JCheckBox checkBox = new JCheckBox("Add tab");
    checkBox.setOpaque(false);
    checkBox.setAlignmentX(1.0f);
    checkBox.setAlignmentY(0.0f);

    panel.add(checkBox);
    panel.add(tabbedPane);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.setSize(400, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setLayout(new OverlayLayout(panel));

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.add("1", new JTextField("one"));
    tabbedPane.add("2", new JTextField("two"));
    tabbedPane.setAlignmentX(1.0f);//from   w  w  w  .j a v a  2s  .  c  om
    tabbedPane.setAlignmentY(0.0f);

    JCheckBox checkBox = new JCheckBox("Check Me");
    checkBox.setOpaque(false);
    checkBox.setAlignmentX(1.0f);
    checkBox.setAlignmentY(0.0f);

    panel.add(checkBox);
    panel.add(tabbedPane);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.setLocationByPlatform(true);
    frame.setSize(400, 100);
    frame.setVisible(true);
}