2011-12-01 113 views
9

我使用JList作爲嚮導的一部分來顯示要執行的所有步驟(它還允許單擊一個步驟來執行)。根據前面的步驟所做的一些步驟,並不總是需要一些步驟。這是我想在列表中禁用的這些不適用的步驟。在JList中禁用項目

如何禁用(阻止選擇)列表中的某些項目?有沒有比繼承JList和覆蓋每個選擇相關方法更好的方法?

回答

15

你必須實現DefaultListSelectionModel,那麼你可以設置標誌,如果的IsEnabled與否

enter image description here

簡單的例子

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 JCheckBox[] checkBoxes; 
    private boolean[] enabledFlags; 

    @Override 
    public void run() { 
     JPanel pnlEnablers = new JPanel(new GridLayout(0, 1)); 
     pnlEnablers.setBorder(BorderFactory.createTitledBorder("Enabled Items")); 
     checkBoxes = new JCheckBox[ITEMS.length]; 
     enabledFlags = new boolean[ITEMS.length]; 
     for (int i = 0; i < ITEMS.length; i++) { 
      checkBoxes[i] = new JCheckBox(ITEMS[i]); 
      checkBoxes[i].setSelected(true); 
      checkBoxes[i].addItemListener(this); 
      enabledFlags[i] = true; 
      pnlEnablers.add(checkBoxes[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) { 
     JCheckBox checkBox = (JCheckBox) event.getSource(); 
     int index = -1; 
     for (int i = 0; i < ITEMS.length; i++) { 
      if (ITEMS[i].equals(checkBox.getText())) { 
       index = i; 
       break; 
      } 
     } 
     if (index != -1) { 
      enabledFlags[index] = checkBox.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; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+1

當您想要顯示禁用時,這是一個很好的答案項目。有一些使用情況實際上更好。我有這樣一個用例,這個工作就像一個魅力。 –

3

如果您從列表模型中刪除了不適用的項目,而不是禁用它們,那將更容易。用戶是否真的想看到殘疾人物品?我不相信它會增加任何價值,但會增加視覺混亂。

+1

這是一個很好的觀點。經過一番思考,我決定將它們變灰,但讓它們被選中。他們在使用next/previous時會被跳過,但如果他們故意進入灰色步驟,將會出現一條消息,說明「啓用」步驟需要執行的操作。 –

1

我想一個JList與細胞不能選擇並透明。所以這裏是我做的:

class DisabledItemListCellRenderer extends JLabel implements ListCellRenderer<Object> { 

    private static final long serialVersionUID = 1L; 

    public DisabledItemListCellRenderer() { 
     setOpaque(false); 
    } 

    @Override 
    public Component getListCellRendererComponent(JList<?> list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 
     String txt = (String) value; 
     setText(txt); 

     return this; 
    }  
}