作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我做了这个改变:
Change TableColumn background in TableView, whilst retaining alternating row colour?
未选中看起来不错:
但是当您选择一个单元格时:
它看起来像这样,但我希望它在被选中/聚焦时正常运行。
我很确定我需要使用样式类,但是,我不知道您需要什么属性来保留具有不同颜色背景的 TableCell
的所有其他功能。另外,我是在单元格级别还是在列级别应用样式类?
更新
我的 CSS 文件:自定义.css
.customhighlight .table-cell {
-fx-background-color: rgba(0, 128, 0, 0.3);
}
.customhighlight .table-cell:selected {
-fx-background-color: inherit;
}
如何将其应用于一列?
我试过了
table.getStyleClass().add("customhighlight");
然而,它改变了整个表格。
我试过了
tableCol.getStyleClass().add("customhighlight");
它什么也没做。
我也在细胞层面上试过了...
最佳答案
如果我没理解错的话,你想要:
modena.css
选中的外观。
您应该将样式类添加到相应的单元格中,然后您可以在 CSS 文件中使用这些样式类。这是一个小例子:
主.java
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.Pair;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
var table = System.getProperties().stringPropertyNames().stream()
.map(name -> new Pair<>(name, System.getProperty(name)))
.collect(collectingAndThen(toCollection(FXCollections::observableArrayList), TableView::new));
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
table.getSelectionModel().setCellSelectionEnabled(true); // Not sure if you're using cell or row selection
var keyCol = new TableColumn<Pair<String, String>, String>("Key");
keyCol.setCellValueFactory(new PropertyValueFactory<>("key"));
table.getColumns().add(keyCol);
var valCol = new TableColumn<Pair<String, String>, String>("Value");
valCol.setCellValueFactory(new PropertyValueFactory<>("value"));
valCol.setCellFactory(tc -> new TableCell<>() {
{ getStyleClass().add("highlighted-table-cell"); }
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item);
}
}
});
table.getColumns().add(valCol);
var scene = new Scene(table, 600, 400);
scene.getStylesheets().add("Main.css");
primaryStage.setScene(scene);
primaryStage.show();
}
}
主.css
.highlighted-table-cell {
-fx-background-color: rgba(0, 128, 0, 0.3);
}
/* Needed by cell selection mode */
.highlighted-table-cell:selected {
-fx-background-color: inherit;
}
/* Needed by row selection mode */
.table-row-cell:selected > .highlighted-table-cell {
-fx-background-color: inherit;
}
关于java - 更改 TableCell 中的背景颜色会删除选择颜色,使其难以阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55581362/
我想模拟这个函数: function getMetaData(key) { var deferred = $q.defer(); var s3 = vm.ini
我是一名优秀的程序员,十分优秀!