简体   繁体   English

如何在JTable的JComboBox中获取ID和值

[英]How get ID and value in JComboBox within JTable

JFrame in my Program 我程序中的JFrame

How to get value "item" and "ID" from JComboBox in JTable by specific row the table? 如何通过表的特定行从JTableJComboBox获取值“ item”和“ ID”?

Code inside JComboBox JComboBox内部的代码

public class ProductItem {
    private int id;
    private String Name;

    public ProductItem() {
    }

    public ProductItem(int id, String Name) {
        this.id = id;
        this.Name = Name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    @Override
    public String toString() {
        return getName();
    }

}

and

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

public class LoadItem {
    static ResultSet rs = null;
    public static DefaultComboBoxModel LoadProduct(){
        DefaultComboBoxModel ProDuctmodel = new DefaultComboBoxModel();
        try {
            rs = BarungCls.BarungSelect("select ID, Name from test_data ");
            while(rs.next()){
                ProDuctmodel.addElement(new ProductItem(Integer.parseInt(rs.getString(1)),rs.getString(2)));
            }
            return ProDuctmodel;
        } catch (SQLException e) {
                JOptionPane.showMessageDialog(null, e);
        }
        return null;
    }
}

Add combo box to table at row 1. 将组合框添加到表的第1行。

Add JCombobox into JTable JCombobox添加到JTable

The combobox in the table is the table's editor component. 表中的组合框是表的编辑器组件。 Unless you want to customize the editor, you do not need to get the item of the combo box (the JTable framework will handle the editing for you). 除非要自定义编辑器,否则不需要获取组合框的项目(JTable框架将为您处理编辑)。 Instead, query the table's model for the values you need. 而是,在表的模型中查询所需的值。 Use JTable.getModel() to retrieve the table's model and use the model's getValue(int, int) method to retrieve the actual values. 使用JTable.getModel()检索表的模型,并使用模型的getValue(int, int)方法检索实际值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM