如何使用CSS在JavaFX中更改ListView单元格的文本颜色 [英] How to change ListView cell's text color in JavaFX using css

查看:411
本文介绍了如何使用CSS在JavaFX中更改ListView单元格的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个列表,其中包含数据库中的所有名称。这些名称代表在公司工作或曾经在公司工作的员工。我已经看到可以更改单元格的文本颜色,但是当我应用它时,它会更改所有文本。我真正想要的是更改不再工作的客户端的颜色。

So I have a list which is full of names from my database. Those names represent employees that work or used to work in the firm. I've seen that is possible to change cell's text color but when I apply it, it changes all of them. What I really want is to change color of the clients that are no more working.

我在List中添加了 not active,因此我可以将 active和非活动客户端。

I've added "not active" in List so I can separate "active" and "not active" clients.

以下是在ArrayList中添加员工姓名的代码:

Here is the code where I add employees names in the ArrayList:

public  ArrayList<String> search(String unit, String name)
    {
        ArrayList<String> temp= new ArrayList<String>();
        String sql="";
        if(unit=="all units")
            sql="SELECT * FROM employees WHERE name='"+name+"';";
        else
            sql="SELECT * FROM employees WHERE unit='"+unit+"' AND name='"+name+"';";
        try {
        ResultSet rs=bp.select(sql);
            while(rs.next())
            {
                if(rs.getInt("active")==1)
                temp.add(rs.getString("name"));
                else
                    temp.add(rs.getString("name")+" - not active");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Collections.sort(temp);
        return temp;
    }

这是我在ListView中添加ArrayList的代码:

And here is the code where I add that ArrayList in my ListView:

for (String item: u.search(unitCB.getValue(),nameTF.getText()))
                            SearchLW.getItems().add(item);

现在,我可以将所有使用css的列表中的文本颜色更改为红色不再活跃的员工?如果不是这样,则另一种有关如何更改此特定ListView单元格文本颜色的解决方案将很有帮助。

Now, I wounder is it possible to change text-color in this list using css to red for all employees that are not active anymore? If it's not, another solution on how to change this specific ListView cell's text-color would be helpful.

推荐答案

您可以使用CSS伪类,用于表示无效样式并在CSS文件中设置样式。使用您当前的代码,它看起来像是:

You can use a CSS pseudoclass to represent an "inactive" style and set the style in a CSS file. With your current code it would look something like:

ListView<String> listView = new ListView<>();
PseudoClass inactive = PseudoClass.getPseudoClass("inactive");
listView.setCellFactory(lv -> new ListCell<String>() {
    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        setText(empty ? null : item);
        pseudoClassStateChanged(inactive, item != null && item.endsWith(" - not active"));
    }
});

然后在外部CSS文件中,您可以定义所需的样式:

Then in an external css file you can define the style you want:

.list-cell:inactive {
    -fx-text-fill: red ;
}

仅在此处测试字符串值非常脆弱(想象一下,如果您试图使应用程序国际化,或者当老板告诉您更改演示文稿以使未激活 用括号括起来时,等等。)我建议创建一个 Employee 类:

Just testing the string value here is pretty fragile (imagine what would happen if you tried to internationalize the application, or when your boss told you to change the presentation so that "not active" was in parentheses, or whatever...). I would recommend creating an Employee class:

public class Employee {
    private final String name ;
    private final boolean active ;

    public Employee(String name, boolean active) {
        this.name = name ;
        this.active = active ;
    }

    public String getName() {
        return name ;
    }

    public boolean isActive() {
        return active ;
    }
}

然后您这样做

ListView<Employee> listView = new ListView<>();
PseudoClass inactive = PseudoClass.getPseudoClass("inactive");
listView.setCellFactory(lv -> new ListCell<Employee>() {
    @Override
    protected void updateItem(Employee employee, boolean empty) {
        super.updateItem(employee, empty);
        if (empty) {
            setText(null);
            pseudoClassStateChanged(inactive, false);
        } else {
            if (employee.isActive()) {
                setText(employee.getName());
                pseudoClassStateChanged(inactive, false);
            } else {
                setText(employee.getName() + " - not active");
                pseudoClassStateChanged(inactive, true);
            }
        }
    }
});

并对您发布的方法进行明显更新:

and make the obvious update to the method you posted:

public  ArrayList<Employee> search(String unit, String name) {
    ArrayList<Employee> temp= new ArrayList<>();
    String sql="SELECT * FROM employees WHERE unit like ? AND name=?";

    try (PreparedStatement pStmnt = conn.prepareStatement(sql)) {

        if("all units".equals(unit))
            pStmnt.setString(1, "%");
        else
            pStmnt.setString(1, unit);

        pStmnt.setString(2, name);
        ResultSet rs=pStmnt.executeQuery();
        while(rs.next())
        {
            temp.add(new Employee(rs.getString("name"), rs.getInt("active")==1);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    temp.sort(Comparator.comparing(Employee::getName));
    return temp;
}

这篇关于如何使用CSS在JavaFX中更改ListView单元格的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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