Can I set the selected item for a JList without having an event thrown to the listeners?

You can remove all ListSelectionListener from the list, make a selection and then add them again. You can create your own ListSelectionModel with a method that doesn't throw the event and set it as a selection model to your JList, and then use getSelectionModel(). YourSelectIndexMethod(index).

You can also divert all your other methods of selection to the list, just find the corresponding entry if selecting the page by other means and select the item in the list. This way the item is selected and the page is loaded once. Code for option 2: public class ListTest extends JPanel{ private static final String items = new String{"1", "2", "3"}; private JList mylist; private JComboBox myCombo; private JTextArea myTA; public ListTest() { setLayout(new BorderLayout()); myCombo = new JComboBox(items); myCombo.

AddActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ valueSelectedCombo(myCombo. GetSelectedIndex()); } }); JPanel pn = new JPanel(); pn. SetLayout(new BoxLayout(pn, BoxLayout.

X_AXIS)); pn. Add(myCombo); pn. Add(Box.

CreateHorizontalGlue()); pn. Add(new JButton(new AbstractAction("Clear"){ @Override public void actionPerformed(ActionEvent e){ myTA. SetText(""); } })); add(pn, BorderLayout.

NORTH); add(new JScrollPane(getJList()), BorderLayout. WEST); add(new JScrollPane(myTA = new JTextArea()), BorderLayout. CENTER); } private void valueSelectedList(int index){ myTA.

SetText(myTA.getText() + "\n" + itemsindex); } private void valueSelectedCombo(int index){ myTA. SetText(myTA.getText() + "\n" + itemsindex); ((CustomSelectionModel)mylist. GetSelectionModel()).

SetSelectionSilent(index); } private JList getJList(){ if (mylist == null){ mylist = new JList(items); mylist. SetSelectionModel(new CustomSelectionModel()); mylist. AddListSelectionListener(new ListSelectionListener(){ @Override public void valueChanged(ListSelectionEvent e){ if (!e.

GetValueIsAdjusting()){ valueSelectedList(mylist. GetSelectedIndex()); } } }); mylist. SetSelectionMode(ListSelectionModel.

SINGLE_SELECTION); mylist. SetPreferredSize(new Dimension(100, 106)); } return mylist; } private static class CustomSelectionModel extends DefaultListSelectionModel{ private boolean isSilent = false; public void setSelectionSilent(int firstIndex){ isSilent = true; setSelectionInterval(firstIndex, firstIndex); isSilent = false; } protected void fireValueChanged(int firstIndex, int lastIndex, boolean isAdjusting){ if (isSilent){ return; } super. FireValueChanged(firstIndex, lastIndex, isAdjusting); } } public static void main(String args){ JFrame frame = new JFrame("test"); frame.

SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); // Add content to the window. Frame.

Add(new ListTest()); // Display the window. Frame.pack(); frame. SetSize(300, 200); frame.

SetVisible(true); } }.

3. I'm not sure I understand this. I want to have the page selected in the JList, so simply bypassing this is not an option.2.

If I do that, wouldn't it disable all ListSelectionEvents? Thus, when the user selects a page, no events will be triggered and no page would be loaded.1. How do I remove all listeners?

I can't find a method like that. The only way I see it is to getListSelectionListeners() and remove them one by one. Really cumbersome.... – Andrei Vajna II Jun 22 '10 at 13:06 ...but other than that, it works.

– Andrei Vajna II Jun 22 '10 at 13:16 3 - all your other then list selections just go to the list and select the corresponding item. This way, the item in the list is selected and the page is loaded once. 2.

You create a custom method in the model and call it when you just have to show the selection. No other methods are affected. 1.

Yes, that's the only way. The easiest way, actually, would be to just load the page once unless the reload it explicitly requested. – Taisin Jun 22 '10 at 13:28 I updated the code, so you can see one way to do the option 2.

– Taisin Jun 22 '10 at 13:32 +1 Very nice example. Note text. Append("\n" + itemsindex) as an alternative in valueSelectedCombo().

– trashgod Jun 22 '10 at 15:27.

It looks like setSelectedIndex() is just a convenient way to set the selection in the ListSelectionModel. Maybe your ListModel could flag or cache the result so it won't get loaded a second time.

The pages are loaded when selected. I don't think it has to do with the ListModel. – Andrei Vajna II Jun 22 '10 at 13:00.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions