在JavaFX中如何在表视图中添加组合框和数据 [英] In JavaFX how to add combobox with data in table view

查看:132
本文介绍了在JavaFX中如何在表视图中添加组合框和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried a lot but not able to populate all values in the data base into my combo box table cell.

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 {

Database Connection public class 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组合框表格单元格。只能在我的组合框架中获取数据库记录的最后一项。我已经创建了数组但仍然无法帮助。为什么我无法在数据库中填充整个项目进入我的组合框表格。请帮助我对代码进行必要的更改。

My problem with the code is that am not getting the full items in the database record to my c1 combobox table cell.Am only getting the last items of the database record in my comboboxtablecell.I have created array but still not helping .Why am not able to populate whole items in database into my combobox tablecell .Please help me with neccessary changes in the code.

推荐答案

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

This is Just basic functionality . when you duble click on cell combobox will visible then you can select value.to see direct Combobox you have write own TableCell class see this you ll understand. I hope this will help you. any ?s post a comment

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");
    }

这篇关于在JavaFX中如何在表视图中添加组合框和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