gpt4 book ai didi

java - 如何使用 css 在 JavaFX 中更改 ListView 单元格的文本颜色

转载 作者:行者123 更新时间:2023-11-30 06:53:04 25 4
gpt4 key购买 nike

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

我在列表中添加了“不活跃”,这样我就可以区分“活跃”和“不活跃”的客户。

这是我在 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 的代码:

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

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

最佳答案

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

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 文件中你可以定义你想要的样式:

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

只是在这里测试字符串值是非常脆弱的(想象一下如果您尝试将应用程序国际化,或者当您的老板告诉您更改演示文稿以便 "not active" 会发生什么括号,或其他...)。我建议创建一个 Employee 类:

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

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

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

关于java - 如何使用 css 在 JavaFX 中更改 ListView 单元格的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com