0

I have a form that has a ComboBox which it gets its items from a database. The combobox takes numerous column-items from a table inside the database. I want to take only one of these items (from the combobox) and copy it to a JTextField.

Here's the code of the creation of the ComboBox in the Order.java file:

cbinv = new JComboBox<>();
cbinv.setModel(new InvComboModel(con));

and the code from the InvComboModel.java:

public InvComboModel(Connection con) {
        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("InvComboModel: " + e.getMessage());
        }
    }

    @Override
    public String getElementAt(int index) {
        String lstn = null;
        try {
            rs.absolute(index + 1);
            lstn = rs.getString("category") + ", " + rs.getString("description") + ", "
                    + rs.getInt("price") + ", " + rs.getInt("quantity");
        } catch (SQLException e) {
            System.out.println("getElementAt(): " + e.getMessage());
        }
        return lstn;
    }
    
    @Override
    public int getSize() {
        int cnt = 0;
        try {
            rs.last();
            cnt = rs.getRow();
        } catch (SQLException ex) {
            System.out.println("getSize(): " + ex.getMessage());
        }
        return cnt;
    }

    public int getIdInvAt(int index) {
        int idInv = 0;
        try {
            rs.absolute(index + 1);
            idInv = rs.getInt("idinv");
        } catch (SQLException e) {
            System.out.println("getElementAt(): " + e.getMessage());
        }
        return idInv;
    }

So, I want when I select something on the Inventory Item to take the third value (which in this case is 500, example image) and copy it to the JTextField of the ItemPrice.

[example][1]: https://i.stack.imgur.com/BWQVw.jpg

In the Order.java file I have the following command but it copies all the selected item in the combobox:

tip.setText((String) cbinv.getSelectedItem());

and when I use the following command it takes the whole line again. It seems that I can't use any other method from the InvComboModel.java file

tip.setText((String) cbinv.getModel().getElementAt());

Thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You cant just get a selected item and assign the whole item to your textBox. You first need to split the string. Try `String item3 = cbinv.getSelectedItem().split(",")[2];`, note the `[2]` at the end which we use to get the third item in the split (at index 2), then once we have that we can assign the split value to your text box `tip.setText(item3);` – sorifiend Jun 14 '21 at 23:22
  • Does this answer your question? [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – sorifiend Jun 14 '21 at 23:26
  • For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. – Andrew Thompson Jul 12 '21 at 10:53

0 Answers0