I added default TEST1 and TEST2 as Name Col in TableView but didn't display when I run it.
Here are two classes and one fxml I used for, can anyone give me some advices regarding this issue ?
only one package call : controllerJBoxFXTest
1st Class MainView.class
package controllerJBoxFXTest;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class MainView extends Application {
public static void main(String[] args){
Application.launch(MainView.class, (java.lang.String[]) null);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
AnchorPane Page = (AnchorPane) FXMLLoader.load(MainView.class.getResource("MainController.fxml"));
Scene scene = new Scene(Page);
primaryStage.setScene(scene);
primaryStage.setTitle("Test Main");
primaryStage.show();
} catch (Exception e){
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, e);
}
}
}
2nd MainController.java, you can see I added TEST1 and TEST2
package controllerJBoxFXTest;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.beans.property.SimpleStringProperty;
public class MainController implements Initializable{
@FXML
TableView<Table> tableID;
@FXML
TableColumn<Table, String> iName;
public class Table {
private final SimpleStringProperty rName;
public Table(String sName) {
this.rName = new SimpleStringProperty(sName);
}
public String getName(){
return rName.get();
}
public void setName(String vName) {
this.rName.set(vName);
}
}
final ObservableList<Table> data = FXCollections.observableArrayList(
new Table("TEST1"),
new Table("TEST2")
);
@Override
public void initialize(URL location, ResourceBundle resources) {
iName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));
tableID.setItems(data);
}
}
3rd This is MainController.fxml which was generated by scene generator
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllerJBoxFXTest.MainController">
<children>
<SplitPane dividerPositions="0.8729096989966555" layoutX="197.0" layoutY="61.0" orientation="VERTICAL" prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView fx:id="tableID" layoutX="224.0" layoutY="107.0" prefHeight="519.0" prefWidth="798.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="iName" prefWidth="769.0" text="Name" />
</columns>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
Can anyone sure why my TableView is empty when I run the MainView.java ?
Thanks,
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…