在JavaFX中如何在表视图中添加组合框和数据

时间:2015-10-06 06:13:12

标签: mysql javafx javafx-2 javafx-8

我已经尝试了很多,但无法将数据库中的所有值填充到我的组合框表格单元格中。

Controller.java

public class controller {
GetConnection gc = new GetConnection();
PreparedStatement pst;
ResultSet rs;
Statement st;
private ObservableList<Users> datas = FXCollections.observableArrayList();

public controller() {
}

@FXML
private TableView<Users> table;
@FXML
private TableColumn<Users, String> c1;

@FXML
private void editable() {
    List<String> names = new ArrayList<String>();
    try {
        ObservableList<Users> datas = FXCollections.observableArrayList();
        String sql = "select * from itemsadd";
        pst = gc.getConnection().prepareStatement(sql);
        rs = pst.executeQuery();
        while (rs.next()) {
            String name = rs.getString("itemcode");
            names.add(name);
            System.out.println("probs" + names);
        }
        ResultSet rs2 = gc.getConnection().createStatement()
                .executeQuery("SELECT * FROM itemsadd WHERE itemcode=1001");

        while (rs2.next()) {
            datas.add(new Users(rs2.getString("itemcode")));
        }
        for (String name : names) {
            c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
            c1.setCellFactory(ComboBoxTableCell.forTableColumn(name));
 //not getting full items here
            System.out.println("hell3" + name);// am getting full items here
        }
        table.setItems(null);
        table.setItems(datas);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
}

public static class Users {
    private StringProperty Itemc;

    private Users(String Itemc) {
        this.Itemc = new SimpleStringProperty(Itemc);
    }

    public String getItemc() {
        return Itemc.get();
    }

    public void setItemc(String Itemc) {
        this.Itemc.set(Itemc);
    }

    public StringProperty ItemcProperty() {
        return Itemc;
    }
}
}

table.fxml             

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

        <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication47.controller">
           <children>
              <TableView fx:id="table" editable="true" layoutX="136.0" layoutY="58.0" onKeyPressed="#editable" prefHeight="200.0" prefWidth="335.0">
                <columns>
                  <TableColumn fx:id="c1" prefWidth="333.0" text="Name" />
                </columns>
              </TableView>
           </children>
        </AnchorPane>

Main.java 装载程序

public class Tableveiw extends Application {
private Stage primaryStage;
private AnchorPane pane;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("AddressApp");

    showPerson();
}

public void showPerson() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Tableveiw.class.getResource("table.fxml"));
        pane = (AnchorPane) loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);

        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}

数据库连接                    公共类GetConnection {

public Connection getConnection() throws Exception {

    Connection con = null;
    try {

        System.out.println("MySQL Connect Example.");

        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "login";
        String driver = "com.mysql.jdbc.Driver";
        // Accessing driver from the jar file
        Class.forName(driver).newInstance();
        // Creating a veriable for Connection Called conn
        con = DriverManager.getConnection(url + dbName, "root", "");

    } catch (Exception e) {
        e.printStackTrace();
    }
    return con;

}

public static void main(String arg[]) {
    GetConnection con = new GetConnection();
    System.out.println("Connection" + con);

}
}

我的代码问题是我没有将数据库记录中的完整项目提供给我的c1组合框表格单元格。只能在我的组合框架中获取数据库记录的最后一项。我已经创建了数组但仍然没有帮助。为什么我无法将数据库中的所有项目填充到我的组合框表格中。请帮助我对代码进行必要的更改。

1 个答案:

答案 0 :(得分:0)

这是基本功能。当你单击单元格组合框将可见时,你可以选择value.to看直接Combobox你已经编写了自己的TableCell类error handling。我希望这能帮到您。任何人发表评论

private void editable() {
    try {
        ObservableList<String> names = FXCollections.observableArrayList();
        ObservableList<Users> datas = FXCollections.observableArrayList();
        String sql = "select * from itemsadd";
        pst = gc.getConnection().prepareStatement(sql);
        rs = pst.executeQuery();
        while (rs.next()) {
            String name = rs.getString("itemcode");
            names.add(name);
            System.out.println("probs" + names);
        }
        ResultSet rs2 = gc.getConnection().createStatement()
                .executeQuery("SELECT * FROM itemsadd WHERE itemcode=1001");

        while (rs2.next()) {
            datas.add(new Users(rs2.getString("itemcode")));
        }
        c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
        c1.setCellFactory(ComboBoxTableCell.forTableColumn(name));
        table.setEditable(true);
        table.getItems().clear();
        table.setItems(datas);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }