-2

好的,所以我知道这是一个常见问题,已经发布了很多,但是尽管我尝试遵循给出的建议,但我的 TableView 直到显示没有数据......我会稍微减少我的对象以保持事情不变尽可能短。这是我的对象:

public SimpleStringProperty itemCode, itemName; 

public ResourceItem(String code, String name) {
    this.itemCode = new SimpleStringProperty(code);
    this.itemName = new SimpleStringProperty(name);
}

public String getItemCode() {
    return itemCode.get();
}

public void setItemCode(String code) {
    itemCode.set(code);
}

public SimpleStringProperty itemCodeProperty() {
    return itemCode;
}

public SimpleStringProperty itemNameProperty() {
    return itemName;
}

public String getItemName() {
    return itemName.get();
}

public void setItemName(String name) {
    itemName.set(name);
}

这里是我创建 TableColumns 的地方:

TableColumn<ResourceItem, String> code = new TableColumn("Item Code"); 
code.setCellValueFactory(new PropertyValueFactory("itemCode"));
TableColumn<ResourceItem, String> code = new TableColumn("Item Name"); 
name.setCellValueFactory(new PropertyValueFactory("itemName"));

我通过 for 循环将资源项添加到 ObservableList 并将 TableView 的项设置到该列表:

ObservableList<ResourceItem> data = FXCollections.observableArrayList();
....
itemsInDB.setItems(data);
itemsInDB.getColumns().addAll(code, name);

然后什么都没有添加。有人可以帮帮我吗?

编辑:这是一个可测试的版本。它确实需要您设置一个名为 ims 的数据库,一个名为 im_resoureitem_br 的表,其中包含两列:IMItemCode Varchar(4) 和 IMItemName Varchar(30)。

public class TableViewTest extends Application {

final String DRIVER = "com.mysql.jdbc.Driver";
String urlHead = "jdbc:mysql://localhost/ims";

final String USER = "root";
final String PASS = "";

Connection connection;
Statement statement;

private TableView<ResourceItem> table = new TableView<ResourceItem>(); //creates table to hold Course objects
private final ObservableList<ResourceItem> data
        = FXCollections.observableArrayList();

@Override
public void start(Stage stage) throws ClassNotFoundException {
    Scene scene = new Scene(new Group());
    stage.setTitle("Fall 2015 Schedule"); //title of stage, appears at top bar
    stage.setWidth(700);
    stage.setHeight(500);

    final Label label = new Label("Brenna Morss-Fish Fall Schedule 2015");

    table.setEditable(true);

    table.setItems(data); //sets rows of table as data from course arraylist
    TableColumn<ResourceItem, String> code = new TableColumn<ResourceItem, String>("Code:");//creates first column
    code.setMinWidth(100);
    code.setCellValueFactory(
            new PropertyValueFactory("itemCode"));
    TableColumn<ResourceItem, String> name = new TableColumn<ResourceItem, String>("Name:");//creates first column
    name.setMinWidth(100);
    name.setCellValueFactory(
            new PropertyValueFactory("itemName")); //defines what column holds according to name field of Course class

    String query = "select * from ims.im_resourceItem_br; ";
    ArrayList<String[]> items = new ArrayList<String[]>();

    TableView<ResourceItem> itemsInDB = new TableView();
    items = getQueryResult(query);
    //itemsInDB.setEditable(false);
    ResourceItem item = new ResourceItem("", "");
    ObservableList<ResourceItem> data = FXCollections.observableArrayList();
    data.removeAll(data);
    //System.out.println(items.get(0).toString()); 
    for (int i = 0; i < items.size(); i++) {
        item.setItemCode(items.get(i)[1]);
        item.setItemName(items.get(i)[2]);
        data.add(item);
    }

    code.setCellValueFactory(new PropertyValueFactory("itemCode"));
    name.setCellValueFactory(new PropertyValueFactory("itemName"));

    itemsInDB.setItems(data);

    System.out.println(itemsInDB.getItems());

    itemsInDB.getColumns().addAll(code, name);

    table.getColumns().addAll(code, name);
    //adds previously defined columns to the table in the order they will appear
    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.getChildren().addAll(label, table); //adds label and course table to VBox layout container

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene); //adds scene to the stage
    stage.show(); //displays stage
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

public ArrayList getQueryResult(String stmt) throws ClassNotFoundException {
    String results = "";
    ResultSet resultSet = null;
    String row = "";
    ArrayList<String[]> list = new ArrayList<String[]>();
    try {

        Class.forName(DRIVER);
        connection = DriverManager.getConnection(urlHead, USER, PASS);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(stmt);

        int columnCount = resultSet.getMetaData().getColumnCount();

        while (resultSet.next()) {
            String delims = "[%]";
            row = "";
            for (int i = 1; i <= columnCount; i++) {
                row += resultSet.getString(i) + "%";
            }
            String[] array = row.split(delims);

            list.add(array);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return list;
}

public static class ResourceItem {

    public SimpleStringProperty itemCode, itemName;

    public ResourceItem(String code, String name) {
        this.itemCode = new SimpleStringProperty(code);
        this.itemName = new SimpleStringProperty(name);
    }

    public String getItemCode() {
        return itemCode.get();
    }

    public void setItemCode(String code) {
        itemCode.set(code);
    }

    public SimpleStringProperty itemCodeProperty() {
        return itemCode;
    }

    public SimpleStringProperty itemNameProperty() {

        return itemName;
    }

    public String getItemName() {
        return itemName.get();
    }

    public void setItemName(String name) {
        itemName.set(name);
    }

public String toString() {
    String print = itemCode + " " + itemName + " ";

    return print; 
}
}

}

4

1 回答 1

0

您已经创建了两个表:一个名为table,当您将其添加到时在 UI 中显示vbox,另一个名为itemsInDB,您填充但从不显示。由于您显示的表格没有数据,并且您填充的表格永远不会显示,因此您永远看不到数据。

当您填充可观察列表时,您还有另一个逻辑错误:您一遍又一遍地将相同的项目添加到列表中,并且每次都更新其属性。因此,如果显示它,您会在表格中重复看到相同的项目。

可能还有其他我没有注意到的错误,但是由于您尚未创建 MCVE,因此我无法实际运行它并对其进行测试。

于 2016-04-05T22:42:28.867 回答