Example usage for javax.swing JTabbedPane getModel

List of usage examples for javax.swing JTabbedPane getModel

Introduction

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

Prototype

public SingleSelectionModel getModel() 

Source Link

Document

Returns the model associated with this tabbedpane.

Usage

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/*from w w  w  .  j  a  va2s. c  o  m*/
        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) {
    JTabbedPane tabbedPane = new JTabbedPane();
    for (int i = 0; i < 5; i++) {
        tabbedPane.add("Tab " + i, new JLabel("Label " + i, SwingConstants.CENTER));
    }/*  w  w  w.  j a  va 2s . com*/

    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:com.t3.client.ui.AddResourceDialog.java

public void initTabPane() {

    final JTabbedPane tabPane = (JTabbedPane) getComponent("tabPane");

    tabPane.getModel().addChangeListener(new ChangeListener() {
        @Override//from w  w w .  j  a v  a2  s.  c  o  m
        public void stateChanged(ChangeEvent e) {
            // Hmmm, this is fragile (breaks if the order changes) rethink this later
            switch (tabPane.getSelectedIndex()) {
            case 0:
                model.tab = Tab.LOCAL;
                break;
            case 1:
                model.tab = Tab.WEB;
                break;
            case 2:
                model.tab = Tab.TABLETOPTOOL_SITE;
                downloadLibraryList();
                break;
            }
        }
    });
}

From source file:net.rptools.maptool.client.ui.AddResourceDialog.java

public void initTabPane() {

    final JTabbedPane tabPane = (JTabbedPane) getComponent("tabPane");

    tabPane.getModel().addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            // Hmmm, this is fragile (breaks if the order changes) rethink this later
            switch (tabPane.getSelectedIndex()) {
            case 0:
                model.tab = Tab.LOCAL;/* w  ww.j  a  v a  2 s . c o m*/
                break;
            case 1:
                model.tab = Tab.WEB;
                break;
            case 2:
                model.tab = Tab.RPTOOLS;
                downloadLibraryList();
                break;
            }
        }
    });
}

From source file:Main.java

public Main() {
    Icon icon = UIManager.getIcon("html.pendingImage");
    JTabbedPane jtb = new JTabbedPane();

    JPanel jplInnerPanel1 = createInnerPanel("Tab 1: Tooltip and Icon");
    jtb.addTab("One", icon, jplInnerPanel1, "Tab 1");
    jtb.setSelectedIndex(0);/* www  .  j  a  va2s  .  co m*/

    JPanel jplInnerPanel2 = createInnerPanel("Tab 2: Icon only");
    jtb.addTab("Two", icon, jplInnerPanel2);

    JPanel jplInnerPanel3 = createInnerPanel("Tab 3: Tooltip and Icon");
    jtb.addTab("Three", icon, jplInnerPanel3, "Tab 3");

    JPanel jplInnerPanel4 = createInnerPanel("Tab 4: Text only");
    jtb.addTab("Four", jplInnerPanel4);

    menu.add(new JMenuItem("Item 1"));
    menu.add(new JMenuItem("Item 2"));

    JLabel tab4Label = new JLabel();
    tab4Label.setText("Four");
    jtb.setTabComponentAt(3, tab4Label);
    tab4Label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {
            jtb.getModel().setSelectedIndex(3);
            if (e.isPopupTrigger()) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });
    setLayout(new GridLayout());
    add(jtb);
}