- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
过去几个小时我一直在网上搜索并尝试实现此功能但没有成功。
我有一个包含多个列的 TableView,其中一个是包含时间的字符串字段。我想突出显示 timeCol
值晚于当前时间的所有行。
这必须每 5 分钟检查一次,并相应地突出显示表格。我假设这需要在后台线程上运行,但我对并发没有经验。
TableView 内容
@FXML
private TableView<BookingImpl> bookingTableView;
@FXML
private TableColumn<BookingImpl, String> timeCol;
@FXML
private TableColumn<BookingImpl, String> nameCol;
@FXML
private TableColumn<BookingImpl, String> pickUpCol;
@FXML
private TableColumn<BookingImpl, String> dropOffCol;
@FXML
private TableColumn<BookingImpl, String> commentCol;
@FXML
private TableColumn<BookingImpl, String> priceCol;
我看过这个帖子JavaFX tableview colors这看起来像我想要实现的,高亮颜色需要是淡紫色。
任何指导将不胜感激。
更新 - James_D 解决方案
我已经对此进行了调整以适合我的应用程序,但它只会突出显示输入的延迟预订,即如果我从现在开始输入 1 分钟的预订并等待它不会突出显示它。我已禁用列的 FXML 实现,但更愿意使用它。
我正在从我的主 Controller 类中引用这个新类,如下所示;
TableRowController rowController = new TableRowController();
rowController.start(bookingTableView);
表格行 Controller
包 Controller ;
import Model.BookingImpl;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.css.PseudoClass;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import java.time.LocalTime;
import java.util.function.Function;
public class TableRowController{
private final PseudoClass future = PseudoClass.getPseudoClass("future");
public void start(TableView table){
ObjectProperty<LocalTime> now = new SimpleObjectProperty<>(LocalTime.now());
table.setRowFactory(tv -> {
TableRow<BookingImpl> row = new TableRow<>();
ChangeListener<LocalTime> listener = (obs, oldTime, newTime) -> updateRow(row, now.get());
now.addListener(listener);
row.itemProperty().addListener((obs, oldItem, newItem) -> {
if (oldItem != null) {
oldItem.getTimeProperty().removeListener(listener);
}
if (newItem != null) {
newItem.getTimeProperty().addListener(listener);
}
updateRow(row, now.get());
});
return row ;
});
configureTable(table);
}
public void updateRow(TableRow<BookingImpl> row, LocalTime now) {
boolean isFuture = false ;
if (row.getItem() != null) {
isFuture = row.getItem().getTime().isBefore(now);
}
row.pseudoClassStateChanged(future, isFuture);
}
private void configureTable(TableView<BookingImpl> table) {
table.getColumns().add(column("Time", (Function<BookingImpl, Property<LocalTime>>) (t) -> t.getTimeProperty()));
table.getColumns().add(column("Name", (Function<BookingImpl, Property<String>>) (t) -> new SimpleStringProperty(t.getClientName())));
table.getColumns().add(column("Pickup", (Function<BookingImpl, Property<String>>) (t) -> new SimpleStringProperty(t.getPickUpAddress())));
table.getColumns().add(column("Dropoff", (Function<BookingImpl, Property<String>>) (t) -> new SimpleStringProperty(t.getDropOffAddress())));
table.getColumns().add(column("Comment", (Function<BookingImpl, Property<String>>) (t) -> new SimpleStringProperty(t.getComments())));
table.getColumns().add(column("Price", (Function<BookingImpl, Property<String>>) (t) -> new SimpleStringProperty(t.getFormattedPrice())));
table.setItems(ObservableLists.bookingsList);
}
private <S,T> TableColumn<S,T> column(String title, Function<S, Property<T>> property) {
TableColumn<S,T> column = new TableColumn<>(title);
column.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
return column ;
}
}
BookingImpl 类
public class BookingImpl implements Booking {
public static int bookingNumberCounter = 1001;
private final int bookingNumber;
private Account account;
private String vehicleType;
private String noPassengers;
private LocalDate date;
private ObjectProperty<LocalTime> time = new SimpleObjectProperty<>();
private String pickUpAddress;
private String dropOffAddress;
private String clientName;
private String clientTel;
private String clientEmail;
private String comments;
private double price;
private boolean completed = false;
private Driver driver;
public BookingImpl(Account account){
this.bookingNumber = bookingNumberCounter;
bookingNumberCounter++;
Archive.allBookings.add(this);
Archive.incompleteBookings.add(this);
ObservableLists.bookingsList.clear();
ObservableLists.bookingsList.addAll(Archive.incompleteBookings);
account.newBooking(this);
}
@Override
public void deleteBooking() {
account.deleteBooking(this);
Archive.allBookings.remove(this);
}
public int getBookingNumber() {
return bookingNumber;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getVehicleType() {
return vehicleType;
}
public void setVehicleType(String vehicleType) {
this.vehicleType = vehicleType;
}
public String getNoPassengers() {
return noPassengers;
}
public void setNoPassengers(String noPassengers) {
this.noPassengers = noPassengers;
}
public String getDate() {
return new SimpleDateFormat("dd/MM/yyyy").format(date);
}
public void setDate(LocalDate date) {
this.date = date;
}
public ObjectProperty<LocalTime> getTimeProperty() {
return time;
}
public LocalTime getTime() {
return this.getTimeProperty().get();
}
public void setTime(LocalTime time) {this.time.set(time);}
public String getPickUpAddress() {
return pickUpAddress;
}
public void setPickUpAddress(String pickUpAddress) {
this.pickUpAddress = pickUpAddress;
}
public String getDropOffAddress() {
return dropOffAddress;
}
public void setDropOffAddress(String dropOffAddress) {
this.dropOffAddress = dropOffAddress;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getClientTel() {
return clientTel;
}
public void setClientTel(String clientTel) {
this.clientTel = clientTel;
}
public String getClientEmail() {
return clientEmail;
}
public void setClientEmail(String clientEmail) {
this.clientEmail = clientEmail;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public double getPrice() {
return price;
}
public String getFormattedPrice() {
DecimalFormat df = new DecimalFormat("#.00");
return "£"+df.format(this.price);
}
public void setPrice(double price) {
this.price = price;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
public Driver getDriver() {
return driver;
}
public void setDriver(Driver driver) {
this.driver = driver;
}
}
最佳答案
我会按如下方式处理:
使用 rowFactory
在 table 上。您创建的行将观察当前元素的时间属性,并在更改时更新 CSS PseudoClass。您还需要该行来观察当前时间:为此创建一个 ObjectProperty<LocalTime>
并从 AnimationTimer
更新它.
使用 CSS PseudoClass 的优点是您可以在外部 CSS 文件中定义样式。
这是一个例子:
import java.time.LocalTime;
import java.util.Random;
import java.util.function.Function;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class HighlightFutureItems extends Application {
private final PseudoClass future = PseudoClass.getPseudoClass("future");
@Override
public void start(Stage primaryStage) {
TableView<Item> table = new TableView<>();
ObjectProperty<LocalTime> now = new SimpleObjectProperty<>(LocalTime.now());
startClock(now);
table.setRowFactory(tv -> {
TableRow<Item> row = new TableRow<>();
ChangeListener<LocalTime> listener = (obs, oldTime, newTime) -> updateRow(row, now.get());
now.addListener(listener);
row.itemProperty().addListener((obs, oldItem, newItem) -> {
if (oldItem != null) {
oldItem.timeProperty().removeListener(listener);
}
if (newItem != null) {
newItem.timeProperty().addListener(listener);
}
updateRow(row, now.get());
});
return row ;
});
configureTable(table);
BorderPane root = new BorderPane(table);
Label clock = new Label();
root.setTop(clock);
clock.textProperty().bind(now.asString());
Scene scene = new Scene(root, 600, 600);
scene.getStylesheets().add("future-highlighting-table.css");
primaryStage.setScene(scene);
primaryStage.show();
}
private void updateRow(TableRow<Item> row, LocalTime now) {
boolean isFuture = false ;
if (row.getItem() != null) {
isFuture = row.getItem().getTime().isAfter(now);
}
row.pseudoClassStateChanged(future, isFuture);
}
private void startClock(ObjectProperty<LocalTime> clock) {
new AnimationTimer() {
@Override
public void handle(long timestamp) {
clock.set(LocalTime.now());
}
}.start();
}
private void configureTable(TableView<Item> table) {
table.getColumns().add(column("Item", Item::nameProperty));
table.getColumns().add(column("Time", Item::timeProperty));
Random rng = new Random();
LocalTime now = LocalTime.now();
for (int i = 1 ; i <= 50 ; i++) {
Item item = new Item("Item "+i, now.plusSeconds(rng.nextInt(120) - 60));
table.getItems().add(item);
}
}
private <S,T> TableColumn<S,T> column(String title, Function<S, Property<T>> property) {
TableColumn<S,T> column = new TableColumn<>(title);
column.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
return column ;
}
public static class Item {
private final StringProperty name = new SimpleStringProperty();
private final ObjectProperty<LocalTime> time = new SimpleObjectProperty<>();
public Item(String name, LocalTime time) {
setName(name);
setTime(time);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
public final ObjectProperty<LocalTime> timeProperty() {
return this.time;
}
public final LocalTime getTime() {
return this.timeProperty().get();
}
public final void setTime(final LocalTime time) {
this.timeProperty().set(time);
}
}
public static void main(String[] args) {
launch(args);
}
}
CSS 文件(future-highlighting-table.css"
)可以很简单
.table-row-cell:future {
-fx-background: palevioletred;
}
关于java - 当时间单元格超过当前时间时突出显示 JavaFX 中的 TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36990605/
我试图通过这段代码读取未知数量的整数: while (1) { int c = getchar (); if (c == EOF) break;
我正试图找到一个类似于谷歌分析日期选择器的日期选择器: 知道 jQuery 是否提供了类似的东西吗? 最佳答案 这个 Twitter Bootstrap 风格的日期范围选择器非常接近。 https:/
我正在使用 javascript。如何获取当前 URL 的路径并将其分配给我的代码?这是我的代码: $(document).ready(function() { $(".share").hides
如何获得今天的Julian day number (JDN)相等的?或任何日期? 我看了又看,但只发现了一些产生“year-dayOfYear”的函数,而不是:2457854。 最佳答案 在 bash
我有相当简单的 UDP 服务器写在 c 上。 有时我需要知道在套接字中排队的所有 udp 数据包(字节)的当前长度。 据我了解,getsockopt 没有得到这样的信息。 欢迎使用 Linux 和 F
我一直在寻找几个小时来找到一个可以在图像中添加诸如“填充:5px”之类的东西的插件。每个人都通过纯 html 做到这一点吗?我们的客户需要一种方法来简单地使用按钮或右键单击上下文菜单来添加它。有什么建
是否有可能获得当前正在执行的 TCL 脚本的完整路径? 在 PHP 中,它将是:__FILE__ 最佳答案 根据“当前正在执行的 TCL 脚本”的含义,您实际上可能会寻找 info script ,甚
我最近从直接使用 ISession 转向了包装的 ISession,即工作单元类型模式。 我曾经使用 SQL Lite(内存中)对此进行测试。我有一个简单的帮助器类,它配置我的 SessionFact
我按照步骤操作 here在 WebStorm 中配置代码完成和其他内容,但我仍然收到以下语法错误。 我该如何解决这个问题? 最佳答案 通过相应地将“JavaScript 语言版本”(Settings/
我可以为我团队的 TFS 当前 Sprint 任务板添加书签吗?我们有两周的冲刺,因此 URL 每两周更改一次。 默认 URL 的形式为: http://[Server]/tfs/[Project]/
是否有 Subversion 命令可以显示当前版本号? 在svn checkout之后,我想启动一个脚本并需要变量中的修订号。如果有像 svn info get_revision_number 这样的
我正在编写表单的一个组件 首次安装组件时,sources={{}} ,一本空字典。由于该组件包装了现有的 Javascript 库,因此我正在实现一个自定义比较函数。为了让这个 diffing 函数
无论系统时间设置为多少以及机器所在的时区,我都需要正确的 UTC 时间。 (即使我必须打电话到互联网才能同步......) 是否有一些库或其他方法可以优雅地做到这一点? 最佳答案 如果您想获得准确可靠
我一边编码,一边拿出一些我和 friend 建立的旧网站来重新开始工作。我已经有一段时间没有做过任何 AJAX 了,当我试图找出我的代码失败的地方时,我发现没有显示很多资源。我猜这是因为我使用的是旧方
由于对性能的巨大影响,我从不怀疑我现在的桌面CPU是否有分支预测。当然可以。但各种 ARM 产品又如何呢? iPhone或Android手机有分支预测吗?较旧的任天堂 DS?基于 PowerPC 的
我有一个具有以下有效负载的 JWT: { "id": "394a71988caa6cc30601e43f5b6569d52cd7f6df", "jti": "394a71988caa6cc30
从其他一些帖子中,我能够通过以下方式获取当前 URI: 但是以下方法不起作用: 我很好奇为什么上面的方法不起作用,以及如何将当前 URI 分配给字符串。 最佳答案 每the javadocs ,g
我在表格 View 中有几个单元格。现在在任何给定的时间点,我想计算 View 中单元格的当前高度,即如果它是 View 的 3/4,它应该返回 (cellheight)*3/4 高度。 我通过以下方
这是网站的身份验证脚本。这安全吗?是最近的节目吗?它已经过时了吗?是否有“更好更安全的方法”我很新,但我没有看到太多地方使用 header 授权。 如有任何帮助,我们将不胜感激!这是我制作的第一个登录
我已经在其他 stackoverflow 线程上检查过这个错误,但在我的代码中没有发现任何错误。也许我累了,但我觉得还好。 网站.urls.py: from django.conf.urls impo
我是一名优秀的程序员,十分优秀!