Java – disable items in JList
•
Java
I'm using JList as part of the wizard to display all the steps to be performed (it also allows you to click one step to enter it) According to the previous steps, some steps will not necessarily be required These are the steps I disabled in the list that don't apply
How do i disable (block selection) some items in the list? Is there a better method than subclassing JList and covering each selection related method?
Solution
You must implement defaultlistselectionmodel, then you can set flag if isenabled
Simple example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JListDisabledItemDemo implements ItemListener,Runnable { private JFrame f = new JFrame("Colors"); private static final String ITEMS[] = {" black "," blue "," green "," orange "," purple "," red "," white "," yellow "}; private JList jList; private JCheck@R_32_2419@[] check@R_32_2419@es; private boolean[] enabledFlags; @Override public void run() { JPanel pnlEnablers = new JPanel(new GridLayout(0,1)); pnlEnablers.setBorder(BorderFactory.createTitledBorder("Enabled Items")); check@R_32_2419@es = new JCheck@R_32_2419@[ITEMS.length]; enabledFlags = new boolean[ITEMS.length]; for (int i = 0; i < ITEMS.length; i++) { check@R_32_2419@es[i] = new JCheck@R_32_2419@(ITEMS[i]); check@R_32_2419@es[i].setSelected(true); check@R_32_2419@es[i].addItemListener(this); enabledFlags[i] = true; pnlEnablers.add(check@R_32_2419@es[i]); } jList = new JList(ITEMS); jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jList.setSelectionModel(new DisabledItemSelectionModel()); jList.setCellRenderer(new DisabledItemListCellRenderer()); jList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { System.out.println("selection"); } } }); JScrollPane scroll = new JScrollPane(jList); scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); Container contentPane = f.getContentPane(); contentPane.setLayout(new GridLayout(1,2)); contentPane.add(pnlEnablers); contentPane.add(scroll); f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); f.setLocation(240,280); UIManager.put("List.background",Color.lightGray); UIManager.put("List.selectionBackground",Color.orange); UIManager.put("List.selectionForeground",Color.blue); UIManager.put("Label.disabledForeground",Color.magenta); SwingUtilities.updateComponentTreeUI(f); f.pack(); javax.swing.SwingUtilities.invokelater(new Runnable() { @Override public void run() { f.setVisible(true); } }); } @Override public void itemStateChanged(ItemEvent event) { JCheck@R_32_2419@ check@R_32_2419@ = (JCheck@R_32_2419@) event.getSource(); int index = -1; for (int i = 0; i < ITEMS.length; i++) { if (ITEMS[i].equals(check@R_32_2419@.getText())) { index = i; break; } } if (index != -1) { enabledFlags[index] = check@R_32_2419@.isSelected(); jList.repaint(); } } public static void main(String args[]) { SwingUtilities.invokelater(new JListDisabledItemDemo()); } private class DisabledItemListCellRenderer extends DefaultListCellRenderer { private static final long serialVersionUID = 1L; @Override public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus) { Component comp = super.getListCellRendererComponent(list,value,index,false,false); JComponent jc = (JComponent) comp; if (enabledFlags[index]) { if (isSelected & cellHasFocus) { comp.setForeground(Color.black); comp.setBackground(Color.red); } else { comp.setForeground(Color.blue); } if (!isSelected) { if ((value.toString()).trim().equals("yellow")) { comp.setForeground(Color.orange); comp.setBackground(Color.magenta); } } return comp; } comp.setEnabled(false); return comp; } } private class DisabledItemSelectionModel extends DefaultListSelectionModel { private static final long serialVersionUID = 1L; @Override public void setSelectionInterval(int index0,int index1) { if (enabledFlags[index0]) { super.setSelectionInterval(index0,index0); } else { /* * The prevIoUsly selected index is before this one,* so walk forward to find the next selectable item. */ if (getAnchorSelectionIndex() < index0) { for (int i = index0; i < enabledFlags.length; i++) { if (enabledFlags[i]) { super.setSelectionInterval(i,i); return; } } } /* * Otherwise,walk backward to find the next selectable item. */ else { for (int i = index0; i >= 0; i--) { if (enabledFlags[i]) { super.setSelectionInterval(i,i); return; } } } } } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码