How can my JComboBox
show the first item before I click on it to choose an item? I ask this because I have a button which adds the selected item to a database but every time I add it and I refresh my combo box with this:
invItemCombo.setModel(new MyComboModelForInv(con));
my combo box goes back again to showing nothing. I must refresh it because every item can get in database only sometimes after this quantity ends and then the item title changes.
I tried a lot but I don't know what I have to do.:
this is where the button goes when it pressed:
private void actionerAdd() {
addln = new AddLineOrder(con, partsOFInv, partsOFCust, this);
invItemCombo.setModel(new MyComboModelForInv(con));
myTable.setModel(new MyModel(con));
}
in MyComboModelForInv i have:
public class MyComboModelForInv extends DefaultComboBoxModel {
public MyComboModelForInv(Connection con) {
this.con = con;
prepareForm();
}
private void prepareForm() {
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT * FROM inventory";
rs = stmt.executeQuery(query);
} catch (SQLException e) {
System.out.println("MyComboModelForInv: " + e.getMessage());
}
}
@Override
public String getElementAt(int index) {
String lstn = null;
try {
rs.absolute(index + 1);
if ((rs.getInt("quantity") <= 0)) {
lstn = rs.getString("category") + ", " + rs.getString("description") + ", " + "ΕΚΤΟΣ ΑΠΟΘΕΜΑΤΟΣ";
} else {
lstn = rs.getString("category") + ", " + rs.getString("description") + ", " + rs.getInt("price") + ", " + rs.getInt("idinv");
}
} catch (SQLException e) {
System.out.println("getElementAt(): " + e.getMessage());
}
return lstn;
}
@Override
public int getSize() {
int cnt = 0;
try {
rs.last();
cnt = rs.getRow();
rs.first();
} catch (SQLException ex) {
System.out.println("getSize(): " + ex.getMessage());
}
return cnt;
}`