[英]JavaFX - How to change TableView color of selected unfocused row?
无论我做什么 - 行的颜色保持不变,并具有灰色。 这些更改仅适用于TableView处于焦点时。
我已经尝试过我在网上找到的所有其他建议,例如来自另一个线程的解决方案:
.table-row-cell:selected { -fx-background-color: red; }
似乎没有任何作用,并且当不在焦点时影响行。
您希望更改选择栏的颜色以获得TableView的聚焦和未聚焦状态
有一个-fx-selection-bar
和-fx-selection-bar-non-focused
在modena.css(默认JavaFX的样式表)定义。 它们都在名为Theming的部分。 所以他们应该成为一个多变的“全球”主题的一部分。 如果你为整个应用程序更改它们,它不仅会改变TableView为选择着色的方式,它甚至会改变Menu,List等等。所以你应该知道它。
但是从上面的评论中可以清楚地看到,您尝试通过在TableView实例上调用方法.setStyle()来添加样式。 如果这样做,通过这两个属性更改颜色将导致仅更改TableView选择栏的颜色。
最小,完整和可验证的示例可能类似于以下代码:
package tablerowcolor;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableRowColor extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList<Person> persons
= FXCollections.observableArrayList(
new Person("Sir", "Tobey"),
new Person("Admiral", "von Schneider"),
new Person("Mr.", "Pommeroy"),
new Person("Mr.", "Winterbottom"));
TableView<Person> tableView = new TableView<>(persons);
tableView.
setStyle("-fx-selection-bar: red; -fx-selection-bar-non-focused: salmon;");
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
tableView.getSelectionModel().clearAndSelect(0);
tableView.getColumns().setAll(firstNameCol, lastNameCol);
Button btn = new Button();
btn.setText("Focus me");
VBox root = new VBox();
root.getChildren().addAll(tableView, btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Selection Row Color");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public class Person {
private final StringProperty firstName
= new SimpleStringProperty(this, "firstName");
public void setFirstName(String value) {
firstNameProperty().set(value);
}
public String getFirstName() {
return firstNameProperty().get();
}
public StringProperty firstNameProperty() {
return firstName;
}
private final StringProperty lastName
= new SimpleStringProperty(this, "lastName");
;
public void setLastName(String value) {
lastNameProperty().set(value);
}
public String getLastName() {
return lastNameProperty().get();
}
public StringProperty lastNameProperty() {
return lastName;
}
public Person(String firstName, String lastName) {
this.firstName.set(firstName);
this.lastName.set(lastName);
}
}
}
Netbeans中的JavaFX应用项目应如下所示:
工作应用程序将如下所示:
在Scene Builder中,您可以通过打开Inspector而不是TableView的属性为TableView设置相同的样式,并将以下内容添加到样式框中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.