jlist

Change JList item background color on hover

感情迁移 提交于 2019-12-10 11:53:39
问题 I'm trying to change the background color of a JList cell when it is being hovered over, but I'm not sure how to do it. Here is what I currently have: package cats.youtube.gui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.util.LinkedList; import javax.swing.AbstractListModel; import javax.swing.DefaultListCellRenderer; import javax.swing.JList; import javax.swing.JPanel; import javax

java swing display search result in a popup under JTextField “like facebook search”

爷,独闯天下 提交于 2019-12-10 10:59:45
问题 I'm trying to simulate in java swing the same search results preview on facebook, but in java I suppose it'll gonna be : JTextField : the user input goes here JList : containing JList of JPanels ( results ) a popup or some container to hold the JList The problem is, I couldn't manage how to display a popup right under the JTextField, I tried swingx autocomplete, editable combo box , glazedlists ... but it's not giving me good results Can anyone help me please ? 回答1: When specifying the point

How to add element to existing JList

此生再无相见时 提交于 2019-12-10 02:58:22
问题 Part of my code ArrayList<Item> i = g.getItems(); Vector itemsVector = new Vector(i); JList items = new JList(iemsVector); Later in the code I create new object which I want to add to JList. How can I do that? 回答1: Populate the JList with a DefaultListModel, not a vector, and have the model visible in the class. Then simply call addElement on the list model to add items to it. 回答2: You may add it ( new object ) to the itemsVector (Vector). After adding an item into Vector object invoke the

How to prevent JList from making selection outside cell bounds?

不羁的心 提交于 2019-12-09 15:26:40
问题 "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

JList - select multiple items

陌路散爱 提交于 2019-12-09 02:22:06
问题 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. 回答1: Use JList.setSelectedIndices(int[]) after calling JList.setSelectionMode(ListSelectionModel

Adding Scrollbar to JList

和自甴很熟 提交于 2019-12-08 21:58:15
问题 I'm trying to add a scrollbar to a JList (which uses a custom data model). Adding a JScrollPane actually hides the JList, instead of adding a scrollbar. When I run the code with the scrollbar, the JList is not visible, and I cannot see the data. playlistField = new JList(playlist); // playlist is a data model playlistField.setLocation(32, 220-36); playlistField.setSize(350,120); playlistField.setVisible(true); this.add(playlistField); listScrollPane = new JScrollPane(playlistField,

Java warning - JList is a raw type, references must be parameterized

拟墨画扇 提交于 2019-12-08 19:49:45
问题 Can someone shed more light the following warning from Eclipse: JList is a raw type. References to generic type JList<E> should be parameterized. A line of code triggering this could be: import javax.swing.JList; .... private JList jList = null; // Warning on this line 回答1: You should put the type of the elements between <>, for example: List<String> list = new ArrayList<String>(); list.add("String 1"); list.add("Some Text"); 回答2: JList is a raw type as of Java 1.7, the same goes for a couple

Why do JList selections occur twice?

若如初见. 提交于 2019-12-08 17:30:21
问题 I have a JList with some items. I've added a listener for when a item in the list is selected. Here is the code for what happens when an item in the list is selected: private void questionaireNamesListValueChanged(ListSelectionEvent evt) { try { inputPanel.setEnabled(false); inputPanel.setVisible(false); inputTextField.setText(""); inputStatusLabel.setText(""); int questionaireIndex = questionaireNamesList.getSelectedIndex(); // Why will this be printed twice? System.out.println("Questionaire

Multiple lines in JList java

懵懂的女人 提交于 2019-12-08 14:24:36
问题 I'm currently working on a school project regarding school administration. In my project it is possible to create a school, education and students. I'm currently adding them all into the same JList(School), and it is giving me one problem. Whenever I'm adding for example a student it goes into the same line on the JList which makes it too long and I'm hoping for a way to do something like this on it: School 1 - Education 1 - Student 1 - Student 2 - Education 2 - Student 3 School 2 etc. etc. I

Java Swing JList, can't set minimum width

徘徊边缘 提交于 2019-12-08 14:24:11
问题 Trying to simple create a JList with a specified visible row count, but a minimum width. It seems "setMinimumSize()" does nothing though... import java.awt.*; import java.awt.event.*; import javax.swing.*; public class UserInterface { final static private int HEIGHT = 400; final static private int WIDTH = 650; public static void main(String[] args) { JPanel content = new JPanel(); String[] entries = { "Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6" }; DefaultListModel