【问题标题】:JavaFX - How to change TableView color of selected unfocused row?JavaFX - 如何更改选定未聚焦行的 TableView 颜色?
【发布时间】:2015-12-04 02:05:30
【问题描述】:

无论我做什么 - 行的颜色保持不变并且呈灰色。这些更改仅适用于 TableView 处于焦点时。

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

.table-row-cell:selected { -fx-background-color: red; }

当不在焦点上时,似乎没有任何工作和影响行。

【问题讨论】:

  • 尝试使用 .table-row-cell:filled:selected {...}
  • 这对我不起作用,我使用 TableView.setStyle("x");这可能是它不起作用的原因吗?
  • 在 TableView.setStyle("x"); ,如果 x=.table-row-cell:filled:selected {...} 那么是的,它不会工作。它应该在 css 文件中。

标签: java javafx tableview


【解决方案1】:

问题

您想要更改选择栏的颜色以实现 TableView 的焦点和非焦点状态

解决方案

modena.css(默认 JavaFX 样式表)中有一个 -fx-selection-bar-fx-selection-bar-non-focused 定义。它们都在一个名为 Theming 的部分中。因此,它们旨在成为一个多变的“全球”主题的一部分。如果您为整个应用程序更改它们,它不仅会改变 TableView 为选择着色的方式,它甚至会改变菜单、列表等。所以您应该注意这一点。

但是从上面的 cmets 中应该很清楚,您尝试通过在 TableView 实例上调用方法 .setStyle() 来添加样式。如果这样做,通过这两个属性更改颜色将导致仅更改 TableView 选择栏的颜色。

Minimal, Complete, and Verifiable example 可能类似于以下代码:

TableRowColor.java

 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 项目结构

Netbeans 中的 JavaFX 应用程序项目应如下所示:

工作应用程序

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

在场景生成器中设置样式

在 Scene Builder 中,您可以通过打开 Inspector 为 TableView 设置相同的样式,而不是 TableView 的属性并将以下内容添加到样式框中:

【讨论】:

  • 它就像一个魅力。我们还可以在颜色规范中使用 RGB 值 "-fx-selection-bar: rgb(176, 210, 242); -fx-selection-bar-non-focused: rgb(176, 210, 242);"
猜你喜欢
  • 2017-04-25
  • 2012-03-02
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2011-04-24
  • 2017-01-06
  • 1970-01-01
相关资源
最近更新 更多