【问题标题】:Populating TableView In JavaFX with TableView created in FXML File使用在 FXML 文件中创建的 TableView 在 JavaFX 中填充 TableView
【发布时间】:2013-06-28 10:57:56
【问题描述】:

我是 JavaFX 和 java 的新手。伙计们,如果缺少什么,请找出这段代码,因为我已经可以使用 println 函数从数据库中打印项目。

public class TableViewController  implements Initializable{

@FXML
private TableView<studentInfo> tblViewer = new TableView<studentInfo>();
@FXML
private TableColumn colID = new TableColumn();
@FXML
private TableColumn colName = new TableColumn();
@FXML
private TableColumn colAddress = new TableColumn();
@FXML
private TableColumn colAge = new TableColumn();
@FXML
private TableColumn colContact = new TableColumn();
@FXML
private Button cmdTest;

@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
System.out.println("READ");
System.out.println(this.getClass().getSimpleName() + ".initialize"); 
cmdTest.setOnAction(new EventHandler<ActionEvent>() {            
    @Override
    public void handle(ActionEvent event) {
    System.out.println("Button Pressed");                   
    colID.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("id"));
    colName.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("name"));
    colAddress.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("address"));
    colAge.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("age"));
    colContact.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("contact_num"));
    tblViewer.getItems().setAll(getAllstudentInfo());
    //tblViewer.getColumns().addAll(colID, colName, colAddress, colAge, colContact);
    //System.out.println(tblViewer.getItems().setAll(getAllstudentInfo()));
    }
} );       
}

public class studentInfo{

private final SimpleIntegerProperty idCol;
private final SimpleStringProperty nameCol;
private final SimpleStringProperty addressCol;
private final SimpleIntegerProperty ageCol;
private final SimpleStringProperty contact_numCol;

public studentInfo(Integer id, String name, String address, Integer age, String contact_num){
    this.idCol = new SimpleIntegerProperty(id);
    this.nameCol = new SimpleStringProperty(name);
    this.addressCol = new SimpleStringProperty(address);
    this.ageCol = new SimpleIntegerProperty(age);
    this.contact_numCol = new SimpleStringProperty(contact_num);
  }  
  public Integer getidCol(){
      return idCol.get();
  }
  public void setidCol(Integer id){
      idCol.set(id);      
  }
  public String getnameCol(){
      return nameCol.get();
  }
  public void setnameCol(String name){
      nameCol.set(name);
  }
  public String getaddressCol(){
      return addressCol.get();
  }
  public void setaddressCol(String address){
      addressCol.set(address);
  }
  public Integer getageCol(){
      return ageCol.get();
  }
  public void setageCol(Integer age){
      ageCol.set(age);
  }
  public String getcontact_numCol(){
      return contact_numCol.get();
  }
  public void setcontact_numCol(String contact_num){
      contact_numCol.set(contact_num);
  }        
  }


public List<studentInfo> getAllstudentInfo(){
Connection conn;
List ll = new LinkedList();
Statement st;
ResultSet rs;
String url = "jdbc:mysql://localhost/java_test";
String user = "root";
String pass = "";
String driver = "com.mysql.jdbc.Driver";

try{
    Class.forName(driver);
    conn = DriverManager.getConnection(url, user, pass);
    st = conn.createStatement();
    String recordQuery = ("Select * from student");            
    rs = st.executeQuery(recordQuery);
    while(rs.next()){    
        Integer id = rs.getInt("id");
        String name = rs.getString("name");
        String address = rs.getString("address");
        Integer age = rs.getInt("age");
        String contact_num = rs.getString("contact_num");
        ll.add(new studentInfo(id, name, address, age, contact_num));
        System.out.println(id +","+ name +","+ address +","+ age +","+ contact_num +" "+"added.");
    }            
}catch(ClassNotFoundException | SQLException ex){
    Logger.getLogger(TableViewController.class.getName()).log(Level.SEVERE, null, ex);
}
return ll;
}

这是 FXML 文件:

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="594.0" prefWidth="838.0" xmlns:fx="http://javafx.com/fxml" fx:controller="loadingcsvtotableview.TableViewController">
<children>
<TableView fx:id="tblViewer" prefHeight="521.0" prefWidth="808.0" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="15.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="59.0">
  <columns>
    <TableColumn prefWidth="75.0" text="ID" fx:id="colID" />
    <TableColumn prefWidth="175.0" text="Name" fx:id="colName" />
    <TableColumn prefWidth="330.0" text="Address" fx:id="colAddress" />
    <TableColumn prefWidth="75.0" text="Age" fx:id="colAge" />
    <TableColumn prefWidth="150.0" text="Contact Number" fx:id="colContact" />
  </columns>
 </TableView>
 <Button fx:id="cmdExport" layoutX="14.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="112.0" text="Export Data" AnchorPane.topAnchor="21.0" />
 <Button fx:id="cmdClose" layoutX="144.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="112.0" text="Close" AnchorPane.topAnchor="21.0" />
 <Button id="cmdClose" fx:id="cmdTest" layoutX="386.0" layoutY="21.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="112.0" text="Test" />
 </children>
</AnchorPane>

我缺少什么?请大家帮帮我..

【问题讨论】:

    标签: java class javafx-2 tableview


    【解决方案1】:

    属性值工厂与 bean 中的任何 getter 和 setter 函数都不匹配。因此,如果您将单元格值工厂设置为下一个:

    colID.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("id"));
    

    属性值工厂将查找函数 getId()、setId() 和 idProperty()。最后一个返回属性本身,即您在 bean 中拥有的属性。查看API of PropertyValueFactorytutorial of Table View 以深入了解这一点。

    也照 agonist_ 说的做,你正在创建一个列两次。

    有几件事。每次单击按钮时,您都在设置 CellValueFactory。你需要把它放在初始化而不是按钮事件中。

    getAllstudentInfo() 函数会阻塞你的 GUI,因为它阻塞了查询。您应该在后台线程中执行此操作。

    因此,请将您的 tblViewer.getItems().setAll(getAllstudentInfo()); 替换为:

    Service<ObservableList<studentInfo>> service = new Service<ObservableList<studentInfo>>() {                 
    @Override
    protected Task<ObservableList<studentInfo>> createTask() {
        return new Task<ObservableList<studentInfo>>() {                            
            @Override
            protected ObservableList<studentInfo> call() throws Exception {
                return FXCollections.observableArrayList(getAllstudentInfo());
            }
        };
    }
    };
    tblViewer.itemsProperty().bind(service.valueProperty());        
    service.start();
    

    看看concurrency tutorial of JavaFX 2肯定会对你有所帮助。

    希望对你有帮助。

    【讨论】:

    • 这个准确而中肯的答案消除了我的困惑。谢谢@Antonio J。
    【解决方案2】:

    首先不要在您的 FXML 导入上创建新实例。 只是

    @FXML
    private TableColumn<String> colContact;
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2012-06-26
      • 2013-06-01
      相关资源
      最近更新 更多