Example usage for javax.swing JTabbedPane addTab

List of usage examples for javax.swing JTabbedPane addTab

Introduction

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

Prototype

public void addTab(String title, Component component) 

Source Link

Document

Adds a component represented by a title and no icon.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.addTab("Tab Label", new JButton("button"));
    int index = pane.getTabCount() - 1;

    int keycode = KeyEvent.VK_L;
    pane.setMnemonicAt(index, keycode);/* w w w . j  a va  2s. c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    pane.addTab("Tab Label", new JButton("Button"));
    // Get index of the new tab
    int index = pane.getTabCount() - 1;

    // Determine whether the tab is enabled
    boolean enabled = pane.isEnabledAt(index);

    // Disable the tab
    pane.setEnabledAt(index, false);/*from w  w w  .ja v  a  2 s .co m*/
}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tp = new JTabbedPane();
    tp.addTab("Dates", new JPanel());
    tp.addTab("Deliveries", new JPanel());
    tp.addTab("Exports", new JPanel());

    tp.setTabComponentAt(0, new JLabel("Dates"));
    tp.setTabComponentAt(1, new JLabel("Deliveries"));
    tp.setTabComponentAt(2, new JLabel("Exports"));

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(tp);/*from   w  w w .  ja  va2 s.c  o m*/
    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 ww  w .j  ava  2s.c o  m
    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) {
    JTabbedPane tab = new JTabbedPane();
    tab.addTab("New tab1", new JLabel("1"));
    tab.addTab("New Tab2", new JLabel("2"));
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JLayer<JComponent>(tab, new TopRightCornerLabelLayerUI()));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(p);// ww  w.  j ava  2  s  .  c  o m
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel ui = new JPanel(new BorderLayout(1, 1));
    JTabbedPane jtp = new JTabbedPane(JTabbedPane.LEFT);
    jtp.addTab("Apple", new JLabel("Apple"));
    jtp.addTab("Banana", new JLabel("Banana"));
    jtp.addTab("Cherries", new JLabel("Cherries"));
    jtp.addTab("Grapes", new JLabel("Grapes"));
    ui.add(jtp, BorderLayout.CENTER);
    jtp.setPreferredSize(new Dimension(200, 200));
    jtp.addChangeListener(e -> {/*from w w  w. j a  v a  2 s  . co m*/
        if (e.getSource() instanceof JTabbedPane) {
            JTabbedPane pane = (JTabbedPane) e.getSource();
            System.out.println("Selected paneNo : " + pane.getSelectedIndex());
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTabbedPane jtp = new JTabbedPane();
    jtp.addTab("Main1", createPanel());
    jtp.addTab("Main2", createPanel());
    f.add(jtp);/*ww  w .j  a v  a  2s .co  m*/
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel() {
        @Override//from  w  ww  .j  a va 2s .com
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    };
    panel.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            System.out.println("Resized to " + e.getComponent().getSize());
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            System.out.println("Moved to " + e.getComponent().getLocation());
        }
    });
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("test", panel);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(tabbedPane);
    frame.pack();

    frame.setVisible(true);
}

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 . j  av  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) {
    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/*from   w  w  w. j  av a  2 s.  c om*/
            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);
}