jlist

Java - How do I get a all the selected values from a JList?

纵然是瞬间 提交于 2019-12-04 03:44:34
A struggling Java newbie here - help! I'm trying to: - Get all the selected values from a JList - Create an ArrayList from those values It seems getSelectedValues is deprecated? Until JDK 1.6 (deprecated in 1.7): public Object[] getSelectedValues() New since JDK 1.7: public List<E> getSelectedValuesList() Returns a list of all the selected items, in increasing order based on their indices in the list. As of JDK1.7 it was replaced with public List<E> getSelectedValuesList() . http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html#getSelectedValuesList%28%29 use getSelectedValuesList

Java - Updating JList after changing an object

断了今生、忘了曾经 提交于 2019-12-04 03:15:10
问题 I have a JList which uses a DefaultListModel. I then add values to the model which then appear in the JList. I have created a MouseListener which (when double clicked) allows the user to edit the current user number of that person they have selected. I have checked that the actual object of that record is being changed, and it is. The only issue I'm having is getting the actual Jlist to update to show the new values of that object. Snippets of the current code I have are: Creating the JList

How to prevent JList from making selection outside cell bounds?

时光毁灭记忆、已成空白 提交于 2019-12-04 03:00:35
"Is there any way to prevent a JList from selecting the last element when the user clicks below it on the list?" That is the question someone asked here and I have the same problem. That guy found a so-so solution (by overriding processMouseEvent() ) but I want to know if there is a better/more elegant way to do it. [Edit] Ok, more detail. If you have a JList and there is some space unoccupied by any cell/element and you click that space then the last element in the JList got selected. For a real example try this JList Swing Tutorial example , click the white space and see that Rollo got

Make JList Values Unselectable [duplicate]

≡放荡痞女 提交于 2019-12-04 02:52:35
This question already has an answer here: Disable items in JList 4 answers I was wondering how to modify a JList so that clicking any values would not do anything. I have looked at other questions but none have helped. I solved it by using the following class: class DisabledItemSelectionModel extends DefaultListSelectionModel { @Override public void setSelectionInterval(int index0, int index1) { super.setSelectionInterval(-1, -1); } } I instantiated the class here: console.setSelectionModel(new DisabledItemSelectionModel()); StormeHawke Assuming your objects in your JList are clickable items,

JOptionPane and scroll function

浪子不回头ぞ 提交于 2019-12-04 02:35:17
I want to JList a lot of results in a JOptionPane, however, I'm not sure how to add in a scroll function should there be too many results. How would I add a scroll bar to a JOptionPane? Any help would be great. Thanks. GETah Here is an example using a JTextArea embedded in a JScrollPane : JTextArea textArea = new JTextArea("Insert your Text here"); JScrollPane scrollPane = new JScrollPane(textArea); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); scrollPane.setPreferredSize( new Dimension( 500, 500 ) ); JOptionPane.showMessageDialog(null, scrollPane, "dialog test with textarea",

Is there a way to get all elements in a JList?

十年热恋 提交于 2019-12-04 02:11:06
I was wondering if there is a way to retrieve a list of all the elements that have been added to a JList. For example I would like JList to return an array or list of Strings or JLabels from a custom cell renderer. Yes of course. You getElementAt() with a loop. Example: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.WindowConstants; public class Jlist { JFrame frame; JList

Disabling individual JComboBox items

倾然丶 夕夏残阳落幕 提交于 2019-12-04 01:37:04
问题 This is a fairly common problem, and the solution I've used is similar to what I searched and found later. One implements a ListCellRenderer with a JLabel that enables or disables itself based on the current selected index: public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setText(value.toString()); UIDefaults defaults = UIManager.getDefaults(); Color fc; if (index == 1) { setEnabled(false); fc = defaults.getColor(

Removing items from JList

穿精又带淫゛_ 提交于 2019-12-03 21:51:11
问题 I've got a simple Jlist with data from List<String> , Now I want to remove selected item from Jlist. Here is the code: final DefaultListModel<String> model = new DefaultListModel(); final JList list = new JList(model); //filling list //loop for every element from List<String> public static void sample(DefaultListModel model, List<String> data) for(int i=;i<data.size();i++) {model.addElement(data.get(i));} //btn pressed public void actionPerformed(ActionEvent arg0) { int index = list

JList - getting value from Click

独自空忆成欢 提交于 2019-12-03 17:21:10
Is there any way to use ListSelectionListener or MouseAdapter to get information about selected value (if value is a String for example), is there any built-in method for that? I only know how to get proper indexes but not the content or content.toString() I'm adding element like this: { DefaultListModel listModel; listModel.addElement(name); } @Edit Thank you for you for help. I solved my problem by doing this (for future generations so they wouldn't need to search as I did): list.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e) { System.out.println("Mouse

How to change background color of the selected item in JList dynamically

巧了我就是萌 提交于 2019-12-03 12:49:22
问题 How can I change the background color of the item which is selected in JList dynamically? 回答1: Something like the following should help as a starting point: public class SelectedListCellRenderer extends DefaultListCellRenderer { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (isSelected) { c.setBackground