List of usage examples for javax.swing DefaultListModel size
public int size()
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);//from w w w . j a v a 2 s .com for (int i = 0; i < 100; i++) { for (int j = 0; j < 5; j++) { model.addElement(labels[j]); } } System.out.println(model.size()); 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 a v a 2 s. c o 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:net.sf.jabref.gui.ContentSelectorDialog2.java
private static int findPos(DefaultListModel<String> lm, String item) { for (int i = 0; i < lm.size(); i++) { String s = lm.get(i);// www . j a va 2s .c o m if (item.compareToIgnoreCase(s) < 0) { // item precedes s return i; } } return lm.size(); }
From source file:Main.java
private void removeActionPerformed(ActionEvent e) { System.out.println("made_list's model: " + made_list.getModel()); System.out.println("Model from a fresh JList: " + new JList().getModel()); DefaultListModel model = (DefaultListModel) made_list.getModel(); if (model.size() > 0) { model.remove(0);/*from ww w .j a va 2 s.c o m*/ } }
From source file:CheckBoxList.java
@SuppressWarnings("unchecked") public java.util.List getCheckedItems() { java.util.List list = new java.util.ArrayList(); DefaultListModel dlm = (DefaultListModel) getModel(); for (int i = 0; i < dlm.size(); ++i) { Object obj = getModel().getElementAt(i); if (obj instanceof JCheckBox) { JCheckBox checkbox = (JCheckBox) obj; if (checkbox.isSelected()) { list.add(checkbox);//from w w w . ja va 2 s.com } } } return list; }
From source file:CheckBoxList.java
@SuppressWarnings("unchecked") public int[] getCheckedIdexes() { java.util.List list = new java.util.ArrayList(); DefaultListModel dlm = (DefaultListModel) getModel(); for (int i = 0; i < dlm.size(); ++i) { Object obj = getModel().getElementAt(i); if (obj instanceof JCheckBox) { JCheckBox checkbox = (JCheckBox) obj; if (checkbox.isSelected()) { list.add(new Integer(i)); }//from ww w . j ava 2 s.co m } } int[] indexes = new int[list.size()]; for (int i = 0; i < list.size(); ++i) { indexes[i] = ((Integer) list.get(i)).intValue(); } return indexes; }
From source file:gtu._work.ui.CheckJavaClassPathUI.java
private void initGUI() { try {/*from ww w . java2 s.c o m*/ BorderLayout thisLayout = new BorderLayout(); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(thisLayout); setTitle("??importjava"); { jTabbedPane1 = new JTabbedPane(); getContentPane().add(jTabbedPane1, BorderLayout.CENTER); { jPanel1 = new JPanel(); BorderLayout jPanel1Layout = new BorderLayout(); jPanel1.setLayout(jPanel1Layout); jTabbedPane1.addTab("", null, jPanel1, null); { classPathText = new JTextField(); jPanel1.add(classPathText, BorderLayout.NORTH); classPathText.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { if (StringUtils.isNotBlank(classPathText.getText())) { String text = classPathText.getText(); DefaultListModel model = (DefaultListModel) classPathList.getModel(); boolean findOk = false; for (int ii = 0; ii < model.size(); ii++) { String val = (String) model.getElementAt(ii); if (StringUtils.equals(val, text)) { findOk = true; } } if (!findOk) { model.addElement(text); } } } }); } { jScrollPane1 = new JScrollPane(); jPanel1.add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.setPreferredSize(new java.awt.Dimension(492, 302)); { DefaultListModel classPathListModel = new DefaultListModel(); classPathList = new JList(); Set<String> clzSet = new HashSet<String>(); for (Enumeration<?> enu = configBean.getConfigProp().keys(); enu.hasMoreElements();) { String key = (String) enu.nextElement(); if (key.contains(CLASSNAME_KEY)) { clzSet.add(configBean.getConfigProp().getProperty(key)); } } for (String clzName : clzSet) { classPathListModel.addElement(clzName); } jScrollPane1.setViewportView(classPathList); classPathList.setModel(classPathListModel); classPathList.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { JListUtil.newInstance(classPathList).defaultJListKeyPressed(evt); } }); } } { executeBtn = new JButton(); jPanel1.add(executeBtn, BorderLayout.WEST); executeBtn.setText("\u57f7\u884c"); executeBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { executeBtnActionPerformed(evt); } }); } { srcPathText = new JTextField(); if (configBean.getConfigProp().containsKey(SRCPATHTEXT_KEY)) { srcPathText.setText(configBean.getConfigProp().getProperty(SRCPATHTEXT_KEY)); } jPanel1.add(srcPathText, BorderLayout.SOUTH); JCommonUtil.jTextFieldSetFilePathMouseEvent(srcPathText, true); } } { jPanel2 = new JPanel(); BorderLayout jPanel2Layout = new BorderLayout(); jPanel2.setLayout(jPanel2Layout); jTabbedPane1.addTab("log", null, jPanel2, null); { jScrollPane2 = new JScrollPane(); jPanel2.add(jScrollPane2, BorderLayout.CENTER); jScrollPane2.setPreferredSize(new java.awt.Dimension(530, 350)); { logArea = new JTextArea(); jScrollPane2.setViewportView(logArea); } } } } pack(); this.setSize(551, 417); } catch (Exception e) { //add your error handling code here e.printStackTrace(); } }
From source file:net.sf.xmm.moviemanager.gui.DialogIMDbMultiAdd.java
/** * Gets info...//from w ww.jav a 2 s. co m **/ public void executeCommandSelect() { int index = getMoviesList().getSelectedIndex(); /* * When adding the file info the an existing movie, a new ModelMovieInfo object is created. * When done, the old ModelMovieInfo object created in the * MovieManagerCommandAddMultipleMovies object needs not to save the file as a new movie, * therefore setCAncel method with true is called at the end of the if scoop. */ DefaultListModel listModel = (DefaultListModel) getMoviesList().getModel(); if (index == -1 || index > listModel.size()) return; if (addInfoToExistingMovie) { ModelMovie model = ((ModelMovie) listModel.getElementAt(index)); if (model.getKey() == -1) return; if (!model.getHasAdditionalInfoData()) { model.updateAdditionalInfoData(); } ModelMovieInfo modelInfoTmp = new ModelMovieInfo(model, false); /* Need to set the hasReadProperties variable because when normally calling the getfileinfo the first time it replaces the old additional values with the new ones Then the second time it plusses the time and size to match. When multiadding the next file info should be directly added to the old, not replace it */ modelInfoTmp._hasReadProperties = true; try { modelInfoTmp.getFileInfo(new File[] { multiAddFile.getFile() }); } catch (Exception e) { log.error("Error occured while retrieving file info.", e); //$NON-NLS-1$ } try { modelInfoTmp.saveToDatabase(null); } catch (Exception e) { log.error("Saving to database failed.", e); //$NON-NLS-1$ } setCanceled(true); dispose(); } else { ModelIMDbSearchHit model = ((ModelIMDbSearchHit) listModel.getElementAt(index)); if (model.getUrlID() == null) return; if (getUrlKeyOnly) { modelEntry.setUrlKey(model.getUrlID()); dispose(); return; } if (multiAddSelectOption == ImdbImportOption.selectFirst && imdbId != null && !imdbId.equals("")) //$NON-NLS-1$ // Use previously fetched imdb id getIMDbInfo(modelEntry, imdbId); else getIMDbInfo(modelEntry, model.getUrlID()); ModelMovieInfo.executeTitleModification(modelEntry); dispose(); } }
From source file:edu.ku.brc.af.ui.forms.formatters.UIFormatterListEdtDlg.java
/** * @param uif/*from w w w.j ava 2 s .c om*/ * @param isNew */ protected void editFormatter(final UIFieldFormatterIFace uif, final boolean isNew) { try { UIFieldFormatterIFace tempCopy = isNew ? uif : (UIFieldFormatterIFace) uif.clone(); boolean isDefault = tempCopy.isDefault(); UIFormatterEditorDlg dlg = new UIFormatterEditorDlg(this, fieldInfo, tempCopy, isNew, doProcessSamples, uiFieldFormatterMgrCache); dlg.setVisible(true); if (!dlg.isCancelled()) { UIFieldFormatterIFace selectedUIF = dlg.getSelectedFormat(); selectedUIF.setDefault(isDefault); if (selectedUIF.isIncrementer() && selectedUIF.getAutoNumber() == null) { AutoNumberIFace autoNum = UIFieldFormatterMgr.getInstance().createAutoNumber( AutoNumberGeneric.class.getName(), fieldInfo.getTableInfo().getClassName(), fieldInfo.getName(), tempCopy.getFields().size() == 1); selectedUIF.setAutoNumber(autoNum); } DefaultListModel model = (DefaultListModel) formatList.getModel(); if (isNew) { model.addElement(selectedUIF); uiFieldFormatterMgrCache.addFormatter(selectedUIF); if (model.size() == 1) { selectedUIF.setDefault(true); formatList.setSelectedValue(selectedUIF, true); } } else { model.removeElement(uif); model.addElement(selectedUIF); uiFieldFormatterMgrCache.removeFormatter(uif); uiFieldFormatterMgrCache.addFormatter(selectedUIF); } setHasChanged(true); } } catch (Exception ex) { ex.printStackTrace(); edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(UIFormatterListEdtDlg.class, ex); } }
From source file:net.sf.jabref.gui.ContentSelectorDialog2.java
private void applyChanges() { boolean changedFieldSet = false; // Watch if we need to rebuild entry editors // First remove the mappings for fields that have been deleted. // If these were re-added, they will be added below, so it doesn't // cause any harm to remove them here. for (String fieldName : removedFields) { metaData.clearContentSelectors(fieldName); changedFieldSet = true;/*from w ww . j ava 2 s. c om*/ } // Cycle through all fields that we have created listmodels for: for (Map.Entry<String, DefaultListModel<String>> entry : wordListModels.entrySet()) { // For each field name, store the values: if ((entry.getKey() == null) || FIELD_FIRST_LINE.equals(entry.getKey())) { continue; } DefaultListModel<String> lm = entry.getValue(); int start = 0; // Avoid storing the <new word> marker if it is there: if (!lm.isEmpty()) { while ((start < lm.size()) && lm.get(start).equals(WORD_FIRSTLINE_TEXT)) { start++; } } if (metaData.getContentSelectors(entry.getKey()).isEmpty()) { changedFieldSet = true; List<String> data = new ArrayList<>(); for (int wrd = start; wrd < lm.size(); wrd++) { String word = lm.get(wrd); data.add(word); } metaData.setContentSelectors(entry.getKey(), data); } } // TODO: remove metadata for removed selector field. panel.markNonUndoableBaseChanged(); // Update all selectors in the current BasePanel. if (changedFieldSet) { panel.rebuildAllEntryEditors(); } else { panel.updateAllContentSelectors(); } panel.getAutoCompleters() .addContentSelectorValuesToAutoCompleters(panel.getBibDatabaseContext().getMetaData()); }