jlist

Why do I sometimes get blank JLists after updating contents through the list model?

房东的猫 提交于 2019-12-01 07:11:56
问题 I have a recurring problem where I have a JList which I wish to update with new contents. I'm using a DefaultListModel which provides methods for adding new content to the list but when using these methods I find that some proportion of the calls result in a completely blank JList. Whether or not the update works seems to be random, and not related to the data which is sent. Below is a simple program which demonstrates the problem. It simply generates a list of an increasing size to update a

Disabling individual JComboBox items

做~自己de王妃 提交于 2019-12-01 06:49:12
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("Label.disabledForeground"); setFocusable(false); } else { // fc = defaults.getColor("Label.foreground");

JList text alignment

心已入冬 提交于 2019-12-01 05:34:45
I have a JList with items that I want to show two values. Is there a way to have it show a string name and then have a right justified string to show a value. Looking something like this: Title__________________120 Title2_________________135 Is it possible to pass in two string to an item and have the first string display on the left and the second one on the right? Sure, implement a custom renderer . You might return a JPanel with BorderLayout as the rendering component, with the LHS text in the WEST , and the RHS text in the EAST . Another way is to shove HTML into the default renderer (a

java refreshing an array into jList

旧巷老猫 提交于 2019-12-01 05:26:51
OK so I have a JList and the content is provided with an array. I know how to add elements to an array but I want to know how to refresh a JList... or is it even possible? I tried Google. :\ import java.applet.Applet; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class bs extends JApplet implements MouseListener { public static String newline; public static JList list; public void init() { String[] data = {"one", "two", "three", "four"}; list = new JList(data); this.getContentPane().add(list); list.addMouseListener(this); String newline = "\n"; list.setVisible(true);

Synchronized JList and JComboBox?

故事扮演 提交于 2019-12-01 03:53:57
In Java Swing, what's the best way for a JList and a JComboBox to be synchronized in terms of the data, i.e., to have the same list of items at any given point of time? Basically, if I add items to (or remove items from) one, the other should reflect the change automatically. I've tried doing the following, but it doesn't seem to work: JList list = new JList(); JComboBox comboBox = new JComboBox(); DefaultListModel listModel = new DefaultListModel(); // add items to listModel... list.setModel(listModel); comboBox.setModel(new DefaultComboBoxModel(listModel.toArray())); Your models - the

java refreshing an array into jList

醉酒当歌 提交于 2019-12-01 03:27:39
问题 OK so I have a JList and the content is provided with an array. I know how to add elements to an array but I want to know how to refresh a JList... or is it even possible? I tried Google. :\ import java.applet.Applet; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class bs extends JApplet implements MouseListener { public static String newline; public static JList list; public void init() { String[] data = {"one", "two", "three", "four"}; list = new JList(data); this

JList - select multiple items

。_饼干妹妹 提交于 2019-12-01 02:29:41
I faced a problem with this setSelectedValue() method in JList when I wanted to select multiple values in a JList automatically, it still selected only one. Is there a way? String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" }; final JList theList = new JList(items); theList.setSelectedValue("Item 1",true); theList.setSelectedValue("Item 2",true); This code shows only Item 2 as selected. Use JList.setSelectedIndices(int[]) after calling JList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) . E.G. import javax.swing.*; import java.io.*; import java.util.ArrayList; class

Add a Jlist to a JScrollPane

折月煮酒 提交于 2019-12-01 01:01:30
I have a JList that I need to Place inside a scroll Pane because I am getting the JList from the database and the values can increase greatly. I need to be able to scroll them down so I wrote: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; public class Checkboxlistener extends JFrame { private JPanel jpAcc = new JPanel(); private JList checkBoxesJList; Checkboxlistener() { //

Word wrap in JList items

江枫思渺然 提交于 2019-11-30 18:44:17
I have a JList with very long item names that cause the horizontal scroll-bar to appear in scroll-pane. Is there anyway that I can word wrap so that the whole whole item name appears in 2 rows yet can be selected in one click? I.E it should still behave as a single item but be displayed in two rows. Here is what I did after seeing the example below I added a new class to my project MyCellRenderer and then I went added MyList.setCellRenderer(new MyCellRenderer(80)); in the post creation code of my List. Is there anything else I need to do? Yep, using Andrew's code, I came up with something like

Adding elements to JList in Swing Java

天大地大妈咪最大 提交于 2019-11-30 18:28:05
I have a function that executes when a button is clicked. Suppose there is a loop to add 1 to 10 to a JList . I add that data to DefaultListModel . It works perfectly and the numbers get added. Then I added a Thread.sleep(1000) within the loop. But the output is different. I wanted to add 1 element every second. But now it waits for 10secs and the add all 1 to 10 together at the end of 10th second. Am I wrong anywhere? List processList = listNumbers.getSelectedValuesList(); DefaultListModel resultList = new DefaultListModel(); listResult.setModel(resultList); for (int i = 0; i < processList