2016-05-24 10 views
-1

私はcostumerテーブルを持つmysql DBに2つのレコードが含まれています。id 1 name omar、id 3 name ahmad。 は、私が取得するには、このクラスを使用し、IDと名前を設定しmysqlの自動番号Jcomboboxの代わりにIDを値に追加

public class ComboItem 
{ 
private int index; 
private String name; 

public ComboItem(int pindex, String pName) 
{ 
    this.index = pindex; 
    this.name = pName; 
} 

public int GetIndex() 
{ 
    return this.index; 
} 

public String GetName() 
{ 
    return this.name; 
} 

public void SetIndex(int pindex) 
{ 
    this.index = pindex; 
} 

public void SetName(String pName) 
{ 
    this.name = pName; 
} 

private void FillNameComboBox(JComboBox combo) throws IOException{ 
    try{ 
     combo.removeAllItems(); 
     Connect c =new Connect(); 
     Statement stmnt= c.MakeConnection().createStatement(); 
     ResultSet rset=stmnt.executeQuery("select idCostumer,Name from Costumer"); 
     while(rset.next()){ 
      int index=Integer.parseInt(rset.getString("idCostumer")); 
      String name=rset.getString("Name"); 
      ComboItem ci=new ComboItem(index, name); 
      combo.addItem(ci); 
     } 

    } 
    catch(SQLException e){ 
     JOptionPane.showMessageDialog(NameComboBox, e); 
    } 
} 

私はそれが私のインデックスにないIDを与え続けてアプリを実行するたびに、メインクラス...私は何をすべき? screen shot

答えて

1

あなたが最初のコンポボックスのクラスを作成するには、次の

1-行うことによってJcompoboxにIDと名前を付けることができ親愛なるオマル は「ComboItem」という名前を付け、次の操作を行い、それを与えることができます:

そのよ後パブリッククラスComboItem {

private int index; 
private String name; 
private String col; 

public ComboItem(int pindex, String pName) { 
    this.index = pindex; 
    this.name = pName; 
} 

public ComboItem(int pindex, String pcol, String pName) { 
    this.index = pindex; 
    this.col = pcol; 
    this.name = pName; 

} 

public int GetIndex() { 
    return this.index; 
} 

public String GetName() { 
    return this.name; 
} 

public String GetCol() { 
    return this.col; 
} 

public int SetIndex(int pindex) { 
    this.index = pindex; 
    return pindex; 
} 

public String SetName(String pName) { 
    this.name = pName; 
    return pName; 
} 

public String SetCol(String pCol) { 
    this.col = pCol; 
    return pCol; 
} 

// This will be used internally by JComboBox as the label to be displayed. 
@Override 
public String toString() { 
    return name; 
} 

// to get the Index of Combobox depends on ComboItem Class 
public static int getIndexById(JComboBox combo, int id) { 

    for (int i = 1; i < combo.getItemCount(); i++) { 
     try { 
      ComboItem c = (ComboItem) combo.getItemAt(i); 
      int tempId = c.GetIndex(); 
      if (tempId == id) { 
       return i; 
      } 
     } catch (Exception e) { 

     } 
    } 
    return 0; 
} 

}

Uは、以下のように、他のクラスでのコンポボックスの項目を設定し、取得することができます:

セットアイテムIDと名前

 try 
    { 
     combo.removeAllItems(); 
     conn = Operations.SqlConnector(); 
     String sql = "select EmpID, EmpName from Emp"; 
     pst = conn.prepareStatement(sql); 
     rs = pst.executeQuery(); 
     ComboItem nullValue = new ComboItem(0,"- select -"); 
     combo.addItem(nullValue); 
     while(rs.next()) 
     { 
      int index = Integer.parseInt(rs.getString("EmpID")); 
      String name = rs.getString("EmpName"); 
      ComboItem c = new ComboItem(index,name); 
      combo.addItem(c); 
      c.GetName(); 
     } 
     pst.close(); 
     rs.close(); 
    } 
    catch(SQLException | NumberFormatException e) 
    { 
     JOptionPane.showMessageDialog(null, e); 
    } 

アイテムのIDまたは名前

ComboItem dept = (ComboItem) ComboBox_DeptID.getSelectedItem(); 
int deptID = dept.GetIndex(); 

とすることができますを取得します次のようにGetIndexByID()を使用してください。

int dept = ComboItem.getIndexById(ComboBox_DeptID, BindList().get(index).GetDeptID()); 
     ComboBox_DeptID.setSelectedIndex(dept); 

お待ちしております。

関連する問題