List of usage examples for javax.swing DefaultListModel elementAt
public E elementAt(int index)
From source file:Main.java
public static void main(final String args[]) { String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultListModel model = new DefaultListModel(); model.ensureCapacity(1000);/*w ww .j av a 2 s . co m*/ for (int i = 0; i < 100; i++) { for (int j = 0; j < 5; j++) { model.addElement(labels[j]); } } Object[] objs = new Object[10]; System.out.println(model.elementAt(1)); JList jlist2 = new JList(model); jlist2.setVisibleRowCount(4); jlist2.setFixedCellHeight(12); jlist2.setFixedCellWidth(200); JScrollPane scrollPane2 = new JScrollPane(jlist2); frame.add(scrollPane2, BorderLayout.CENTER); frame.setSize(300, 350); frame.setVisible(true); }
From source file:com.palantir.ptoss.cinch.swing.JListWiringHarness.java
private static void updateListModel(JList list, List<?> newContents) { if (newContents == null) { newContents = ImmutableList.of(); }//from w w w.j ava2 s . co m ListModel oldModel = list.getModel(); List<Object> old = Lists.newArrayListWithExpectedSize(oldModel.getSize()); for (int i = 0; i < oldModel.getSize(); i++) { old.add(oldModel.getElementAt(i)); } if (old.equals(newContents)) { return; } Object[] selected = list.getSelectedValues(); DefaultListModel listModel = new DefaultListModel(); for (Object obj : newContents) { listModel.addElement(obj); } list.setModel(listModel); List<Integer> newIndices = Lists.newArrayListWithCapacity(selected.length); Set<Object> selectedSet = Sets.newHashSet(selected); for (int i = 0; i < listModel.size(); i++) { if (selectedSet.contains(listModel.elementAt(i))) { newIndices.add(i); } } list.setSelectedIndices(ArrayUtils.toPrimitive(newIndices.toArray(new Integer[0]))); }
From source file:com.iisigroup.ris.WebFileScanUtilBrowserUI.java
private void initGUI() { final SwingActionUtil swingUtil = (SwingActionUtil) SwingActionUtil.newInstance(this); try {// w w w . jav a2 s. co m BorderLayout thisLayout = new BorderLayout(); getContentPane().setLayout(thisLayout); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); this.setTitle("browser source code"); { informationMenu = new JPopupMenu(); setComponentPopupMenu(this, informationMenu); } { ListModel openFileListModel = new DefaultListModel(); openFileList = new JList(); openFileList.setModel(openFileListModel); getContentPane().add(openFileList, BorderLayout.NORTH); openFileList.setPreferredSize(new java.awt.Dimension(663, 281)); openFileList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent evt) { swingUtil.invokeAction("openFileList.valueChanged", evt); } }); openFileList.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent evt) { swingUtil.invokeAction("openFileList.mouseMoved", evt); } }); openFileList.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { swingUtil.invokeAction("openFileList.keyPressed", evt); } }); } { jPanel1 = new JPanel(); GroupLayout jPanel1Layout = new GroupLayout((JComponent) jPanel1); jPanel1.setLayout(jPanel1Layout); getContentPane().add(jPanel1, BorderLayout.SOUTH); jPanel1.setPreferredSize(new java.awt.Dimension(478, 35)); { openSelected = new JButton(); openSelected.setText("open all"); openSelected.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { swingUtil.invokeAction("openSelected.actionPerformed", evt); } }); } jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup().addContainerGap(178, 178) .addComponent(openSelected, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE) .addContainerGap(174, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup().addGap(7) .addComponent(openSelected, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE) .addGap(0, 6, Short.MAX_VALUE)); } { informationMenu = new JPopupMenu(); } this.setSize(486, 350); this.setLocationRelativeTo(null); //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // ?tooltip //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx int initialDelay = ToolTipManager.sharedInstance().getInitialDelay(); ToolTipManager.sharedInstance().setInitialDelay(0); //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // ?tooltip //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx swingUtil.addAction("openFileList.mouseClicked", new Action() { public void action(EventObject evt) throws Exception { DefaultListModel model = (DefaultListModel) openFileList.getModel(); int pos = openFileList.getLeadSelectionIndex(); if (pos == -1) { return; } setOpenFileListToolTip(); MouseEvent eeev = (MouseEvent) evt; if (eeev.getClickCount() != 2) { return; } MFile file = (MFile) model.elementAt(pos); openSource(file.file); } }); final JListUtil jlistUtil = JListUtil.newInstance(openFileList); swingUtil.addAction("openFileList.keyPressed", new Action() { public void action(EventObject evt) throws Exception { jlistUtil.defaultJListKeyPressed((KeyEvent) evt); setOpenFileListToolTip(); } }); swingUtil.addAction("openSelected.actionPerformed", new Action() { public void action(EventObject evt) throws Exception { DefaultListModel model = (DefaultListModel) openFileList.getModel(); for (Enumeration<?> enu = model.elements(); enu.hasMoreElements();) { MFile file = (MFile) enu.nextElement(); openSource(file.file); } } }); swingUtil.addAction("openFileList.mouseMoved", new Action() { public void action(EventObject evt) throws Exception { setOpenFileListToolTip(); } }); swingUtil.addAction("openFileList.valueChanged", new Action() { public void action(EventObject evt) throws Exception { System.out.println(evt); setOpenFileListToolTip(); } }); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.executequery.gui.editor.ManageShortcutsPanel.java
private EditorSQLShortcut getShortcutAt(int index) { DefaultListModel model = modelFromList(); if (index >= model.size()) { return null; }/*from w w w. j av a 2 s . c om*/ return (EditorSQLShortcut) model.elementAt(index); }