gpt4 book ai didi

JavaFX TableView - 同一行中新单元格中的新值

转载 作者:行者123 更新时间:2023-11-30 07:20:11 27 4
gpt4 key购买 nike

我遇到以下问题:

我将 TableViews 单元格样式设置为矩形。现在我想添加带有 X 和 Y 坐标的对象(obj.getAblaufX()obj.getAblaufY())。如何在 JavaFX TableView 中正确添加它们?

我当前的代码:

ObservableList<Event> eventList = loadEvents();
TableView<Event> tableView = new TableView<Event>();
tableView.getSelectionModel().setCellSelectionEnabled(true);
tableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
tableView.setItems(eventList);

for(int i = 0; i < 10; i++) {
TableColumn<Event, String> tmpCol = new TableColumn<>();
tmpCol.setId(Integer.toString(i));
tmpCol.setMaxWidth(40);
tmpCol.setCellValueFactory(cellData -> new ObservableValue<String>() {
@Override
public void addListener(InvalidationListener listener) {
// TODO Auto-generated method stub
}
@Override
public void removeListener(InvalidationListener listener) {
// TODO Auto-generated method stub
}
@Override
public void addListener(ChangeListener<? super String> listener) {
// TODO Auto-generated method stub
}
@Override
public void removeListener(ChangeListener<? super String> listener) {
// TODO Auto-generated method stub
}
@Override
public String getValue() {
if(Integer.parseInt(tmpCol.getId()) == cellData.getValue().getAblaufX()) {
return cellData.getValue().getAbteilung();
} else {
return "";
}
}
});

tableView.getColumns().add(tmpCol);
}

结果:

enter image description here

但是:

对象 SP1 的 X = 0 且 Y = 0

对象 SP2 的 X = 1 且 Y = 0

那么,如何解决这个问题呢?

最佳答案

通过使用 Event作为项目类型,您可以防止 TableView显示多个Event每行。使用Integer作为项目类型,并将值存储在 ObservableMap<TablePos, Event> 中哪里TablePos是一个包含 x 的类和y坐标:

public final class TablePos {

private final int x;
private final int y;

public TablePos(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public int hashCode() {
int hash = 7;
hash = 17 * hash + this.x;
hash = 17 * hash + this.y;
return hash;
}

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof TablePos)) {
return false;
}
final TablePos other = (TablePos) obj;
return (this.x == other.x && this.y == other.y);
}

}
public class Event {

private final StringProperty name;
private final IntegerProperty x;
private final IntegerProperty y;

public Event(String name, int x, int y) {
// set beans for properties here to make this instance accessible to listeners
this.y = new SimpleIntegerProperty(this, "y", y);
this.x = new SimpleIntegerProperty(this, "x", x);
this.name = new SimpleStringProperty(this, "name", name);
}

public final String getName() {
return this.name.get();
}

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

public final StringProperty nameProperty() {
return this.name;
}

public final int getX() {
return this.x.get();
}

public final void setX(int value) {
this.x.set(value);
}

public final IntegerProperty xProperty() {
return this.x;
}

public final int getY() {
return this.y.get();
}

public final void setY(int value) {
this.y.set(value);
}

public final IntegerProperty yProperty() {
return this.y;
}

}
private static void put(Map<TablePos, Event> map, Event evt) {
map.put(new TablePos(evt.getX(), evt.getY()), evt);
}

@Override
public void start(Stage primaryStage) {
ObservableMap<TablePos, Event> contents = FXCollections.observableHashMap();

TableView<Integer> tableView = new TableView<>(FXCollections.observableArrayList());

TableColumn columnGroup = new TableColumn("Heutige Termine");

ChangeListener<Number> xChangeListener = (observable, oldValue, newValue) -> {
Event evt = (Event) ((Property) observable).getBean();
TablePos oldPos = new TablePos(oldValue.intValue(), evt.getY());
TablePos newPos = new TablePos(newValue.intValue(), evt.getY());
contents.remove(oldPos);
contents.put(newPos, evt);
};

ChangeListener<Number> yChangeListener = (observable, oldValue, newValue) -> {
Event evt = (Event) ((Property) observable).getBean();
TablePos oldPos = new TablePos(evt.getX(), oldValue.intValue());
TablePos newPos = new TablePos(evt.getX(), newValue.intValue());
contents.remove(oldPos);
contents.put(newPos, evt);
};

contents.addListener((MapChangeListener.Change<? extends TablePos, ? extends Event> change) -> {
if (change.wasRemoved()) {
Event evt = change.getValueRemoved();
evt.xProperty().removeListener(xChangeListener);
evt.yProperty().removeListener(yChangeListener);
}
if (change.wasAdded()) {
Event evt = change.getValueAdded();
evt.xProperty().addListener(xChangeListener);
evt.yProperty().addListener(yChangeListener);
}

});

// items denote the y coordinate
for (int i = 0; i < 10; i++) {
tableView.getItems().add(i);
}

// one column per x coordiante
for (int i = 0; i < 10; i++) {
final int index = i;
TableColumn<Integer, String> column = new TableColumn<>();

// take value from Map using x (index) and y (item value)
column.setCellValueFactory(cd
-> Bindings.selectString(
Bindings.valueAt(contents, new TablePos(index, cd.getValue())), "name"));

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

tableView.getColumns().add(columnGroup);

Event opTarget = new Event("Something", 2, 9);

put(contents, new Event("SP1", 0, 0));
put(contents, new Event("SP2", 1, 0));
put(contents, opTarget);

Button move = new Button("move");
move.setOnAction(evt -> opTarget.setX(9 - opTarget.getX()));
Button rename = new Button("rename");
rename.setOnAction(evt -> opTarget.setName(opTarget.getName().equals("42") ? "Answer" : "42"));

Scene scene = new Scene(new VBox(10, tableView, move, rename));

primaryStage.setScene(scene);

primaryStage.show();
}

请注意,这需要您知道初始化时的行/列数(为了简单起见,我都使用 10)。如果不是这种情况,您可以将监听器添加到 ObservableMap动态添加/删除行/列。

关于JavaFX TableView - 同一行中新单元格中的新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718226/

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