【问题标题】:JList - Highlight specific cells in redJList - 以红色突出显示特定单元格
【发布时间】:2014-03-04 19:00:07
【问题描述】:

我在表单上有一个 JList。当表单加载时,JList 会填充我数组中的项目。这些物品是产品,并且在产品详细信息旁边有一个“库存数量”编号。 在下面的代码中,我找到了股票编号,如果它小于 5,我希望该行以红色突出显示。

目前,如果有任何数量少于 5,我的整个 Jlist 将以红色突出显示。帮助! 我对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 );  
        } 

【问题讨论】:

  • 什么时候将背景设置为红色以外的颜色?
  • 如果库存少于 5 个,则背景应为红色。如果有 5 个或更多库存,则背景不应为红色。
  • 好的,但是你什么时候真正将背景设置为红色以外的任何东西?在我看来,无论如何您都将其设置为红色。
  • 不,红色应该是唯一设置的颜色。我真的不明白代码 - 我以前从未使用过 HashMap 或 CellRenderer
  • 您需要将颜色设置为红色以外的颜色,以使颜色不是红色。说真的,只需将第一个红色更改为蓝色即可了解我的意思。

标签: java swing rendering cell jlist


【解决方案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。这只是几个代码 sn-ps。

老实说,我认为您要做的是为您的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());

【讨论】:

    【解决方案2】:

    您正在尝试做很多事情。你根本不需要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));
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2011-04-12
      • 1970-01-01
      • 2015-11-03
      • 2020-03-29
      相关资源
      最近更新 更多