JavaFX-如何更改所选未聚焦行的TableView颜色?

JavaFX-如何更改所选未聚焦行的TableView颜色?,java,javafx,tableview,Java,Javafx,Tableview,无论我做什么-行的颜色保持不变,并具有灰色。这些更改仅在TableView处于焦点时有效 我已经尝试了我在网上找到的所有其他建议,例如来自另一个线程的解决方案: 。表格行单元格:选中{-fx背景色:红色;} 当不对焦时,似乎没有任何东西起作用并影响行。问题 要更改TableView的聚焦和非聚焦状态的选择栏的颜色 解决方案 modena.css(默认JavaFX样式表)中有一个-fx选择栏和-fx选择栏非焦点定义。它们都在一个叫做主题化的部分。因此,它们是一个多变的“全球”主题的一部分。如果您为

无论我做什么-行的颜色保持不变,并具有灰色。这些更改仅在TableView处于焦点时有效

我已经尝试了我在网上找到的所有其他建议,例如来自另一个线程的解决方案:

。表格行单元格:选中{-fx背景色:红色;}

当不对焦时,似乎没有任何东西起作用并影响行。

问题 要更改TableView的聚焦和非聚焦状态的选择栏的颜色

解决方案 modena.css(默认JavaFX样式表)中有一个
-fx选择栏
-fx选择栏非焦点
定义。它们都在一个叫做主题化的部分。因此,它们是一个多变的“全球”主题的一部分。如果您为整个应用程序更改它们,它不仅会更改TableView为选择着色的方式,甚至还会更改菜单、列表等,因此您应该知道这一点

但是从上面的注释可以清楚地看出,您试图通过调用TableView实例上的.setStyle()方法来添加样式。如果这样做,通过这两个属性更改颜色将导致仅更改TableView选择栏的颜色

代码可能类似于以下代码:

TableRowColor.java
package tablerowcolor;
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TableRowColor扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
观察者
=FXCollections.observableArrayList(
新人(“先生”、“托比”),
新人(“海军上将”、“冯·施耐德”),
新人(“先生”,“波默罗伊”),
新人(“先生”,“温特巴顿”);
TableView TableView=新的TableView(人);
桌面视图。
setStyle(“-fx选择栏:红色;-fx选择栏非聚焦:鲑鱼;”);
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
tableView.getSelectionModel().clearAndSelect(0);
tableView.getColumns().setAll(firstNameCol,lastNameCol);
按钮btn=新按钮();
btn.setText(“聚焦我”);
VBox root=新的VBox();
root.getChildren().addAll(tableView,btn);
场景=新场景(根,300,250);
primaryStage.setTitle(“选择行颜色”);
初级阶段。场景(场景);
primaryStage.show();
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
发射(args);
}
公共阶层人士{
私有最终属性名
=新的SimpleStringProperty(即“名字”);
public void setFirstName(字符串值){
firstNameProperty().set(值);
}
公共字符串getFirstName(){
返回firstNameProperty().get();
}
public StringProperty firstNameProperty(){
返回名字;
}
私有最终StringProperty lastName
=新的SimpleStringProperty(此“姓氏”);
;
public void setLastName(字符串值){
lastNameProperty().set(值);
}
公共字符串getLastName(){
返回lastNameProperty().get();
}
公共StringProperty lastNameProperty(){
返回姓氏;
}
公众人物(字符串名、字符串名){
this.firstName.set(firstName);
this.lastName.set(lastName);
}
}
}
Netbeans项目结构 Netbeans中的JavaFX应用程序项目应如下所示:

工作应用程序 工作应用程序将如下所示:

在场景生成器中设置样式 在场景生成器中,可以通过打开“检查器”而不是“表视图”的属性,并将以下内容添加到样式框中,将相同的样式设置到表视图中:


尝试使用.table行单元格:filled:selected{…}这对我来说不起作用,我使用TableView.setStyle(“x”)来实现;这可能是它不工作的原因吗?在TableView.setStyle(“x”),如果x=.table行单元格:filled:selected{…},则是,它将不起作用。它应该在css文件中。
 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);
    }
  }
}