0

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.

Seban
  • 184
  • 4
  • 20
  • 1
    Can you explain what you think this code does? It makes no sense to me. Why are you configuring the columns in a loop? Why are you creating a new data list on every iteration in the loop? You seem to be creating a `ComboBoxTableCell` with a single item (`names`); is this intentional? – James_D Oct 06 '15 at 10:45
  • 1
    if you are new to anything, start to learn at the bottom and in small steps: f.i. first plain edits in a tableView, then custom edit handlers, then specialized cells (like combo), then dynamic lists in the combo, (and or in the tableView itself) then (as the very very last step) the interaction with the database (which is rather independent from pure fx). When you are stuck at any of those step, create a SSCCE and post a question about that step only :-) – kleopatra Oct 07 '15 at 09:15
  • no, not without your environment ;-) Missing fxml and database: you should provide the former and simulate the latter (simple hard-coded lists/maps are fine) – kleopatra Oct 07 '15 at 10:25
  • @kleopatra I have edited with my full source code .Please help me with code to bring all items in the data base to comboboxtablecell. – Seban Oct 07 '15 at 11:38
  • @James_D i have edited my code with some changes and code is runnable but am not getting all values of database am only getting last value of the record – Seban Oct 07 '15 at 11:39
  • it's still not self-contained (please re-read my last comment carefully, particularly the part about how to replace the database part - which obviously we don't have ;) – kleopatra Oct 07 '15 at 11:44
  • @Seban please change the ?s Title to In javafx How to add combobox with data in table view (not exactly like this meaning should be this). if you change this it will help full to others – Gaali Prabhakar Oct 08 '15 at 15:56

1 Answers1

0

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");
    }
Community
  • 1
  • 1
Gaali Prabhakar
  • 583
  • 6
  • 23
  • 1
    Please help me http://stackoverflow.com/questions/33031543/how-to-change-values-in-tableview-dynamically-whenever-i-select-an-corresponding – Seban Oct 09 '15 at 06:45
  • 2
    can i give a example ?,because i can get data from your data base right? so i will give a example and you change your code, dont worry i ll try to change your code tooo. but if cant get time to change your code. you need to change your code if it is ok let me know i ll give a example – Gaali Prabhakar Oct 09 '15 at 12:44
  • 3
    sorry Seban i m on holyday i ll come after 2 day . now i dont have laptop with me.if i get any computer i ll send u. other wise i ll send after 2 days – Gaali Prabhakar Oct 11 '15 at 16:39