Example usage for javax.swing JList getSelectedIndex

List of usage examples for javax.swing JList getSelectedIndex

Introduction

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

Prototype

public int getSelectedIndex() 

Source Link

Document

Returns the smallest selected cell index; the selection when only a single item is selected in the list.

Usage

From source file:edu.ku.brc.specify.conversion.ConvertVerifier.java

/**
 * @return/*from ww w  .j av a  2 s  .  c o m*/
 * @throws SQLException
 */
private Pair<String, String> chooseTable() throws SQLException {
    MySQLDMBSUserMgr mgr = new MySQLDMBSUserMgr();

    final Vector<DBNamePair> availOldPairs = new Vector<DBNamePair>();
    final Vector<DBNamePair> availNewPairs = new Vector<DBNamePair>();

    try {
        if (mgr.connectToDBMS(itUsrPwd.first, itUsrPwd.second, hostName)) {
            BasicSQLUtils.setSkipTrackExceptions(true);

            Connection conn = mgr.getConnection();
            Vector<Object[]> dbNames = BasicSQLUtils.query(conn, "show databases");
            for (Object[] row : dbNames) {
                System.err.println("Setting [" + row[0].toString() + "] ");
                conn.setCatalog(row[0].toString());

                boolean isSp5 = false;
                boolean isSp6 = false;

                Vector<Object[]> tables = BasicSQLUtils.query(conn, "show tables");
                for (Object[] tblRow : tables) {
                    if (row[0].toString().equals("debugdb")) {
                        System.err.println(tblRow[0].toString());
                    }
                    if (tblRow[0].toString().equals("usysversion")) {
                        isSp5 = true;
                        break;

                    } else if (tblRow[0].toString().equals("gift")) {
                        isSp6 = true;
                        break;
                    }
                }

                if (isSp5 || isSp6) {
                    String collName = null;
                    Vector<Object[]> tableDesc = BasicSQLUtils.query(conn,
                            "SELECT CollectionName FROM collection");
                    if (tableDesc.size() > 0) {
                        collName = tableDesc.get(0)[0].toString();
                    }

                    if (collName == null) {
                        continue;
                    }

                    if (isSp5) {
                        availOldPairs.add(new DBNamePair(collName, row[0].toString()));
                    } else {
                        availNewPairs.add(new DBNamePair(collName, row[0].toString()));
                    }
                }

                System.err.println("isSp5 [" + isSp5 + "] isSp6 [" + isSp6 + "] ");
            }

            Comparator<Pair<String, String>> comparator = new Comparator<Pair<String, String>>() {
                @Override
                public int compare(Pair<String, String> o1, Pair<String, String> o2) {
                    return o1.second.compareTo(o2.second);
                }
            };
            Collections.sort(availOldPairs, comparator);
            Collections.sort(availNewPairs, comparator);

            mgr.close();
            BasicSQLUtils.setSkipTrackExceptions(false);

            final JList oldlist = new JList(availOldPairs);
            final JList newList = new JList(availNewPairs);
            CellConstraints cc = new CellConstraints();
            PanelBuilder pb = new PanelBuilder(new FormLayout("f:p:g,10px,f:p:g", "p,2px,f:p:g,4px,p"));
            pb.addSeparator("Specify 5 Databases", cc.xy(1, 1));
            pb.add(UIHelper.createScrollPane(oldlist), cc.xy(1, 3));

            pb.addSeparator("Specify 6 Databases", cc.xy(3, 1));
            pb.add(UIHelper.createScrollPane(newList), cc.xy(3, 3));

            ArrayList<String> list = new ArrayList<String>(labels.length);
            for (String s : labels) {
                list.add(s);
            }
            chkPanel = new ToggleButtonChooserPanel<String>(list, Type.Checkbox);
            chkPanel.setUseScrollPane(true);
            chkPanel.createUI();
            //pb.add(chkPanel, cc.xyw(1, 5, 3));

            /*ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                boolean isSelected = chkPanel.getButtons().get(0).isSelected();
                int inx = chkPanel.getSelectedIndex();
                if (inx == 0)
                {
                    Vector<JToggleButton> btns = chkPanel.getButtons();
                    for (int i=1;i<btns.size();i++)
                    {
                        btns.get(i).setEnabled(!isSelected);
                    }
                } 
            }
            };
                    
            chkPanel.getButtons().get(0).addActionListener(al);
            chkPanel.getButtons().get(chkPanel.getButtons().size()-1).addActionListener(al);*/

            ListSelectionListener oldDBListener = new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        DBNamePair pair = (DBNamePair) oldlist.getSelectedValue();
                        if (pair != null) {
                            int index = 0;
                            for (DBNamePair p : availNewPairs) {
                                if (p.second.startsWith(pair.second)) {
                                    final int inx = index;
                                    SwingUtilities.invokeLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            newList.setSelectedIndex(inx);
                                            newList.ensureIndexIsVisible(inx);
                                        }
                                    });
                                }
                                index++;
                            }
                        }
                    }
                }
            };

            oldlist.getSelectionModel().addListSelectionListener(oldDBListener);

            MouseAdapter ma = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);

                    Vector<JToggleButton> btns = chkPanel.getButtons();
                    if (e.getSource() == btns.get(0)) {
                        boolean isSelected = btns.get(0).isSelected();

                        for (int i = 1; i < btns.size(); i++) {
                            btns.get(i).setEnabled(!isSelected);
                        }
                    } else if (e.getSource() == btns.get(btns.size() - 1)) {
                        boolean isSelected = btns.get(btns.size() - 1).isSelected();
                        for (int i = 0; i < btns.size() - 1; i++) {
                            if (i > 0)
                                btns.get(i).setSelected(!isSelected);
                            btns.get(i).setEnabled(!isSelected);
                        }
                    }
                }
            };
            chkPanel.getButtons().get(0).addMouseListener(ma);
            chkPanel.getButtons().get(chkPanel.getButtons().size() - 1).addMouseListener(ma);

            /*ChangeListener cl = new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e)
            {
                Vector<JToggleButton> btns = chkPanel.getButtons();
                if (e.getSource() == btns.get(0))
                {
                    boolean isSelected = btns.get(0).isSelected();
                    System.out.println(isSelected);
                            
                    for (int i=1;i<btns.size();i++)
                    {
                        btns.get(i).setEnabled(!isSelected);
                    }
                } else if (e.getSource() == btns.get(btns.size()-1))
                {
                    boolean isSelected = btns.get(0).isSelected();
                    System.out.println(isSelected);
                            
                    for (int i=0;i<btns.size()-1;i++)
                    {
                        btns.get(i).setEnabled(!isSelected);
                    }
                }
            }
            };
            chkPanel.getButtons().get(0).addChangeListener(cl);
            chkPanel.getButtons().get(chkPanel.getButtons().size()-1).addChangeListener(cl);*/

            pb.setDefaultDialogBorder();

            final CustomDialog dlg = new CustomDialog(null, "Select a DB to Verify", true, pb.getPanel());

            ListSelectionListener lsl = new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        dlg.getOkBtn().setEnabled(oldlist.getSelectedIndex() > -1);
                    }
                }
            };
            oldlist.addListSelectionListener(lsl);
            newList.addListSelectionListener(lsl);

            oldlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            newList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            MouseAdapter listMA = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        dlg.getOkBtn()
                                .setEnabled(oldlist.getSelectedIndex() > -1 && newList.getSelectedIndex() > -1);
                        dlg.getOkBtn().doClick();
                    }
                }
            };
            oldlist.addMouseListener(listMA);
            newList.addMouseListener(listMA);

            dlg.createUI();
            dlg.pack();
            //dlg.setSize(300, 800);
            dlg.pack();
            dlg.setVisible(true);
            if (dlg.isCancelled()) {
                return null;
            }

            DBNamePair oldPair = (DBNamePair) oldlist.getSelectedValue();
            namePairToConvert = (DBNamePair) newList.getSelectedValue();
            namePairToConvert.first = oldPair.second;
            return namePairToConvert;
        }
    } catch (Exception ex) {

    }
    return null;
}

