How to deselect Jlist item on some event in java - java

could any one suggest me any method or any other way in java that i could deselect my jlist item when some event occurs?
i tried this but this does not seem to work
myJList.setSelectedIndex(-1);
myJList.ensureIndexIsVisible(-1);

Try myJList.clearSelection(); It clears the selection(s) on your JList object.
If multiple list items selected together, they can also be deselected via this method.

Related

Is there an way to remove a JTextFiled if a certain item in JComboBox is selected in java?

I have a registration panel where if student, instructor, and course administrator can register so if student is selected it should show something like this when Student is selected:
and should show like this if other two are selected like this when any of other two are selected:
I tried using if condition on the selected item in where I have added those text fields but it seems it only works at the beginning of the program when I run it on the basis of what is pre-selected and does not change when I select other items in JComboBox. Is there a solution to this?
You can achieve this in different ways. One of such ways is to use Action Listeners. A JComboBox object generates an action event when a selection is made (see Handling Events on a Combo Box).
In your case, you need to trigger an event based on the selection made in a combo box. This action should change the visibility of components in your panel, which are simply changing the visible attribute from true to false (or vice versa) depending on the selection made.

Select item in JList using code

I want to be able to write a statement that manually selects an item in a JList, such as:
JList myList = new JList(items);
myList.selectValueAt(index);
Documentation of JList:
The selection state of a JList is managed by another separate model, an instance of ListSelectionModel. JList is initialized with a selection model on construction, and also contains methods to query or set this selection model. Additionally, JList provides convenient methods for easily managing the selection. These methods, such as setSelectedIndex and getSelectedValue, are cover methods that take care of the details of interacting with the selection model. By default, JList's selection model is configured to allow any combination of items to be selected at a time; selection mode MULTIPLE_INTERVAL_SELECTION. The selection mode can be changed on the selection model directly, or via JList's cover method. Responsibility for updating the selection model in response to user gestures lies with the list's ListUI.
In your case:
myList.setSelectionIndex(index);
You need to get the ListSelectionModel from the JList. With that, you can modify the selection:
...
ListSelectionModel sm = myList.getSelectionModel();
sm.clearSelection(); // clears the selection
sm.setSelectionInterval(index, index); // Sets a selection interval
...
While the ListSelectionModel allows fine grained control over the selection behaviour of the JList, JList itself also provides convenient methods like JList.setSelectedIndex() to simply modify the selected elements.
You can use
jList.setSelectedValue(string, rootPaneCheckingEnabled)
or
jList.setSelectedIndex(index);

How to select an Item from a JList among other JLists in the same JPanel

I'm working on a small File Management program in Eclipse(Version:Kepler). Also am new to Java Programming.
I designed a JPanel in a JFrame, where there are two JScrollPanes each containing a JList. The two JLists are programmed to show a list of files in two separate folders(i.e. JList1 shows list of files in Folder A and JList2 shows list of files in Folder B). Also there is a button for selection.
I need to program the button in such a way that only one Item is selected i.e. if I select an Item in JList1 then only that Item gets selected, not any Item in JList2 and vice-versa.
Its something to do with focus methods but I don't know how. I've searched the web, but search-results show item selection in one JList (not two JLists in the same panel).
Sample Codes would be appreciated.
PanelBrowser is an example that uses ListSelectionModel.SINGLE_SELECTION to preclude multiple selection.
Addendum: How do I determine that the currently selected Item is of JList1?
You can use a ListSelectionListener, as shown in the example cited above and How to Write a List Selection Listener. Also consider Action, shown here.

Show/Hide JMenuItems in a JPopupMenu based on a JTree's selection

I have a JTree which displays a JPopupMenu when I right click nodes in the JTree. What is the best way to hide/show or enable/disable certain JMenuItems in my JPopupMenu based on the node selected in the JTree?
The first solution that came to mind was to add a TreeSelectionListener to the JTree for the 'value changed' event. In the event handling code I would use the TreeSelectionEvent's getNewLeadSelectionPath() method to get the path of the most recent selection, and use the resulting TreePath object's getLastPathComponent() to get the selected node. From here I would have a series of IF statements that access my JPopupMenu object and perform the modifications necessary to hide/show specific JMenuItems.
However, something feels off about this, and so I decided I would ask SO if there was a better approach.
The way that I chose to tackle this within my own app was to use the "userObject" property of the DefaultMutableTreeNode class which allows you to just store any data you want along with your node. I have a variety of types of things that extend from an abstract base class which defines a "createPopupMenu()" method. Then, in the selection listener (just as you described in your question) I get the user object and ask it to create a popup menu appropriate for the selected object and display that.
Getting the selected tree node is straight forward and should work as you described it. To modify the popup menu I would recommend using Actions. This way you wouldn’t have to modify your live menu and could also add e.g. a JToolBar that contains the same actions that react the same way the items in your menu do.

JComboBox getSelectedItem() not changing

I'm binding a JComboBox to an observable List. I clear and add Objects form the observable list. This works fine and reflects the changes in the JComboBox correctly.
The problem is I can't select the content of the list using the mouse although the newly added items are correctly displayed in the combobox when expanded. The getSelectedItem() is always stuck on the first item in the list.
List<Object> sourceListObserver =
ObservableCollections.observableList(new ArrayList<Object>());
The binding is done using Netbeans GUI designer.
I have now also tried using DefaultComboBoxModel.
DefaultComboBoxModel model = new DefaultComboBoxModel();
wireSourceComboBox.setModel(model);
Using wireSourceComboBox.removeAllItems(); and wireSourceComboBox.addItem(qb);
Still the same behaviour after removing and adding objects to the combobox.
without a working example it's hard to prove, but you probably need to fire the event listeners to have the UI track your model correctly.
see AbstractListModel.fireContentsChanged

Categories

Resources