【发布时间】:2018-10-20 18:10:39
【问题描述】:
所以我试图让一个 TableView 来代表一排座位。因此,一行代表“Reihe”类的对象(德语“row”)。Reihe 有一个 Sitzplatz(“seat”)数组。每个座位都有一个按钮,应该显示在座位单元格中。 所以我对 TableColumns 的 cellFactories 有点困惑。如何告诉列显示来自 row.seat[columnIdx] 的座位按钮? 我不能返回 ObservableValue 对吗?那么我用什么作为 CellFactories 呢?
类“Reihe”(=row):
public class Reihe implements DataObject
{
private Sitzplatz[] seats;
public Reihe(int seats,int saal)
{
this.seats=new Sitzplatz[seats];
for(int i=0; i<this.seats.length; i++)
{
this.seats[i]=new Sitzplatz();
this.seats[i].setSaal_SID(""+saal);
}
}
public Sitzplatz getSeat(int idx)
{
return seats[idx];
}
...
类“Sitzplatz”(“座位”):
public class Sitzplatz implements DataObject
{
private SimpleStringProperty platz, reihe,saal_SID, reservierung_REID;
private SeatButton button;
public Sitzplatz()
{
this.platz=new SimpleStringProperty();
this.saal_SID=new SimpleStringProperty();
this.reihe=new SimpleStringProperty();
this.reservierung_REID=new SimpleStringProperty();
button=new SeatButton();
}
public SeatButton getButton()
{
return button;
}
...
列的初始化:
for(int j=0; j<seatColumns; j++)
{
TableColumn<Reihe,Button> nColumn=new TableColumn<>("Seat"+j);
//final int idx=j;
nColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Reihe, Button>, ObservableValue<Button>>() {
@Override
public ObservableValue<Button> call(TableColumn.CellDataFeatures<Reihe, Button> p) {
// ???
}
});
nColumn.setMinWidth(50);
nColumn.setEditable(true);
//getColumns().add(nColumn);
getColumns().add(nColumn);
}
我发现了一些关于使用 Button extends TableCell 的信息,但我还是无法真正弄清楚它应该如何工作:
public class SeatButton extends TableCell<Reihe, Button>
{
Button cellButton;
//private Sitzplatz seat;
public SeatButton()
{
//seat=row.getSeat(column);
cellButton=new Button();
cellButton.setMinWidth(30);
cellButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
//....
}
});
}
}
【问题讨论】:
-
这可能超出了您的需要,但它应该让您朝着正确的方向前进。 stackoverflow.com/questions/17067481/…
-
我建议您将自己的代码限制为 1 种语言。您似乎还没有决定要使用德语还是英语。 (我推荐使用英文,因为这样你会与 API 保持一致。)
-
我知道有人会这么说,我自己也知道因为这主要是为了用“德语”SQL 数据库测试 JavaFX+JDBC,所以语言有点混乱
标签: button javafx tableview tablecolumn