From source file:src.gui.ItSIMPLE.java

/**
 * This method initializes movieMakerToolBar
 *
 * @return javax.swing.JToolBar//from www.j  a  va  2  s . c  o m
 */
private JToolBar getMovieMakerToolBar() {

    JToolBar movieMakerToolBar = new JToolBar();
    movieMakerToolBar.setRollover(true);

    // create the buttons

    // generate movie
    JButton generateMovieButton = new JButton("Generate Movie",
            new ImageIcon("resources/images/makemovie.png"));
    generateMovieButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {

            // the thread is created so the status bar can be refreshed
            new Thread() {
                public void run() {
                    ItTreeNode selectedNode = (ItTreeNode) problemsPlanTree.getLastSelectedPathComponent();
                    if (selectedNode != null && selectedNode.getLevel() == 3) {

                        Element problem = selectedNode.getData();
                        movie = PlanSimulator.getMovie(xmlPlan, problem);
                        //XMLUtilities.printXML(xmlPlan);
                        //XMLUtilities.printXML(movie);

                        //IN CASE WE WANT TO RUN THE METRICS WITH SIMULATION
                        //Element domain = problem.getParentElement().getParentElement();
                        //Element metrics = PlanSimulator.createMetricsNode(problem, domain);
                        //if(metrics.getChildren().size() > 0){
                        //    PlanSimulator.createMetricDatasets(metrics, xmlPlan, problem, domain, movie);
                        //}
                        //XMLUtilities.printXML(metrics);

                        PlanNavigationList.getInstance().setList(xmlPlan, movie,
                                problem.getParentElement().getParentElement(),
                                (JPanel) movieMakerSplitPane.getLeftComponent(),
                                (JPanel) movieMakerSplitPane.getRightComponent());
                        replanMenu.setEnabled(true);
                    }
                }
            }.start();
        }
    });
    movieMakerToolBar.add(generateMovieButton);

    // back
    JButton backButton = new JButton("Backward", new ImageIcon("resources/images/backward.png"));
    backButton.setToolTipText("Backward");
    backButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JList navigationList = PlanNavigationList.getInstance().getPlanList();
            final int selected = navigationList.getSelectedIndex();
            if (selected > 0) {
                navigationList.setSelectedIndex(selected - 1);
            }
        }

    });
    movieMakerToolBar.add(backButton);

    // forward
    JButton forwardButton = new JButton("Forward", new ImageIcon("resources/images/forward.png"));
    forwardButton.setToolTipText("Forward");
    forwardButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JList navigationList = PlanNavigationList.getInstance().getPlanList();
            final int selected = navigationList.getSelectedIndex();
            if (selected > -1 && selected < navigationList.getModel().getSize() - 1) {
                navigationList.setSelectedIndex(selected + 1);
            }
        }

    });
    movieMakerToolBar.add(forwardButton);

    movieMakerToolBar.addSeparator();

    // zoom in
    //JButton zoomInButton = new JButton("Zoom In",new ImageIcon("resources/images/zoomIN.png"));
    JButton zoomInButton = new JButton(new ImageIcon("resources/images/zoomIN.png"));
    zoomInButton.setToolTipText("Zoom In");
    zoomInButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {

            try {
                ItGraph leftGraph = (ItGraph) ((JScrollPane) ((JPanel) movieMakerSplitPane.getLeftComponent())
                        .getComponent(0)).getViewport().getView();
                leftGraph.setScale(leftGraph.getScale() * 1.25);

                ItGraph rightGraph = (ItGraph) ((JScrollPane) ((JPanel) movieMakerSplitPane.getRightComponent())
                        .getComponent(0)).getViewport().getView();
                rightGraph.setScale(rightGraph.getScale() * 1.25);
            } catch (Exception e1) {
            }

        }
    });
    movieMakerToolBar.add(zoomInButton);

    // zoom out
    //JButton zoomOutButton = new JButton("Zoom Out",new ImageIcon("resources/images/zoomOUT.png"));
    JButton zoomOutButton = new JButton(new ImageIcon("resources/images/zoomOUT.png"));
    zoomOutButton.setToolTipText("Zoom Out");
    zoomOutButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {

            try {
                ItGraph leftGraph = (ItGraph) ((JScrollPane) ((JPanel) movieMakerSplitPane.getLeftComponent())
                        .getComponent(0)).getViewport().getView();
                leftGraph.setScale(leftGraph.getScale() * 0.8);

                ItGraph rightGraph = (ItGraph) ((JScrollPane) ((JPanel) movieMakerSplitPane.getRightComponent())
                        .getComponent(0)).getViewport().getView();
                rightGraph.setScale(rightGraph.getScale() * 0.8);
            } catch (Exception e1) {
            }

        }
    });
    movieMakerToolBar.add(zoomOutButton);

    // 1:1
    JButton oneToOneScaleButton = new JButton("1:1");
    oneToOneScaleButton.setToolTipText("1:1");
    oneToOneScaleButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {

            try {
                ItGraph leftGraph = (ItGraph) ((JScrollPane) ((JPanel) movieMakerSplitPane.getLeftComponent())
                        .getComponent(0)).getViewport().getView();
                leftGraph.setScale(1.0);

                ItGraph rightGraph = (ItGraph) ((JScrollPane) ((JPanel) movieMakerSplitPane.getRightComponent())
                        .getComponent(0)).getViewport().getView();
                rightGraph.setScale(1.0);
            } catch (Exception e1) {
            }

        }
    });
    movieMakerToolBar.add(oneToOneScaleButton);

    movieMakerToolBar.addSeparator();

    // edit state
    JButton editStateButton = new JButton("Edit", new ImageIcon("resources/images/edit.png"));
    editStateButton.setToolTipText("Edit current state");
    editStateButton.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // take the current state
            Element currentState = PlanNavigationList.getInstance().getCurrentState();

            ItTreeNode problemNode = (ItTreeNode) problemsPlanTree.getLastSelectedPathComponent();
            ItTreeNode domainNode = (ItTreeNode) problemNode.getParent();
            ItTreeNode projectNode = (ItTreeNode) domainNode.getParent();

            // the object diagram must have parent
            //add the current state to the domain
            Element currentStateClone = (Element) currentState.clone();
            problemNode.getData().getChild("objectDiagrams").addContent(currentStateClone);

            graphTabbedPane.openEditStateTab(currentStateClone, domainNode.getData(), projectNode.getData());

            //mainTabbedPane.setSelectedIndex(0);
            CardLayout cl = (CardLayout) (mainTabbedPane.getLayout());
            cl.show(mainTabbedPane, "UML");
        }
    });
    movieMakerToolBar.add(editStateButton);

    movieMakerToolBar.addSeparator();
    // plan evaluation

    /*
    JButton planEvaluationButton = new JButton("Evaluate Plan", new ImageIcon("resources/images/eval.png"));
    planEvaluationButton.setToolTipText("<html>Generate a plan evaluation in the planReport. <br> This is restricted to non-time-based domain only.</html>");
    planEvaluationButton.addActionListener(new java.awt.event.ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //RUN plan evaluation
    generateEvaluatioReport.actionPerformed(e);
    }
    });
    movieMakerToolBar.add( planEvaluationButton);
     */

    // virtual reality

    JButton virtualRealityButton = new JButton("Virtual Prototyping",
            new ImageIcon("resources/images/virtualprototype.png"));
    virtualRealityButton.setToolTipText("Generate virtual prototype files");
    virtualRealityButton.addActionListener(new java.awt.event.ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ItTreeNode problemNode = (ItTreeNode) problemsPlanTree.getLastSelectedPathComponent();
            ItTreeNode domainNode = (ItTreeNode) problemNode.getParent();
            //ItTreeNode projectNode = (ItTreeNode)domainNode.getParent();
            try {

                VirtualPrototypingBlender.generatePrototypeFiles(domainNode.getData(), problemNode.getData(),
                        xmlPlan);
                //VirtualRealityRobotNavigationDomain.generateBackgroundFile(problemNode.getData(), xmlPlan);
            } catch (IOException ex) {
                Logger.getLogger(ItSIMPLE.class.getName()).log(Level.SEVERE, null, ex);
            }
            //VirtualRealityRobotNavigationDomain.generateBackgroundFile(problemNode.getData(), xmlPlan);
        }
    });
    movieMakerToolBar.add(virtualRealityButton);

    return movieMakerToolBar;
}

From source file:org.genedb.jogra.plugins.TermRationaliser.java

/**
 * PRIVATE HELPER METHODS//w  w w.  j  av  a2s. c o  m
 */

/* 
 * Synchronise to the item selected in sourcelist 
 */
private void synchroniseLists(JList sourceList, JList targetList) {
    Term term = (Term) sourceList.getSelectedValue();
    targetList.setSelectedValue(term, true);
    targetList.ensureIndexIsVisible(targetList.getSelectedIndex());
}

From source file:org.parosproxy.paros.extension.history.PopupMenuExportMessage.java

public boolean isEnableForComponent(Component invoker) {

    if (invoker.getName() != null && invoker.getName().equals("ListLog")) {
        try {//from   w w  w  .  j av  a 2 s.  c o  m
            JList list = (JList) invoker;
            if (list.getSelectedIndex() >= 0) {
                this.setEnabled(true);
            } else {
                this.setEnabled(false);
            }
        } catch (Exception e) {
            // ZAP: Log exceptions
            log.warn(e.getMessage(), e);
        }
        return true;

    }
    return false;
}

From source file:org.yccheok.jstock.gui.IndicatorPanel.java

private void Install() {
    if (false == promptToSaveSignificantEdits()) {
        return;/*w  w  w.j  a v a  2  s .  c  o  m*/
    }
    final Wizard wizard = this.getWizardDialog();
    int ret = wizard.showModalDialog(650, 400, true);
    if (ret != Wizard.FINISH_RETURN_CODE) {
        final JList jList = this.getCurrentActiveJList();
        // Although cancel button is pressed, possible some indicators had
        // already installed from JStock server.
        //
        // Refresh JList after install from JStock server.
        this.syncJListWithIndicatorProjectManager(jList, this.getCurrentActiveIndicatorProjectManager());
        if (jList.getModel().getSize() > 0 && jList.getSelectedIndex() < 0) {
            jList.setSelectedIndex(0);
        }
        return;
    }
    final WizardModel wizardModel = wizard.getModel();
    final WizardPanelDescriptor wizardSelectInstallIndicatorMethodDescriptor = wizardModel
            .getPanelDescriptor(WizardSelectInstallIndicatorMethodDescriptor.IDENTIFIER);
    final WizardSelectInstallIndicatorMethodJPanel wizardSelectInstallIndicatorMethodJPanel = (WizardSelectInstallIndicatorMethodJPanel) wizardSelectInstallIndicatorMethodDescriptor
            .getPanelComponent();
    if (wizardSelectInstallIndicatorMethodJPanel.isLocalFileSelected()) {
        final File file = wizardSelectInstallIndicatorMethodJPanel.getSelectedFile();
        assert (file != null);
        if (this.jTabbedPane1.getSelectedIndex() == 0) {
            this.alertIndicatorInstall(file);
        } else if (this.jTabbedPane1.getSelectedIndex() == 1) {
            this.moduleIndicatorInstall(file);
        }
    } else {
        final JList jList = this.getCurrentActiveJList();
        // Refresh JList after install from JStock server.
        this.syncJListWithIndicatorProjectManager(jList, this.getCurrentActiveIndicatorProjectManager());
        if (jList.getModel().getSize() > 0 && jList.getSelectedIndex() < 0) {
            jList.setSelectedIndex(0);
        }
    }
}