Example usage for javax.swing JList ensureIndexIsVisible

List of usage examples for javax.swing JList ensureIndexIsVisible

Introduction

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

Prototype

public void ensureIndexIsVisible(int index) 

Source Link

Document

Scrolls the list within an enclosing viewport to make the specified cell completely visible.

Usage

From source file:Main.java

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

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);/*  w  w  w .ja  va 2  s  . com*/
    for (int i = 0; i < 100; i++) {
        model.addElement(Integer.toString(i));
    }
    JList jlist2 = new JList(model);

    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

    frame.setSize(300, 350);
    frame.setVisible(true);

    jlist2.ensureIndexIsVisible(50);
}

From source file:SizingSamples.java

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

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);//from ww w. java 2  s .  c  om
    for (int i = 0; i < 100; i++) {
        model.addElement(Integer.toString(i));
    }
    JList jlist2 = new JList(model);
    ListCellRenderer renderer = new FocusedTitleListCellRenderer();
    jlist2.setCellRenderer(renderer);

    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

    frame.setSize(300, 350);
    frame.setVisible(true);

    jlist2.ensureIndexIsVisible(50);
}

From source file:Main.java

@Override
public void valueChanged(final ListSelectionEvent e) {
    JList list = (JList) e.getSource();
    list.ensureIndexIsVisible(list.getSelectedIndex());
}

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

/**
 * @return/*from   www .ja  va 2s. co 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:org.genedb.jogra.plugins.TermRationaliser.java

/**
 * PRIVATE HELPER METHODS// w  w w  . j a  v a  2s. c om
 */

/* 
 * 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.isatools.isacreatorconfigurator.configui.DataEntryPanel.java

private void swapElements(JList list, DefaultListModel model, int a, int b) {
    Object o1 = model.getElementAt(a);
    Object o2 = model.getElementAt(b);
    model.set(a, o2);//from   w  ww  . jav  a  2s  .  co m
    model.set(b, o1);

    // Swap elements in list too
    List<Display> fields = tableFields.get(getCurrentlySelectedTable());

    Display e1 = fields.get(a);
    Display e2 = fields.get(b);

    fields.set(a, e2);
    fields.set(b, e1);

    list.setSelectedIndex(b);
    list.ensureIndexIsVisible(b);
}

From source file:util.ui.UiUtilities.java

/**
 * Moves Selected Items from one List to another
 *
 * @param fromList//from   w  w  w. j a v a2  s.  c o m
 *          Move from this List
 * @param toList
 *          Move into this List
 * @return Moved Elements
 */
public static Object[] moveSelectedItems(JList fromList, JList toList) {
    DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
    DefaultListModel toModel = (DefaultListModel) toList.getModel();

    // get the selection
    int[] selection = fromList.getSelectedIndices();

    if (selection.length == 0) {
        return new Object[] {};
    }

    Object[] objects = new Object[selection.length];
    for (int i = 0; i < selection.length; i++) {
        objects[i] = fromModel.getElementAt(selection[i]);
    }

    // get the target insertion position
    int targetPos = toList.getMaxSelectionIndex();
    if (targetPos == -1) {
        targetPos = toModel.getSize();
    } else {
        targetPos++;
    }

    // suppress updates on both lists
    if (selection.length >= 5) {
        fromList.setModel(new DefaultListModel());
        toList.setModel(new DefaultListModel());
    }

    // move the elements
    for (int i = selection.length - 1; i >= 0; i--) {
        Object value = fromModel.remove(selection[i]);
        toModel.add(targetPos, value);
    }

    if (selection.length >= 5) {
        fromList.setModel(fromModel);
        toList.setModel(toModel);
    }

    // change selection of the fromList
    if (fromModel.getSize() > 0) {
        int newSelection = selection[0];
        if (newSelection >= fromModel.getSize()) {
            newSelection = fromModel.getSize() - 1;
        }
        fromList.setSelectedIndex(newSelection);
    }

    if (selection.length >= 5) {
        fromList.repaint();
        fromList.revalidate();
        toList.repaint();
        toList.revalidate();
    }

    // change selection of the toList
    toList.setSelectionInterval(targetPos, targetPos + selection.length - 1);

    // ensure the selection is visible
    toList.ensureIndexIsVisible(toList.getMaxSelectionIndex());
    toList.ensureIndexIsVisible(toList.getMinSelectionIndex());

    return objects;
}

