Example usage for javax.swing JTabbedPane getSelectedIndex

List of usage examples for javax.swing JTabbedPane getSelectedIndex

Introduction

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

Prototype

@Transient
public int getSelectedIndex() 

Source Link

Document

Returns the currently selected index for this tabbedpane.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    // Get the index of the currently selected tab
    int selIndex = pane.getSelectedIndex();

    // Select the last tab
    selIndex = pane.getTabCount() - 1;/*from  ww w  .j  av a 2  s  .c o m*/
    pane.setSelectedIndex(selIndex);
}

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  ww  .  j a v a  2 s .c om*/
        if (e.getSource() instanceof JTabbedPane) {
            JTabbedPane pane = (JTabbedPane) e.getSource();
            System.out.println("Selected paneNo : " + pane.getSelectedIndex());
        }
    });
}

From source file:TabSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Tabbed Pane Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();

    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    for (int i = 0, n = titles.length; i < n; i++) {
        add(tabbedPane, titles[i]);/*from   www .  jav a 2  s . c  o m*/
    }

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
            int index = sourceTabbedPane.getSelectedIndex();
            System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
        }
    };
    tabbedPane.addChangeListener(changeListener);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    String titles[] = { "A", "B", "C", "D", "E", "F" };
    int mnemonic[] = { KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E,
            KeyEvent.VK_F };/*from w ww . j  a v a  2  s  .c  o m*/
    for (int i = 0, n = titles.length; i < n; i++) {
        add(tabbedPane, titles[i], mnemonic[i]);
    }

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
            int index = sourceTabbedPane.getSelectedIndex();
            System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
        }
    };
    tabbedPane.addChangeListener(changeListener);

    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);

}

From source file:TabbedPaneSample.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "Tabbed Pane Sample" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane tabbedPane = new JTabbedPane();
    String titles[] = { "General", "Security", "Content", "Connection", "Programs", "Advanced" };
    for (int i = 0, n = titles.length; i < n; i++) {
        add(tabbedPane, titles[i]);/*w  ww  . j  av  a  2s .com*/
    }

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
            int index = sourceTabbedPane.getSelectedIndex();
            System.out.println("Tab changed to: " + sourceTabbedPane.getTitleAt(index));
        }
    };
    tabbedPane.addChangeListener(changeListener);

    Container contentPane = frame.getContentPane();
    contentPane.add(tabbedPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    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 .ja v  a 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[] argv) throws Exception {

    JTabbedPane pane = new JTabbedPane();

    pane.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            JTabbedPane pane = (JTabbedPane) evt.getSource();

            int sel = pane.getSelectedIndex();
            System.out.println(sel);
        }/*ww  w. j  a va2 s  . com*/
    });
}

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//ww  w. j  a v  a2 s. 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

