문제

I am working on a small project which assigned to me by my teacher I am beginner to java I have used combo box in my form to fetch data from database... I am able to get the data from the database while clicking on item1 in the combo box.. I want to get rid of item
so that i can get values directly here's my code...

import java.sql.*;
Statement stmt=null;
Connection con=null;
try
  {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con=DriverManager.getConnection("jdbc:odbc:sql_login");
    stmt=con.createStatement();
    String search="select * from tblflight";
    ResultSet rs=stmt.executeQuery(search);
    while(rs.next())
 {
    Date d1 = new Date(rs.getDate("departure_date").getTime());
        combo_box.addItem(d1);//
 }
        rs.close();
        con.close();
}catch(Exception e)
{
        e.printStackTrace();
}
 combox[ITEM1,ITEM2,ITEM3,ITEM4]//When form displays it shows these items
 but when I remove these items from properties I found combobox empty
 !![this is the first img when i open my form][1]
 ![this is the second img when i click any of the item][2]

![1]: http://i.stack.imgur.com/ez5fa.png "hidden"

![2]: http://i.stack.imgur.com/orlDK.png "hidden"

도움이 되었습니까?

해결책

To get selected value you can do it as follows,

String sSelectedValue = combobox.getSelectedItem().toString();

To get selected item index

int iSelectedItemIndex = combobox.getSelectedItemIndex();

To remove item.

combobox.removeItemAt(index); //index is the index of the item you want to remove from the list.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top