我想创建酒店软件系统。
我创建了FXML页面。它有TableView
名为tablesettings
。
首先在控制器类的initialize
方法上将列添加到tablesettings
,然后添加行。
现在我需要通过列和行的索引来更改单个单元格的背景颜色。 我想要一个这样的观点:
我的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数据库架构预约表
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');
如果你帮助我会很高兴............
答案 0 :(得分:0)
首先,您的列的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);
)。