0

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;
    }`
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What about method [setSelectedItem](https://docs.oracle.com/javase/8/docs/api/javax/swing/ComboBoxModel.html#setSelectedItem-java.lang.Object-) in interface `javax.swing.ComboBoxModel`? Refer to [How to Use Combo Boxes](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html) – Abra Jun 12 '21 at 12:48
  • Post code properly formatted. Once you paste the code you can select the code and then click on on the `{}` button. to retain the formatting in the forum. That is not an MRE. We don't have access to your database. Your question is about using the `setModel()` method of the combo box. Where the data comes from is irrelevant. So you just create a model, add some hardcoded data to the model and then add the model to the combo box. That is your MRE once you get that working, then you fix your problem in your real application. – camickr Jun 12 '21 at 13:21
  • I my question now is more beautiful :p But i didnt undesrstand what i have to do. – Spaliaras4k18cm Jun 12 '21 at 13:36
  • Code is formatted correct, but still not an MRE. We can't copy/paste/compile and test that code. Here is an example of an MRE: https://stackoverflow.com/a/4982576/131872. In your case the MRE would be a frame with a combo box (and some data) and a button. When you click the button you change the model by adding hard coded data to the model. If that works, great you know how to use the setModel() method. So know you replace the hardcoded data with your SQL query. If it doesn't work, then you post the MRE and we can help. We don't have access to your database so we can't help with that. – camickr Jun 12 '21 at 13:37
  • Your issue is with your SQL and custom model. Don't create a custom model. Just add the data to the combo box directly by iterating through the ResultSet, once the query is completed. – camickr Jun 12 '21 at 13:39
  • Okay, i think i cant paste all my 6 classes and my db... the only i want to know is how can i set selected item manual... – Spaliaras4k18cm Jun 12 '21 at 13:40
  • 1
    You were NOT asked to post your classes and db. I specifically stated we don't have access to your database. *only i want to know is how can i set selected item manual.* - Exactly! Forget about your application and learn how to do this. You were asked to create a new test class to learn how to update a combo box dynamically on the click of a button with hard coded data. The "MRE" will be about 20-30 lines of code in a single class. – camickr Jun 12 '21 at 13:44
  • You mean something like: rs.updateInt("quantity",rs.getInt("quantity")-1); – Spaliaras4k18cm Jun 12 '21 at 13:44
  • Look at the source code for class `javax.swing.DefaultComboBoxModel`. In the class constructor, the first item in the model is explicitly set as the selected item. The code in class `MyComboModelForInv` (as posted in your question) does **not** set the selected item, hence when you create a new `MyComboModelForInv` and set it as the model for your `JComboBox`, nothing is shown. – Abra Jun 12 '21 at 13:44
  • 1
    *You mean something like* - absolutely not. Your question about a combo box. It is irrelevant where the data comes from. You first need to learn how to use the combo box correctly. Then worry about how the data is loaded into the combo box. The fact that you are extending DefaultComboBoxModel is unnecessary and is likely causing problems. – camickr Jun 12 '21 at 13:46
  • BTW - the term MRE that @camickr used, refers to a [mre]. Before that I wrote a longer document called the [Short, Self Contained, Correct Example](http://www.sscce.org/). It is essentially the same thing, but the latter document goes into more detail as to how, and why to make a short code example. I agree with camickr re removing the DB from the mix at this stage. Simply have a text field to enter a new item in the combo box, and add an action listener to the field so when the tester types something and presses enter, the data should be added to the combo box. Work that out & the answer .. – Andrew Thompson Jun 12 '21 at 14:01
  • .. to the wider problem should be clear. – Andrew Thompson Jun 12 '21 at 14:01
  • Okay thank you very much guys, i finally found this: p=invItemCombo.getSelectedIndex(); invItemCombo.setModel(new MyComboModelForInv(con,invItemCombo)); invItemCombo.setSelectedIndex(p); – Spaliaras4k18cm Jun 12 '21 at 14:12
  • That solution may not work. What if you add an item to the combo box that appears before the previously selected item. The index of that previously selected item will change – camickr Jun 12 '21 at 14:31
  • I add items to ComboBox from other JDialog, so when the user uses the combobox he must leave to insert new items. – Spaliaras4k18cm Jun 12 '21 at 14:38

0 Answers0