1

I tried the below code and am getting values of "names" inside the while loop for a combo box.But coming outside the loop am only getting last value from database.

@FXML
private void fillcombobox()
{
    try
    {
        String sql = "select * from location";
        pst = gc.getConnection().prepareStatement( sql );
        rs = pst.executeQuery();
        while ( rs.next() )
        {
            String names = rs.getString( "Address" );
            combobox.getItems().add( names );
        }
    }
    catch ( Exception e )
    {
        System.out.println( "" + e );
    }
}

The above code fetches all values from table location and get the all values present in column "Address". And using object names I am getting values in combo box now using this code:
combobox.getItems().add(names);

Please help me the possible way to get values outside the while loop.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
Seban
  • 184
  • 4
  • 20
  • 1
    The combobox.getItems() contains the list of names which can be accessed outside the loop. – Uluk Biy Oct 07 '15 at 06:42
  • @UlukBiy actually i need to access object "names" outside loop when ever i tried to access names object outside it only gives me last value in the column "Address". – Seban Oct 07 '15 at 06:46
  • 1
    The "String names" is actually only an **one** object, i.e. it keeps the address name (not names) of the current processing record (of recordset). So at the end of the loop it will keep the address name of the last record. combobox.getItems() contains the list of name objects. – Uluk Biy Oct 07 '15 at 06:51
  • @UlukBiy when ever i try to access names outside loop for this code columnmain2.setCellValueFactory(new PropertyValueFactory("Itemc")); columnmain2.setCellFactory(ComboBoxTableCell.forTableColumn(names)); i am only getting only last value in "ADDRESS NAME" in above code – Seban Oct 07 '15 at 06:59
  • 1
    Use the "names" in the @user99370 's answer below. It contains all addresses. – Uluk Biy Oct 07 '15 at 07:03
  • @UlukBiy tnq u so much it works!! – Seban Oct 07 '15 at 07:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91562/discussion-between-seban-and-uluk-biy). – Seban Oct 07 '15 at 07:40

1 Answers1

2

The String is single object it can store one value only, so for every loop it change value. that is the reason your getting last value. any ?s post comment

@FXML
private void fillcombobox() {
List<String> names=new ArrayList<String>();
  try {
    String sql = "select * from location";
    pst = gc.getConnection().prepareStatement( sql );
    rs = pst.executeQuery();
    while ( rs.next() )
    {
        String name = rs.getString( "Address" );
        names.add(name );
        combobox.getItems().add( names );
    }
    for(String name:names){
       System.out.println(name);
    }
 }
  catch ( Exception e )
  {
    System.out.println( "" + e );
  }
}
Gaali Prabhakar
  • 583
  • 6
  • 23
  • 1
    System.out.println("hell2"+name);//am getting all values here columnmain2.setCellFactory(ComboBoxTableCell.forTableColumn(name));//but not here } whenever i add to comboboxtablecell it shows only last value in record. – Seban Oct 07 '15 at 07:35
  • 2
    i have to c your code then only i can tell what is wrong why your not getting values – Gaali Prabhakar Oct 07 '15 at 08:29
  • here is my whole code:http://stackoverflow.com/questions/32962914/how-to-populate-all-values-in-database-to-combo-box-inside-my-table-view-in-java – Seban Oct 07 '15 at 10:42
  • 2
    i have solved problem .problem withe the way your adding data to setCellFactory.i ll post the code in above ? – Gaali Prabhakar Oct 07 '15 at 16:07