0

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.

7
  • 1
    The combobox.getItems() contains the list of names which can be accessed outside the loop. Commented Oct 7, 2015 at 6: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". Commented Oct 7, 2015 at 6: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. Commented Oct 7, 2015 at 6: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 Commented Oct 7, 2015 at 6:59
  • 1
    Use the "names" in the @user99370 's answer below. It contains all addresses. Commented Oct 7, 2015 at 7:03

1 Answer 1

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 );
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

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.
i have to c your code then only i can tell what is wrong why your not getting values
i have solved problem .problem withe the way your adding data to setCellFactory.i ll post the code in above ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.