Pet 개체 목록을 표시하는 netbeans/javafx 프로그램을 설정하려고 시도하고 모든 행에 onclick 리스너를 제공합니다.Rowfactory를 추가 한 후 Javafx 테이블 뷰가 표시되지 않습니다.
나는 fxml에 다음과 같은 한 :
MyTables.fxml 내가 넷빈즈를 사용하고
<VBox alignment="CENTER" layoutX="11.0" layoutY="12.0" prefWidth="345.0">
<children>
<TableView id="Table1" fx:id="Table1" styleClass="mytable"/>
<TableView id="Table2" fx:id="Table2" styleClass="mytable"/>
<TableView id="Table3" fx:id="Table3" styleClass="mytable"/>
<TableView id="Table4" fx:id="Table4" styleClass="mytable"/>
</children>
</VBox>
를, 그래서 FXML을로드 뷰의 topComponent을 설정하는 차례 컨트롤러 클래스를로드합니다.
컨트롤러 클래스에 다음 코드를 추가하여 테이블에 내용을 추가하십시오. 각 테이블에 포함 된 Pet 객체의 ID, Name 및 Owner에 해당하는 3 개의 행을 가져야합니다. 여기에 내 문제입니다
Pet.java
public final class Pet {
public final StringProperty id = new SimpleStringProperty(this, "id");
public final StringProperty name = new SimpleStringProperty(this, "name");
public final StringProperty owner = new SimpleStringProperty(this, "owner");
public Pet() {}
public Pet(String id, String name, String owner) {
this.id.set(id);
this.name.set(name);
this.owner.set(owner);
}
public String getId() {
return id.get();
}
public void setId(String id) {
this.id.set(id);
}
public StringProperty idProperty() {
return id;
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public StringProperty nameProperty() {
return name;
}
public String getOwner() {
return owner.get();
}
public void setOwner(String owner) {
this.owner.set(owner);
}
public StringProperty ownerProperty() {
return owner;
}
}
:
MyTablesController.java
public class MyTablesController implements Initializable {
@FXML
private TableView<Pet> Table1;
@FXML
private TableView<Pet> Table2;
@FXML
private TableView<Pet> Table3;
@FXML
private TableView<Pet> Table4;
private TableView<Pet>[] allTables;
public MyTablesController() {
allTables = new TableView[4];
allTables[0] = Table1;
allTables[1] = Table2;
allTables[2] = Table3;
allTables[3] = Table4;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
for(TableView<Pet> table : allTables) {
table.getColumns().add(createCol("ID", Pet::idProperty, 100));
table.getColumns().add(createCol("Name", Pet::nameProperty, 140));
table.getColumns().add(createCol("Owner", Pet::ownerProperty, 100));
table.getItems().addAll(
new Pet("1234", "spot", "joe"),
new Pet("5432", "snowball", "bill"),
new Pet("7612", "snoopy", "frank")
);
}
setupTableRowDragEvents();
}
private TableColumn<Pet, String> createCol(String title,
Function<Pet, ObservableValue<String>> mapper, double size) {
TableColumn<Pet, String> col = new TableColumn<>(title);
col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue()));
col.setPrefWidth(size);
return col ;
}
private void setupTableRowDragEvents() {
for (TableView<Pet> table : allTables) {
table.setRowFactory(tv -> {
TableRow<Pet> row = new TableRow<>();
row.setOnMouseClicked(event -> {
System.out.println("I CLICKED ON " + row.getItem().getName()");
});
return row;
});
}
}
}
마지막으로, 여기에 애완 동물 개체입니다.
MyTablesController.initialize를 비워두면 4 개의 그래프가 잘 표시됩니다. 그것은 나머지 코드 (setCols 호출과 setupTableRowDragEvents() 호출)를 추가 할 때만입니다 ...
initialize()에있는 것 중 하나 또는 모두가 있으면 그래프가 표시되지 않습니다. 나는 창문이 완전히 비어 있고 그래프가 나타나지 않는 것 외에는 오류 메시지 나 이상한 행동을 얻지 못한다.
나는 내 코드가 sort tableview with drag and drop (rows)에서 나왔습니다. 나는 이것과 비교할 때 내가 어디까지 망쳐 놓고 있는지 알 수 없다. 그 대답은 틀렸고 누구도 그것을 확인하는 것을 귀찮게 했습니까?
내가 뭘 잘못하고 있니?
. 배열을 채울 때 테이블 뷰 참조는 모두 null이므로 배열을 null 참조로 채 웁니다. 그런 다음 배열을 반복 할 때'table.getColumns()'를 처음 호출 할 때 널 포인터 예외가 발생합니다. 코드를 컨트롤러 생성자에서'initialize()'의 시작 부분으로 옮깁니다. –
방금 베이컨을 구해 줬는데, 어제 모두이 물건을 훔쳐 보면서 쓸모가 없었습니다. 나는 틀린 장소를보고있었습니다. 내 다음 과제는 예외가 출력 콘솔에 표시되지 않는 이유를 파악하는 것입니다.하지만 다른 문제입니다. – MattT
FXML을로드하는 코드를 게시해야합니다. 아마 당신은 예외를 괴롭히지 않을 것입니까? 어쨌든, 이제 마침내 내 컴퓨터로 돌아와 공항을 돌아 다니지 않아서 대답으로 대답을 올렸습니다. –