How To Disable Multiselect of Listbox (Jlist) in Java
Asked Answered
E

2

6

How do I disable multiselect in listbox (Jlist) of java?

Code:

configId.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
defaultModel = new FTCSDefaultListModel();
defaultModel.addElement(cecfgVo.getConfigIdList());
configId = new FTCSList(defaultModel);
configId.setVisibleRowCount(10);
JScrollPane pane = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pane.setPreferredSize(new Dimension(100,100));
pane.setViewportView(configId);
Eyelid answered 9/5, 2014 at 5:7 Comment(0)
I
15

Use :

JList list = new JList(); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

More informations on docs.oracle.com : jList : setSelectionMode

Iota answered 7/5, 2015 at 13:33 Comment(0)
D
-3

Use setEnabled(boolean enabled) inherited by JLIst from JComponent to enable / disable it like this:

yourJlistObject.setEnabled(false);

From JComponent JavaDocs:

public void setEnabled(boolean enabled) Sets whether or not this component is enabled. A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input. Note: Disabling a component does not disable its children. Note: Disabling a lightweight component does not prevent it from receiving MouseEvents.

Dumbfound answered 9/5, 2014 at 5:12 Comment(1)
Why is this the correct answer? This will disable the whole Jlist, not multiple selection.Geneviegenevieve

© 2022 - 2024 — McMap. All rights reserved.