jlist

How to generate a Jlist with alternating colors

和自甴很熟 提交于 2019-11-29 13:00:25
问题 In Java how do I get a JList with alternating colors? Any sample code? 回答1: To customize the look of a JList cells you need to write your own implementation of a ListCellRenderer. A sample implementation of the class may look like this: (rough sketch, not tested) public class MyListCellThing extends JLabel implements ListCellRenderer { public MyListCellThing() { setOpaque(true); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean

JList.getModel() ClassCastException

早过忘川 提交于 2019-11-29 04:36:14
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 don't understand why this is happening. Is there anything I can do to stop this from happening? You

JScrollPane and JList auto scroll

*爱你&永不变心* 提交于 2019-11-29 03:36:01
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.getAdjustable().getMaximum()); } }); It auto scrolls down. But, if I try to scroll back up to re-read a

Refresh JList in a JFrame

谁说我不能喝 提交于 2019-11-29 03:06:23
I've got a JList which displays information from a vector. The user can then add and remove information from this vector. Is it possible to refresh the JList inside my JFrame when items are added / removed from the Vector? Currently I'm doing.. list = new JList(names); jframe.add(new JScrollPane(list), BorderLayout.CENTER); but this doesn't refresh the JList to anything new. I've check and my vector contents etc. do change but the list isn't refreshing. Why? how can I fix it? You should not be updating the Vector. Changes should be made directly to the ListModel then the table will repaint

Changing JList row color at runtime

独自空忆成欢 提交于 2019-11-29 02:32:14
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? Based on ListDemo sample from SUN. If you enter some text in the textfield which isn't in the list and you hit highlight it gets

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

时光怂恿深爱的人放手 提交于 2019-11-28 21:27:52
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 click buttons to fire an action) Vinay A simple example would be like below using listselectionlistener

Add ToolTipText in Jlist Item(s)

北慕城南 提交于 2019-11-28 14:33:33
public static void populateJList(JList list) { ArrayList<String> contents= DataAccess.getContents(); DefaultListModel listModel = new DefaultListModel(); for (String string : contents) { listModel.addElement(string); } preset.setModel(listModel); } I have this method that populates my JList, is there a way that I can add toolTipText of every item in the JList? You can override the getToolTipText(..) method of the JList. I believe you can also use a custom renderer which invokes the setToolTipText(...) method. If you don't want to override JList , you can use a MouseMotionAdapter to change the

Cannot add checkbox to the JList

萝らか妹 提交于 2019-11-28 14:21:37
I'm very new to programming, and I can't add JCheckbox to the JList . There is no error but nothing is displayed. JFrame f=new JFrame(); String[] labels={"a","b","c","d","e"}; JCheckBox[] ch=new JCheckBox[labels.length]; JList list=new JList(); for (int i = 0; i < labels.length; i++) { ch[i]=new JCheckBox("CheckBox"+i); list.add(ch[i]); } JScrollPane pane=new JScrollPane(list); f.add(pane); f.setVisible(true); trashgod A JList renderer can draw a checkbox, but JList does not support a cell editor. Instead, consider a one-column JTable . The default renderer & editor for a column of type

Reordering JList with Drag and Drop

匆匆过客 提交于 2019-11-28 13:25:26
I encountered a problem regarding reordering elements in a JList using Drag and Drop. This following code is a modification of a code where you could drag elements from one JList to another (worked only one way). I tried to make it usable for only one JList, but the elements can't even be dragged out of the list. So I guess it can't be done this way. Any ideas what I'm doing wrong or not taking into consideration? The idea is to get it to work for a Jlist with thumbnails, but since I can't even get it to work with just strings... I have been looking into several D'n'D tutorials, but still I

Is there something wrong with Swing's MVC implementation for JList?

三世轮回 提交于 2019-11-28 13:23:52
Some time ago I asked this question . All solutions are workarounds. Now this can't be. I feel that something is wrong here, but I can't tell if it is Swing's MVC model that is conceptually wrong, or if it is my thinking that is conceptually wrong. Here is the problem again. I am using a JList to implement a list of thumbnails for the pages of a document. If the user selects another thumbnail from the list, that page is loaded. To do this I added a ListSelectionListener to the JList , which when the selection changes, it loads that page. But the user can also change the page using another