jlist

Swing JList with multiline text and dynamic height

假如想象 提交于 2019-11-28 00:47:15
I already read/tried these posts but that didn't help: Display multiple lines within a Jlist cell How to get multiline for a Jlist text? Problem displaying components of JList What I need is a ListCellRenderer which returns a panel with an icon on the left and a text of dynamic length on the right (like in any forum: on the left a user avatar, on the right the post text). The texts are NOT known to me, so I can't set a fixed cell height. Further, the text length differs from list cell to list cell. So every list cell needs its own height depending on the length of the text. Actually a really

How to create JList with icon and text?

北城余情 提交于 2019-11-27 23:58:43
I have this code want to make JList talk text and icon what must to do. Some persons advice me to make list of label and in table put text an icon is it Possible? How? Search on ///////////////// for embedded comment. import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JLabel; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import

How to clear a JList in Java?

南笙酒味 提交于 2019-11-27 23:30:35
问题 i have a jList in gui where i can add some data with Add button. what i want to add another button called Clear which will clear all elements. i tried this: private void jButtonClearActionPerfomed(java.awt.event.ActionEvent evt) { DefaultListModel listmodel=new DefaultListModel(); jList1 = new JList(listmodel); if(evt.getSource()==jButtonClear) JList.setListData(new String[0]; else listmodel.removeAllElements(); } When I click on Add button this will add elements. When I click on Clear button

JList.getModel() ClassCastException

牧云@^-^@ 提交于 2019-11-27 18:49:01
问题 When I call JList<String>.getModel() and cast it to DefaultListModel<String> it gives me this exception. Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JList$4 cannot be cast to javax.swing.DefaultListModel The code that throws it: private JList<String> list = new JList<String>(); ((DefaultListModel<String>) list.getModel()).addElement(...); It doesn't do it every time though. Most of the time it works perfectly, but other times it throws this exception. I

JScrollPane and JList auto scroll

a 夏天 提交于 2019-11-27 17:41:19
问题 I've got the next code: listModel = new DefaultListModel(); listModel.addElement(dateFormat.format(new Date()) + ": Msg1"); messageList = new JList(listModel); messageList.setLayoutOrientation(JList.VERTICAL); messageScrollList = new JScrollPane(messageList); messageScrollList.setPreferredSize(new Dimension(500, 200)); messageScrollList.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { e.getAdjustable().setValue(e

Changing JList row color at runtime

我的梦境 提交于 2019-11-27 16:52:24
问题 I am trying to change JList rows dynamically. I need change nth row colour, highlight it(n is unknown during compilation). I saw a lot of examples with custom ListCellRenderer, but all were "static". In other words I have JList with x rows. During runtime my "business logic" detects nth row is important. So I want make its background green, wait one second, and then make it white again. One more thing, don't wan change row selection. What is the best way to do so? 回答1: Based on ListDemo

JList with categories

一曲冷凌霜 提交于 2019-11-27 15:23:27
I have been googling hard trying to find a JList implementation with categories. I guess I could implement one myself, but the cell renderer, model and everything is a bit of a pain. Therefore I turn to you! My question is this: If I have a list of items, assigned to categories, I could display them in a JTree. But since I know the depth is never more than 2, I feel the JTree is overkill. Do you know if there is an easy way in Swing (or in some external library) to make a JList like this: Jlist with categories http://dl.dropbox.com/u/4607638/jlist-cats.png Where the blue fields are labels (not

JList that contains the list of Files in a directory

萝らか妹 提交于 2019-11-27 14:54:40
I created a JList that contains a list of files that are in a directory. Here is the JList . JList MList; String ListData[] // Create a new listbox control List = new JList(ListData); I also created a method that reads a list of text files in a directory: public String ReadDirectory() { String path = "C://Documents and Settings/myfileTxt"; String files = null; File folder = new File(path); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { files = listOfFiles[i].getName(); if (files.endsWith(".txt") || files.endsWith(".TXT"))

Find selected item of a JList and display it in real time

微笑、不失礼 提交于 2019-11-27 13:52:43
问题 I have a JList , where i am displaying some ID's. I want to capture the ID the user clicked and dis play it on a JLabel . String selected = jlist.getSelectedItem().toString(); The above code gives me the selected JList value. But this code has to be placed inside a button event, where when i click the button it will get the JList value an assign it to the JLabel . But, what i want to do is, as soon as the user clicks an item of the JList to update the JLabel in real time. (without having to

Disable items in JList

跟風遠走 提交于 2019-11-27 09:06:14
I'm using a JList as part of a wizard to display all the steps to be performed (it also allows clicking on a step to go to it). Some steps will not always be needed, based on what's done in previous steps. It's these inapplicable steps I'd like to disable in the list. How can I go about disabling (preventing the selection of) certain items in the list? Is there a better way than subclassing JList and overriding every selection-related method? you have to implements DefaultListSelectionModel , then you can set the Flag if isEnabled or not simple example import java.awt.*; import java.awt.event.