JavaFX - 如何更改所选未聚焦行的TableView颜色? [英] JavaFX - How to change TableView color of selected unfocused row?

查看:344
本文介绍了JavaFX - 如何更改所选未聚焦行的TableView颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



似乎没有任何效果,并且当不在焦点时影响行。

解决方案

问题



您想要为TableView的聚焦和未聚焦状态更改选择栏的颜色



解决方案



有一个 -fx-selection-bar 和< modena.css 中的code> -fx-selection-bar-non-focused 定义(默认JavaFX样式表)。它们都位于名为 Theming 的部分中。所以他们应该成为一个多变的全球主题的一部分。如果你为整个应用程序更改它们,它不仅会改变TableView为选择着色的方式,它甚至会改变Menu,List等等。所以你应该知道它。



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





工作申请



工作申请表如下:





在场景生成器中设置样式



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




No matter what i do - the color of the row keeps unchanged and has a greyish color. The changes only work for when the TableView is in focus.

I have tried every other suggestion i found online, for example a solution from another thread:

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

Nothing seems to work and affect the rows when not in focus.

解决方案

Problem

You want to change the color of the selection bar for a focused and unfocused state of TableView

Solution

There is a -fx-selection-bar and -fx-selection-bar-non-focused definition in modena.css (default JavaFX style sheet). Both of them are in a section called Theming. So they are meant to be part of a changeable "global" theme. If you change them for the whole application, it will not only change the way TableView will color the selection, it will change even Menu's, List's etc. So you should be aware of it.

But from your comments above it should be clear, that you try to add the style by calling the method .setStyle() on the TableView instance. If you doing that, changing the color by this boths attributes will result in changing only the color of the TableView selection bar.

An Minimal, Complete, and Verifiable example could look like the following code:

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 Project Structure

The JavaFX Appliction Project in Netbeans should look like this:

Working Application

The working application will look like this:

Setting Style in Scene Builder

In Scene Builder you be able to set the same style to a TableView by open the Inspector, than Properties of the TableView and add the following to the style boxes:

这篇关于JavaFX - 如何更改所选未聚焦行的TableView颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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