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);
有帮助吗?

解决方案

Use :

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

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top