jlist

JList not responding to listModel.addElement(

依然范特西╮ 提交于 2019-12-11 02:35:57
问题 I have a JList in my Java Swing application and when a user clicks a button, the list clears and the contents reset as follows: public void reset(ArrayList<String> content) { listModel.removeAllElements(); System.out.println(content.size()); for(int i = 0; i < content.size(); i++) { listModel.addElement(content.get(i)); System.out.println("Added element " + content.get(i)); } } The list is initialized as follows listModel = new DefaultListModel(); list = new JList(listModel); However there is

Swing JList freezes when I delete and add while 0 index was selected

梦想的初衷 提交于 2019-12-11 02:33:43
问题 This is an example where you press a button and jList1 is refilled with items from a1 to a1000.: //variable private List<String> list = new ArrayList<>(); ... //main method jList1.setModel(new DefaultListModel()); for(int i = 0; i < 1000; i++) { list.add("a"+i); } ... //button action - jList1 refill DefaultListModel dtm = (DefaultListModel)jList1.getModel(); dtm.removeAllElements(); for(String s : list) { dtm.addElement(s); } If I fill the jList1 , then select (with mouse) 0 index (first

Use Button as renderer for list

旧街凉风 提交于 2019-12-11 00:48:12
问题 I would like to use a more complex renderer consisting of several components for a list (more precisely something like this (a textfieldinput with some buttons arranged in a panel). However when I try to use a button in a list it seems to ignore clicks. Minimal example from javax.swing import DefaultListCellRenderer from javax.swing import DefaultListSelectionModel from javax.swing import JButton from javax.swing import JLabel from javax.swing import JPanel from javax.swing import JList from

How to edit a JList item text by double clicking on it?

我只是一个虾纸丫 提交于 2019-12-11 00:01:03
问题 I have a JList populated with Strings. I want to be able to edit an item by double clicking on it and then edit its text in the JList itself. So if I click an item, then it's highlighted and I see the cursor, then for example I delete the text and write something else. Then I click enter and the text is edited successfully. Is that possible & how can I do this please? I really need this. 回答1: See List Editor for a couple of solutions, one is to just use a JTable, the other is to create a

Get multiple selected items from a JList

耗尽温柔 提交于 2019-12-10 21:56:43
问题 I am creating a screen with four lists on it. Basically two pairs of lists where you can select lines on one list in the pair and move them to the other list in the pair. Looking at the documentation I need a ListSelectionModel for each list to determine which lines have been selected. I will use a [Sel] or [Des] button to do the actual transfer. The documentations and samples say I need a ListSelectionListener but as I will not access the model until the user clicks on the button do I

How to make List with all files in folder using swing?

邮差的信 提交于 2019-12-10 18:56:08
问题 I want to make a list with filename from a folder and show all the files that are present in that folder with a particular extension. I want the list to be selectable so that I can select and delete the file from the list or edit it. I know how to select all files from a folder but don't know how to show it in GUI. File folder = new File("c:/"); File[] listOfFiles = folder.listFiles(); 回答1: This example shows how to enumerate the files in a directory and display them in a JToolBar and a JMenu

Disable JList Cell Selection Property

流过昼夜 提交于 2019-12-10 18:35:15
问题 I am attempting to display an array of strings in a JList , which is then added to a JPanel using Java Swing . I am not having a problem displaying the data in the Jlists , however I would like to remove the default property that allows a user to select items in the Jlist . I am attempting to simply display the data to the user. Unfortunately I am unable to locate the property that would allow me to disable this feature. A example of the selection property that I am referring to can be seen

Changing cursor when rendering a JList

别来无恙 提交于 2019-12-10 17:53:28
问题 I've achieved what I'm trying to do, but I can't help but to think there is a more efficient way...allow me to illustrate. In short, the question I'm asking is if there's a way to determine when a component has finished it's initial rendering. I have a JList, which is hooked up to a DefaultListModel and getting painted by a custom renderer which extends the DefaultListCellRenderer. This intention of the JList is to "page" through a log file, populating 2500 elements every time a new page is

How to remove multiple items in JList

时光毁灭记忆、已成空白 提交于 2019-12-10 17:30:02
问题 It's funny, I can't find out how to delete multiple selected items in a JList Help please UPD: OK, the problem was in NetBeans, because it creates JList and sets model AbstractListModel which somehow not working with remove method. 回答1: DefaultListModel dlm = (DefaultListModel) subjectList.getModel(); if(this.subjectList.getSelectedIndices().length > 0) { int[] selectedIndices = subjectList.getSelectedIndices(); for (int i = selectedIndices.length-1; i >=0; i--) { dlm.removeElementAt

How can I create a JList that contains entries of a Hashtable of String and Object?

▼魔方 西西 提交于 2019-12-10 13:17:26
问题 I want to create a JList that contains entries of a Hashtable of String and object : Hashtable<String,Object> the JList element should contain the hashtable entry and display the value of the entry key that is a string ... Is it possible ? How can it be done ? 回答1: Implement the ListModel interface by extending AbstractListModel . Use the derived model to create your JList . See also How to Use Lists. 回答2: Use the keyset of your Hashtable for the data in your JList : Hashtable<String, Object>