In JavaFX how to add combobox with data in table view

家住魔仙堡 提交于 2019-12-31 05:57:05

问题


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 loader

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

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

}
}

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.


回答1:


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


来源:https://stackoverflow.com/questions/32962914/in-javafx-how-to-add-combobox-with-data-in-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!