private void init() {
    this.setLayout(new BorderLayout());
    JPanel topPanel = new JPanel();
    final JLabel topLabel = new JLabel("North");
    topPanel.add(topLabel);/*from w  w  w  .  java2s. c  o m*/
    this.add(topPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel firstTabCont = new JPanel();
    firstTabCont.add(new JLabel("First"));
    tabbedPane.addTab("First", firstTabCont);

    JPanel secondTabCont = new JPanel();
    secondTabCont.add(new JLabel("Second"));
    tabbedPane.addTab("Second", secondTabCont);

    this.add(tabbedPane, BorderLayout.CENTER);

    JPanel bottomPanel = new JPanel();
    final JLabel bottomLabel = new JLabel("South");
    bottomPanel.add(bottomLabel);
    this.add(bottomPanel, BorderLayout.SOUTH);

    tabbedPane.addChangeListener(evt -> {
        JTabbedPane pane = (JTabbedPane) evt.getSource();
        int selectedIndex = pane.getSelectedIndex();
        if (selectedIndex == 0) {
            topLabel.setText("");
            topLabel.setText("Hi");

            bottomLabel.setText("");
            bottomLabel.setText("Bye");
        } else {
            topLabel.setText("");
            topLabel.setText("Bye");

            bottomLabel.setText("");
            bottomLabel.setText("Hi");
        }

    });
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

From source file:com.mc.printer.model.panel.task.BuildModelTask.java

@Override
public Object doBackgrounp() {
    javax.swing.JTabbedPane tabs = AutoPrinterApp.getMainView().getMainWorkPanel();
    if (tabs != null) {
        if (tabs.getSelectedIndex() >= 0) {
            Component selectedcom = tabs.getSelectedComponent();
            if (selectedcom instanceof CanvasWork) {
                CanvasWork selectPanel = (CanvasWork) selectedcom;
                FormBeans beansForm = selectPanel.getBeansForm();

                /**/
                if (beansForm.getHeightcm() == 0 || beansForm.getWidthcm() == 0) {
                    int res = BaseMessage.CONFIRM(
                            "?\r\nA4.");
                    if (res != JOptionPane.OK_OPTION) {
                        return null;
                    }//from w ww  .  j  av a 2  s.c o  m
                }

                String imagePath = beansForm.getImgpath();
                HashMap<String, ComponentBean> parameterMap = selectPanel.getSavedForms();
                if (parameterMap.size() > 0) {
                    Set set = parameterMap.keySet();
                    boolean foundKey = false;
                    //???
                    List<ComponentBean> beans = new ArrayList();
                    Iterator it = set.iterator();
                    while (it.hasNext()) {
                        ComponentBean com = parameterMap.get(it.next().toString());
                        beans.add(com);
                        if (com.isIskey()) {
                            foundKey = true;
                        }
                    }
                    beansForm.setElements(beans);
                    //
                    //                        if (!foundKey) {
                    //                            BaseMessage.ERROR("PRIMARY KEY.");
                    //                            return null;
                    //                        }

                    BaseFileChoose fileChoose = new BaseFileChoose("?",
                            new String[] { Constants.MODEL_SUFFIX }, AutoPrinterApp.getMainFrame());
                    String selectedPath = fileChoose.showSaveDialog();
                    if (!selectedPath.trim().equals("")) {

                        //get formname
                        selectedPath = selectedPath + File.separator + beansForm.getFormname();
                        //String time=DateHelper.format(new Date(), "yyyyMMddHHmmss");
                        String finalZip = selectedPath;
                        if (!selectedPath.endsWith("." + Constants.MODEL_SUFFIX)) {
                            finalZip = selectedPath + "." + Constants.MODEL_SUFFIX;
                        }

                        String tempDir = selectedPath + File.separator + Constants.MODEL_TEMP_DIR;

                        File imageFile = new File(imagePath);

                        String xmlPath = tempDir + File.separator + imageFile.getName() + ".xml";
                        XMLHelper helper = new XMLHelper(xmlPath, beansForm);
                        try {
                            helper.write();
                        } catch (JAXBException ex) {
                            ex.printStackTrace();
                            logger.error(ex.getMessage());
                            return ex;
                        }

                        File paramFile = new File(xmlPath);
                        if (paramFile.isFile() && paramFile.exists()) {
                            try {
                                FileUtils.copyFileToDirectory(imageFile, new File(tempDir));
                                ZipHelper.createZip(tempDir, finalZip);
                                return "??.\r\n" + finalZip;
                            } catch (IOException ex) {
                                ex.printStackTrace();
                                logger.error(ex.getMessage());
                                return ex;
                            } finally {
                                try {
                                    FileUtils.deleteDirectory(new File(selectedPath));
                                } catch (IOException ex) {
                                    ex.printStackTrace();
                                    logger.error(ex.getMessage());
                                    return ex;
                                }
                            }
                        }
                    }
                } else {
                    return "??.";
                }
            } else {
                return "??????.";
            }
        } else {
            return ".";
        }
    } else {
        return ".";
    }
    return null;
}