Javafx id setGpField(GoldplusField gpField){this.gpField.set(gpField);} public ObjectProperty gpFieldProperty(){返回this.gpField;} 公共映射项(
id setGpField(GoldplusField gpField){this.gpField.set(gpField);} public ObjectProperty gpFieldProperty(){返回this.gpField;} 公共映射项(字符串columnName){this.excelColumnName.set(columnName);} 公共映射项(GoldplusField gpField){this.gpField.set(gpField);} 公共映射项(字符串列名称,GoldplusField gpField){ this.excelColumnName.set(columnName); this.gpField.set(gpField); } } 公共级GoldplusField{ 私有StringProperty表=新的SimpleStringProperty(本“表”); 私有StringProperty dbName=新的SimpleStringProperty(此“dbName”); private StringProperty gpName=新的SimpleStringProperty(即“gpName”); 公共字符串getDbName(){返回dbName.get();} 公共字符串getGpName(){返回gpName.get();} 公共字符串getTable(){return table.get();} public void setDbName(字符串dbName){this.dbName.set(dbName);} public void setGpName(字符串gpName){this.gpName.set(gpName);} public void setTable(字符串表){this.table.set(table);} public StringProperty tableProperty(){返回this.table;} public StringProperty gpNameProperty(){返回this.gpName;} public StringProperty dbNameProperty(){返回this.dbName;} 公共GoldplusField(字符串表、字符串dbName、字符串gpName){ this.dbName.set(dbName); this.gpName.set(gpName); 本.表.集(表); } } }Javafx id setGpField(GoldplusField gpField){this.gpField.set(gpField);} public ObjectProperty gpFieldProperty(){返回this.gpField;} 公共映射项(,javafx,event-handling,javafx-8,observable,tablecolumn,Javafx,Event Handling,Javafx 8,Observable,Tablecolumn,id setGpField(GoldplusField gpField){this.gpField.set(gpField);} public ObjectProperty gpFieldProperty(){返回this.gpField;} 公共映射项(字符串columnName){this.excelColumnName.set(columnName);} 公共映射项(GoldplusField gpField){this.gpField.set(gpField);} 公共映射项(字符串列名
SSCCE,SSCCE,SSCCE。。。。(和往常一样)我会在几个小时后上传一个。谢谢你的评论。@kleopatra,我把它改成了SSCCE.:-)不编译(在一些setter中有额外的开始括号)-请精确!除了技术上的问题:你不太明白你在追求什么-修改组合的项目,然后?好吧,明白你的意思了。所发生的情况是,修改列表会更改组合的选择状态,然后再次触发编辑(正如您已经注意到的:-)一个快速修复方法是将activeList.remove包装到platform.runlater中。不确定那是否完全安全,想一想,想一想,想一想,想一想。。。。(和往常一样)我会在几个小时后上传一个。谢谢你的评论。@kleopatra,我把它改成了SSCCE.:-)不编译(在一些setter中有额外的开始括号)-请精确!除了技术上的问题:你不太明白你在追求什么-修改组合的项目,然后?好吧,明白你的意思了。所发生的情况是,修改列表会更改组合的选择状态,然后再次触发编辑(正如您已经注意到的:-)一个快速修复方法是将activeList.remove包装到platform.runlater中。但不确定这是否完全安全
package tableviewexample;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.collections.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class TableViewExample extends Application {
@Override
public void start(Stage primaryStage) {
TableView<MappingItem> table = new TableView<>();
// FIRST COLUMN
TableColumn<MappingItem, String> colA = new TableColumn<>("Excel Column");
colA.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<MappingItem, String>, ObservableValue<String>> () {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<MappingItem, String> param) {
return new ReadOnlyObjectWrapper(param.getValue().getExcelColumnName());
}
});
//SECOND COLUMN
TableColumn<MappingItem, GoldplusField> colB = new TableColumn<>("Database Field Column");
colB.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<MappingItem, GoldplusField>, ObservableValue<GoldplusField>> () {
@Override
public ObservableValue<GoldplusField> call(TableColumn.CellDataFeatures<MappingItem, GoldplusField> param) {
return new ReadOnlyObjectWrapper(param.getValue().getGpField());
}
});
GoldplusField gp1 = new GoldplusField("T1", "fName", "First Name");
GoldplusField gp2 = new GoldplusField("T1", "phn", "Phone");
GoldplusField gp3 = new GoldplusField("T2", "lName", "Last Name");
GoldplusField gp4 = new GoldplusField("T2", "adrs", "Address");
ObservableList<GoldplusField> deactiveFieldsList = FXCollections.observableArrayList();
ObservableList<GoldplusField> activeFieldsList = FXCollections.observableArrayList(gp1, gp2, gp3, gp4);
colB.setCellFactory(ComboBoxTableCell.forTableColumn(new FieldToStringConvertor(), activeFieldsList));
colB.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<MappingItem, GoldplusField>>() {
@Override
public void handle(TableColumn.CellEditEvent<MappingItem, GoldplusField> t) {
if (t.getNewValue() != null) {
deactiveFieldsList.add(t.getNewValue());
((MappingItem) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setGpField(t.getNewValue());
// ******************************************************************************************** //
// This creates a new instance of the EventHandler in which I get the "next" item on the List.
// ******************************************************************************************** //
activeFieldsList.remove(t.getNewValue());
}
}
}
);
//THIRD COLUMN
TableColumn<MappingItem, String> colC = new TableColumn<>("Test Column");
PropertyValueFactory<MappingItem, String> nameFac = new PropertyValueFactory<>("name");
colC.setCellValueFactory(nameFac);
colC.setCellFactory(TextFieldTableCell.forTableColumn());
table.setEditable(true);
table.getColumns().addAll(colA, colB, colC);
GoldplusField gp5 = new GoldplusField("T1", "other", "Other");
MappingItem mi1 = new MappingItem("name", gp5);
mi1.excelColumnName.set("name1");
MappingItem mi2 = new MappingItem("phone", gp5);
mi2.excelColumnName.set("nam2");
ObservableList<MappingItem> miList = FXCollections.observableArrayList(mi1, mi2);
table.setItems(miList);
StackPane root = new StackPane();
root.getChildren().add(table);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
class FieldToStringConvertor extends StringConverter<GoldplusField> {
@Override
public String toString(GoldplusField object) {
if (object != null)
return object.getGpName();
else
return "";
}
@Override
public GoldplusField fromString(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
public class MappingItem {
private StringProperty excelColumnName = new SimpleStringProperty(this, "excelColumnName");
private ObjectProperty<GoldplusField> gpField = new SimpleObjectProperty<GoldplusField>(this, "gpField");
public String getExcelColumnName() { return excelColumnName.get(); }
public void setExcelColumnName(String excelColumnName) { this.excelColumnName.set(excelColumnName); }
public StringProperty excelColumnNameProperty() { return excelColumnName; }
public GoldplusField getGpField() { return gpField.get(); }
public void setGpField(GoldplusField gpField) { this.gpField.set(gpField); }
public ObjectProperty gpFieldProperty() { return this.gpField; }
public MappingItem(String columnName) { this.excelColumnName.set(columnName); }
public MappingItem(GoldplusField gpField) { this.gpField.set(gpField); }
public MappingItem(String columnName, GoldplusField gpField) {
this.excelColumnName.set(columnName);
this.gpField.set(gpField);
}
}
public class GoldplusField {
private StringProperty table = new SimpleStringProperty(this, "table");
private StringProperty dbName = new SimpleStringProperty(this, "dbName");
private StringProperty gpName = new SimpleStringProperty(this, "gpName");
public String getDbName() { return dbName.get(); }
public String getGpName() { return gpName.get(); }
public String getTable() { return table.get(); }
public void setDbName(String dbName) { this.dbName.set(dbName); }
public void setGpName(String gpName) { this.gpName.set(gpName); }
public void setTable(String table) { this.table.set(table); }
public StringProperty tableProperty() { return this.table; }
public StringProperty gpNameProperty() { return this.gpName; }
public StringProperty dbNameProperty() { return this.dbName; }
public GoldplusField(String table, String dbName, String gpName) {
this.dbName.set(dbName);
this.gpName.set(gpName);
this.table.set(table);
}
}
}