Select item in JList using code - java

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);

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.

Related to checkbox in jtable

I am using a table to display data.
I am providing checkbox to each row of a table to perform some operations based on selection. When I did like that, I am able to check multiple rows.
But my requirement is, at any point of time I should check only one checkbox. To be precise, I need the behavior of Buttongroup to all checkboxes in table.
How can I do this?
If you really want to use checkboxes, I assume your TableModel holds a boolean for those checkboxes. It should be trivial to move the logic for the single selection to the TableModel.
If you do not need the checkboxes but just want to operate on the selected rows (see JTable#getSelectedRows), you can adjust the ListSelectionModel which is present on the JTable to only allow for single selection (see ListSelectionModel#SINGLE_SELECTION)
CheckOne is a complete example that simply clears all check boxes in a specific column and sets the new value. This related example uses JRadioButton.

Java Swing - inform GUI about changes to the model

I have a column in JTable that binds to the underlying boolean property on a list of business objects. I also have a combobox, which should select which items should be selected. I basically added the following code as a handler to the combobox:
macroCombo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox) e.getSource();
Predicate filter = (Predicate) comboBox.getSelectedItem();
for(SelectableKey key : tableEntries){
key.setSelected(filter.evaluate(key));
}
}
});
I also have a few other controls I want to change based on the value. At the moment, only a few cells in the table change their state to be selected/deselected. Only when I click on the row, or select multiple rows, the UI updates itself. Is there a call from the handler I need to make to tell GUI to redraw itself? ALos, if I modify other controls than JTable, how would I tell them to change their state?
Thanks
When you update a value in your TableModel, the model should fire a corresponding TableModelEvent (type: UPDATE).
If your TableModel for example extends from AbstractTableModel, you can call the fireTableRowsUpdated method after you have made the changes.
Another approach is a TableModel which knows when it gets updated (for example by adding listeners to the objects it contains). This allows other code to simply update the objects contained in the TableModel, without having knowledge of the TableModel. The TableModel itself will then fire the event when it detects changes made to the objects it contains.
I prefer the second approach, as this avoids that I have to pass that TableModel around to all my other classes.
Consult the table tutorial for more information.

Filter Combobox Data in Java

In java, let's say there are two jpanels, when I click button 'A' on Panle'1', it will show panel '2'. In panel '2', there are two comboboxes and I finished all necessary coding. But one thing to filter is combobox'1' will show only those data who has 'book'prefix. & combobox '2' will show only those without 'book prefix'. How should I filter it?
The ComboBoxModel controls the content of your JComboBox. The only way to filter that I know of is to not have the unwanted values in your models. You can filter them out when creating the models.
Assuming you have all the desired values in a List, I would use the GlazedLists library. It provides observable lists and lets you do all kinds of interesting stuff with them, like filtering and sorting. Of particular interest in this case is FilterList: you supply it an EventList and a Matcher which decides how to filter the EventList. The FilterList acts like a view on the EventList, meaning that if you change the EventList, the FilterList will reflect this. With this FilterList, you can then make a EventComboBoxModel and use that as your model.

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