[英]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.