2014-03-04 100 views
0

我在表單上有一個JList。當表單加載時,JList將使用我的數組中的項目填充。這些物品是產品,並在產品詳細信息旁邊有「庫存數量」編號。 在下面的代碼中,我找到了股票編號,如果它小於5,我希望該行以紅色突出顯示。JList - 突出顯示紅色的特定單元格

目前我的整體Jlist被突出顯示爲紅色,如果有任何數量小於5。 我對Java很新,所以請儘可能簡單地解釋一下! 如果有人可以解釋爲什麼我的代碼工作不正常,那將會很棒 - 我真的不理解很多「細胞渲染」的東西 - 我昨天才看到它。

public void lowStock(){ 
    DefaultListModel<String> list = new DefaultListModel<String>(); 
    list = (DefaultListModel) lstProducts.getModel(); 
    int listSize = list.getSize(); 

    for (int i=0; i<listSize; i++){ 
     String element = list.get(i); 
     int blankSpace = element.lastIndexOf(" "); 
     String quantity = element.substring(blankSpace).trim(); 
     final int intQuantity = Integer.parseInt(quantity); 

     if (intQuantity < 5){ 
      ListCellRenderer lstclrnd; 
      lstProducts.setCellRenderer(new DefaultListCellRenderer(){ 

      //element.setBackGround(Color.red); 
     }); 
    } 
} 

class MyListRenderer extends DefaultListCellRenderer 
{ 
    private HashMap theChosen = new HashMap(); 

    public Component getListCellRendererComponent(JList list, 
      Object value, int index, boolean isSelected, 
      boolean cellHasFocus) 
    { 
     super.getListCellRendererComponent(list, value, index, 
       isSelected, cellHasFocus); 

      theChosen.put(value, "chosen"); 
      setBackground(Color.red); 
      if(theChosen.containsKey(value)) 
     { 
      setBackground(Color.red); 
     } 
+0

渲染你什麼時候將背景設置爲紅色以外的東西嗎? –

+0

如果庫存少於5件,背景應爲紅色。如果有5個或更多庫存,背景不應該是紅色。 – ibexe

+0

好的,但是你什麼時候將背景設置爲除紅色之外的其他任何東西?在我看來,無論如何,你都將它設置爲紅色。 –

回答

1

你的問題出在下面的代碼段:

public Component getListCellRendererComponent(JList list, 
     Object value, int index, boolean isSelected, 
     boolean cellHasFocus) { 

    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 

    theChosen.put(value, "chosen"); 
    setBackground(Color.red); //MOST LIKELY THIS LINE RIGHT HERE 
    if(theChosen.containsKey(value)) { 
     setBackground(Color.red); 
    } 
    ... 

沒有該行setBackground(Color.red);,沒有顏色應設置。

這是很難看到究竟是怎麼回事 - 你應該提交一個SSCCE。這只是幾個代碼片段。

老實說,我認爲你要做的是爲你的JList設置一個ListCellRenderer。像下面這樣的東西就足夠了。

class MyCellRenderer extends JLabel implements ListCellRenderer<Object> { 
    public MyCellRenderer() { 
     setOpaque(true); 
    } 

    public Component getListCellRendererComponent(JList<?> list, 
                Object value, 
                int index, 
                boolean isSelected, 
                boolean cellHasFocus) { 

     //I don't know why you would have leading whitespace here... but w/e 
     //This probably needs modification depending on your data 
     String quantity = value.toString().substring(blankSpace).trim(); 
     setText(quantity); 
     int intQuantity = Integer.parseInt(quantity); 

     Color background; 
     Color foreground; 

     if (intQuantity < 5) { 
      background = Color.RED; 
      foreground = Color.WHITE; 

     } else { 
      background = Color.WHITE; 
      foreground = Color.BLACK; 
     } 

     setBackground(background); 
     setForeground(foreground); 

     return this; 
    } 
} 

然後,你JList,你可能初始化後馬上,需要做到以下幾點:

myJList.setCellRenderer(new MyCellRenderer()); 
1

你正在試圖做的辦法不多。根本不需要Map。看下面,很簡單。

您目前全部呈紅色的原因是因爲您在if以外有setBackground。所以無論發生什麼,它都會變成紅色。你可以看到更多的here如何使用列表

import java.awt.Color; 
import java.awt.Component; 
import javax.swing.DefaultListCellRenderer; 
import javax.swing.JList; 
import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 

public class ListColorRed { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       Integer[] nums = {10, 2, 5, 8, 2, 9, 2, 8, 10, 4, 6}; 
       JList list = new JList(nums); 
       list.setCellRenderer(new DefaultListCellRenderer() { 

        @Override 
        public Component getListCellRendererComponent(JList list, 
          Object value, int index, boolean isSelected, 
          boolean cellHasFocus) { 

         super.getListCellRendererComponent(list, value, index, 
           isSelected, cellHasFocus); 

         Integer num = (Integer) value; 

         if (num < 5) { 
          setBackground(Color.RED); 
         } 

         return this; 
        } 
       }); 

       JOptionPane.showMessageDialog(null, new JScrollPane(list)); 
      } 
     }); 
    } 
} 

enter image description here

相關問題