gpt4 book ai didi

mysql - Javafx 更改 tableview 单元格背景颜色

转载 作者:行者123 更新时间:2023-11-30 22:15:33 25 4
gpt4 key购买 nike

我想创建酒店软件系统。

应用截图

this is screenshot of my app

我已经创建了 FXML 页面。它有一个名为 tablesettingsTableView。在 Controller 类的 initialize 方法中,我首先将列添加到 tablesettings,然后添加行。

现在我需要通过列和行的索引更改单个单元格的背景颜色。我想要这样的 View :

desired result

我的 Java 代码:

@FXML
private TableView tablesettings;

public void getDatasReserv() {
ArrayList<LocalDate> dates = new ArrayList<LocalDate>();
ArrayList<String> dts = new ArrayList<String>();
ArrayList roomss = new ArrayList();
ArrayList roomtypes = new ArrayList();
LocalDate localDate1 = girissene.getValue();
LocalDate localDate2 = cykyssene.getValue();
Date DPCurrentDate1 = Date.valueOf(girissene.getValue());
Date DPCurrentDate2 = Date.valueOf(cykyssene.getValue());
dts.add("Otag");
dts.add("Görnüşi");
dts.add(localDate1.toString());

while (!localDate1.equals(localDate2)) {
dates.add(localDate1);
localDate1 = localDate1.plusDays(1);
dts.add(localDate1.toString());
}

// add columns
for (int i = 0; i < dts.size(); i++) {
final int finalIdx = i;
TableColumn<ObservableList<String>, String> column = new TableColumn<>(dts.get(i) );
column.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue().get(finalIdx)));
column.cellValueFactoryProperty();
if(i==0)
column.maxWidthProperty().set(25);
if(i==1)
column.minWidthProperty().set(150);
else
column.minWidthProperty().set(80);
tablesettings.getColumns().add(column);
}

try {
connected();
String sql = "SELECT * from roomsview";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
roomss.add(rs.getString(1));
roomtypes.add(rs.getString(2));
}
closed();
} catch (Exception e) {}

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

data = FXCollections.observableArrayList();
item = FXCollections.observableArrayList();
// add data
for(int j=0 ; j<roomss.size(); j++){
//Iterate Row
String strs= (String) roomss.get(j);
int y = Integer.parseInt(strs);
map.put(y,j);
ObservableList<Object> row = FXCollections.observableArrayList();
for(int i=1 ; i<=dts.size(); i++){
//Iterate Column
if(i==1){
String str= (String) roomss.get(j);
row.add(str);
}
else if(i==2){
String str= (String) roomtypes.get(j);
row.add(str);
}
else{row.add("");}
}
data.add(row);
}
tablesettings.setItems(data);
tablesettings.getSelectionModel().setSelectionMode(javafx.scene.control.SelectionMode.MULTIPLE);
tablesettings.getSelectionModel().setCellSelectionEnabled(true);
//*****

ObservableList<Object> row2 = FXCollections.observableArrayList();
try {
connected();
String sql = "SELECT `20`.startdate, `20`.enddate, `20`.number FROM `20` WHERE `20`.startdate BETWEEN '"+new java.sql.Timestamp(DPCurrentDate1.getTime())+"' AND '"+new java.sql.Timestamp(DPCurrentDate2.getTime())+"'"
+ " AND `20`.enddate BETWEEN '"+new java.sql.Timestamp(DPCurrentDate1.getTime())+"' AND '"+new java.sql.Timestamp(DPCurrentDate2.getTime())+"'";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
// item.addAll((ObservableList[]) tablesettings.getItems().get(map.get(rs.getInt(3))));
row2.add(tablesettings.getItems().get(map.get(rs.getInt(3))));

}
closed();

} catch (Exception e) {}

}

Mysql数据库schema只保留表

CREATE TABLE `reservation` (
`reservid` int(20) NOT NULL,
`roomid` int(20) NOT NULL,
`guestid` int(11) NOT NULL,
`startdate` date NOT NULL,
`enddate` date NOT NULL,
`price` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Дамп данных таблицы `reservation`
--

INSERT INTO `reservation` (`reservid`, `roomid`, `guestid`, `startdate`, `enddate`, `price`) VALUES
(50, 149, 1, '2016-07-16', '2016-07-17', '655'),
(51, 127, 2, '2016-07-15', '2016-07-23', '2400');

如果你能帮上忙,我会很高兴......

最佳答案

首先你的列的cellValueFactory应该是

column.setCellValueFactory(param -> Bindings.valueAt(param.getValue(), finalIdx));

否则列表中的更改不会触发 TableView 的更新。

要为单元格添加颜色,请将行的元素类型更改为某个类,该类包含允许您确定背景和文本的属性。示例:

@Override
public void start(Stage primaryStage) {
TableView<ObservableList<ColoredItem>> table = new TableView<>(FXCollections.observableArrayList(
FXCollections.observableArrayList(new ColoredItem("1"), new ColoredItem("2", Color.GREEN)),
FXCollections.observableArrayList(new ColoredItem("3", Color.RED), new ColoredItem("4"))
));

List<String> dts = Arrays.asList("column 1", "column 2");

for (int i = 0; i < dts.size(); i++) {
final int finalIdx = i;
TableColumn<ObservableList<ColoredItem>, ColoredItem> column = new TableColumn<>(dts.get(i));
column.setCellValueFactory(param -> Bindings.valueAt(param.getValue(), finalIdx));
column.setCellFactory(tv -> new TableCell<ObservableList<ColoredItem>, ColoredItem>() {

@Override
protected void updateItem(ColoredItem item, boolean empty) {
super.updateItem(item, empty);

if (empty || item == null) {
// remove background and text
backgroundProperty().unbind();
textProperty().unbind();
setBackground(Background.EMPTY);
setText(null);
} else {
// bind background and text to the item properties
textProperty().bind(item.valueProperty());
backgroundProperty().bind(Bindings.createObjectBinding(() -> new Background(
new BackgroundFill(item.getBackground(),
CornerRadii.EMPTY,
Insets.EMPTY)),
item.backgroundProperty()));
}
}

});
table.getColumns().add(column);
}

Button btn = new Button("change");
btn.setOnAction(evt -> {
table.getItems().get(0).set(0, new ColoredItem("42", Color.YELLOW));
table.getItems().get(1).get(0).setValue("99");
table.getItems().get(1).get(0).setBackground(Color.DARKMAGENTA);
});
Scene scene = new Scene(new VBox(table, btn));

primaryStage.setScene(scene);
primaryStage.show();
}
public class ColoredItem {

public ColoredItem(String value, Paint backround) {
this.background = new SimpleObjectProperty<>(backround);
this.value = new SimpleStringProperty(value);
}

public ColoredItem(String value) {
this(value, null);
}

private final StringProperty value;
private final ObjectProperty<Paint> background;

public final Paint getBackground() {
return this.background.get();
}

public final void setBackground(Paint value) {
this.background.set(value);
}

public final ObjectProperty<Paint> backgroundProperty() {
return this.background;
}

public final String getValue() {
return this.value.get();
}

public final void setValue(String value) {
this.value.set(value);
}

public final StringProperty valueProperty() {
return this.value;
}

}

此外:如果可以避免,请不要使用原始类型。通过指定类型参数,编译器可以进行一些检查以防止运行时出错。此外,这样你就不需要自己写下类型转换(例如 String str= (String) roomtypes.get(j); 可以写成 String str= roomtypes.get (j);).

关于mysql - Javafx 更改 tableview 单元格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38418089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com