JList-以红色突出显示特定的单元格 [英] JList - Highlight specific cells in red

查看:83
本文介绍了JList-以红色突出显示特定的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JList on a form. When the form loads, the JList is populated with items from my array. The items are products and have a "quantity in stock" number next to the product details. In the code below I find the stock number and if it is less than 5 I want that line to be highlighted in red.

如果数量少于5,此刻我的WHOLE Jlist用红色突出显示.帮助! 我对Java很陌生,所以请尽可能简单地解释一下! 如果有人可以解释为什么为什么我的代码不能正常工作,那就太好了-我真的不了解很多单元格渲染"的内容-我昨天才碰到它.

At the moment my WHOLE Jlist is being highlighted in red if there is any quantity which is less than 5. Help!! I'm pretty new to Java so please explain as simply as possible! If someone could explain why my code isn't working properly as well that would be great - I really don't understand quite a lot of the "Cell Rendering" stuff - I only came across it yesterday.

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 );  
        } 

推荐答案

您正在尝试做更多的事情.您根本不需要Map.见下文,非常简单.

You're trying to do way to much. You don't need the Map at all. See below, very simple.

当前全部变为红色的原因是因为setBackgroundif之外.因此,无论发生什么情况,它都会变成红色.您可以在此处中找到更多有关如何使用呈现列表

The reason you currently getting all colored red is because you have setBackground outside of the if. So no matter what happens, it will be red. You can see more here on how to use a render for a list

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));
            }
        });
    }
}

这篇关于JList-以红色突出显示特定的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