Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从列表中隐藏jcombobox项,但仍使用它从数据库表中获取数据_Java_Jquery_Database_Swing_Jcombobox - Fatal编程技术网

Java 如何从列表中隐藏jcombobox项,但仍使用它从数据库表中获取数据

Java 如何从列表中隐藏jcombobox项,但仍使用它从数据库表中获取数据,java,jquery,database,swing,jcombobox,Java,Jquery,Database,Swing,Jcombobox,您好,所以不知怎的设法使该id不显示在组合框中,但当我按下“确定”按钮时,如何使其显示,我可以得到id值?使用 String from = (String) jComboBox1.getSelectedItem(); 当按下按钮时不起作用。。。我明白了 String code = (String) item.getValue(); 我需要的id,但如何将其传递给下一个查询 public void select() { try { String sql = "se

您好,所以不知怎的设法使该id不显示在组合框中,但当我按下“确定”按钮时,如何使其显示,我可以得到id值?使用

String from = (String) jComboBox1.getSelectedItem(); 
当按下按钮时不起作用。。。我明白了

String code = (String) item.getValue(); 
我需要的id,但如何将其传递给下一个查询

public void select() {
     try {
        String sql = "select * from category";
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();

        while (rs.next()) {

    jComboBox1.addItem(new Item<String>(rs.getString("mkid"), rs.getString("name")));

        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

    jComboBox1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox jComboBox1 = (JComboBox) e.getSource();
            Item item = (Item) jComboBox1.getSelectedItem();
            String code = (String) item.getValue();
            System.out.println(code);
        }
    });


}


The item

 public class Item<V> implements Comparable<Item>
 {
private V value;
private String description;


public Item(V value, String description)
{
    this.value = value;
    this.description = description;
}


public V getValue()
{
    return value;
}

public String getDescription()
{
    return description;
}

public int compareTo(Item item)
{
    return getDescription().compareTo(item.getDescription());
}

@Override
public boolean equals(Object object)
{
    Item item = (Item)object;
    return value.equals(item.getValue());
}

@Override
public int hashCode()
{
    return value.hashCode();
}


@Override
public String toString()
{
    return description;
}
public void select(){
试一试{
String sql=“从类别中选择*”;
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
jcombox1.addItem(新项(rs.getString(“mkid”)、rs.getString(“name”));
}
}捕获(例外e){
showMessageDialog(null,e);
}
jcombox1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
JComboBox jComboBox1=(JComboBox)e.getSource();
Item=(Item)jCombox1.getSelectedItem();
字符串代码=(字符串)item.getValue();
System.out.println(代码);
}
});
}
项目
公共类项实现了可比性
{
私人价值;
私有字符串描述;
公共项(V值、字符串描述)
{
这个值=值;
this.description=描述;
}
public V getValue()
{
返回值;
}
公共字符串getDescription()
{
返回说明;
}
公共整数比较(项目)
{
返回getDescription().compareTo(item.getDescription());
}
@凌驾
公共布尔等于(对象)
{
Item=(Item)对象;
返回值.equals(item.getValue());
}
@凌驾
公共int hashCode()
{
返回值。hashCode();
}
@凌驾
公共字符串toString()
{
返回说明;
}

您需要创建一个自定义对象来包含数据,然后将自定义对象添加到模型中。然后需要使用自定义渲染器来显示此对象


查看如何执行此操作的示例。

创建一个新类,该类将JCombobox作为父类,并具有一个私有变量
id
,您需要执行类似的操作,您可能会想这样做。对不起,我是新手,我读了几遍带有自定义渲染器的ComboBox,但仍然不知道该做什么。我也读过关于如何执行此操作的内容e JCombobox,但我还是遗漏了一些东西:(