如何在 JTable 中的 JComboBox 中获取 ID 和值
How get ID and value in JComboBox within JTable
JFrame
in my Program
如何通过 table?
的特定行从 JTable
中的 JComboBox
获取值 "item" 和 "ID"
Code inside 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();
}
}
和
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;
}
}
将组合框添加到第 1 行的 table。
Add JCombobox
into JTable
table 中的组合框是 table 的编辑器组件。除非您想自定义编辑器,否则不需要获取组合框的项目(JTable 框架会为您处理编辑)。相反,查询 table 的模型以获得您需要的值。使用 JTable.getModel()
检索 table 的模型并使用模型的 getValue(int, int)
方法检索实际值。
JFrame
in my Program
如何通过 table?
的特定行从JTable
中的 JComboBox
获取值 "item" 和 "ID"
Code inside 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();
}
}
和
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;
}
}
将组合框添加到第 1 行的 table。
Add JCombobox
into JTable
table 中的组合框是 table 的编辑器组件。除非您想自定义编辑器,否则不需要获取组合框的项目(JTable 框架会为您处理编辑)。相反,查询 table 的模型以获得您需要的值。使用 JTable.getModel()
检索 table 的模型并使用模型的 getValue(int, int)
方法检索实际值。