From source file:util.ui.UiUtilities.java

/**
 * Moves Selected Items from one List to another
 *
 * @param fromList/*from ww w  .  j  a  va  2 s  . c om*/
 *          Move from this List
 * @param toList
 *          Move into this List
 * @param row
 *          The target row where to insert
 * @return Moved Elements
 */
public static Object[] moveSelectedItems(JList fromList, JList toList, int row) {
    DefaultListModel fromModel = (DefaultListModel) fromList.getModel();
    DefaultListModel toModel = (DefaultListModel) toList.getModel();

    // get the selection
    int[] selection = fromList.getSelectedIndices();

    if (selection.length == 0) {
        return new Object[] {};
    }

    Object[] objects = new Object[selection.length];
    for (int i = 0; i < selection.length; i++) {
        objects[i] = fromModel.getElementAt(selection[i]);
    }

    // move the elements
    for (int i = selection.length - 1; i >= 0; i--) {
        Object value = fromModel.remove(selection[i]);
        toModel.insertElementAt(value, row);
    }

    // change selection of the fromList
    if (fromModel.getSize() > 0) {
        int newSelection = selection[0];
        if (newSelection >= fromModel.getSize()) {
            newSelection = fromModel.getSize() - 1;
        }
        // fromList.setSelectedIndex(-1);
    }

    // change selection of the toList
    toList.setSelectionInterval(row, row + selection.length - 1);

    // ensure the selection is visible
    toList.ensureIndexIsVisible(toList.getMaxSelectionIndex());
    toList.ensureIndexIsVisible(toList.getMinSelectionIndex());

    return objects;
}

From source file:util.ui.UiUtilities.java

/**
 * Move selected Items in the JList/*from  w w w  . j a v  a2 s  .com*/
 *
 * @param list
 *          Move Items in this List
 * @param row
 *          The target row where to insert
 * @param sort
 *          Dummy parameter, does nothing
 */
public static void moveSelectedItems(JList list, int row, boolean sort) {
    DefaultListModel model = (DefaultListModel) list.getModel();

    // get the selection
    int[] selection = list.getSelectedIndices();
    if (selection.length == 0) {
        return;
    }

    boolean lower = false;
    // Remove the selected items
    Object[] items = new Object[selection.length];
    for (int i = selection.length - 1; i >= 0; i--) {
        if (selection[i] < row && !lower) {
            row = row - i - 1;
            lower = true;
        }
        items[i] = model.remove(selection[i]);
    }

    for (int i = items.length - 1; i >= 0; i--) {
        model.insertElementAt(items[i], row);
    }

    // change selection of the toList
    list.setSelectionInterval(row, row + selection.length - 1);

    // ensure the selection is visible
    list.ensureIndexIsVisible(list.getMaxSelectionIndex());
    list.ensureIndexIsVisible(list.getMinSelectionIndex());
}

From source file:util.ui.UiUtilities.java

/**
 * Move selected Items in the JList/*www  .ja v  a2s  .co  m*/
 *
 * @param list
 *          Move Items in this List
 * @param nrRows
 *          Move Items nrRows up/down
 */
public static void moveSelectedItems(JList list, int nrRows) {
    DefaultListModel model = (DefaultListModel) list.getModel();

    // get the selection
    int[] selection = list.getSelectedIndices();
    if (selection.length == 0) {
        return;
    }

    // Remove the selected items
    Object[] items = new Object[selection.length];
    for (int i = selection.length - 1; i >= 0; i--) {
        items[i] = model.remove(selection[i]);
    }

    // insert the elements at the target position
    int targetPos = selection[0] + nrRows;
    targetPos = Math.max(targetPos, 0);
    targetPos = Math.min(targetPos, model.getSize());
    for (int i = 0; i < items.length; i++) {
        model.add(targetPos + i, items[i]);
    }

    // change selection of the toList
    list.setSelectionInterval(targetPos, targetPos + selection.length - 1);

    // ensure the selection is visible
    list.ensureIndexIsVisible(list.getMaxSelectionIndex());
    list.ensureIndexIsVisible(list.getMinSelectionIndex());
}