点击Jtable 后 如何让jcombobox 值为点击Jtable的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了点击Jtable 后 如何让jcombobox 值为点击Jtable的值相关的知识,希望对你有一定的参考价值。

给Jtable 添加了监听,但是在点击Jtable时向让相应的Jcombobox显示相应的数据,不知道如何显示了 ???

参考技术A 当你选中的单元格后(应该是增加的ActionListener 事件吧!),再得到 金融系的值,把它存起来,在设置Jcombobox的值为金融系即可(用setSelectedItem(Object anObject方法)。本回答被提问者采纳

从数据库中获取值后如何清除 jComboBox

【中文标题】从数据库中获取值后如何清除 jComboBox【英文标题】:How to clear jComboBox after fetching value from database 【发布时间】:2013-05-27 08:41:49 【问题描述】:

谁能帮我解决这个代码?

在这里输入代码我想问一下当从代码中的jComboBox3中选择另一个值时如何清除JcbSub(jComboBox):

1 private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) 
2    Connection con;
3    Statement stmt;
4    try 
5        
6        Class.forName("sun.jdbc.odbc.JdbcOdbc");
7     catch (ClassNotFoundException ex) 
8        JOptionPane.showMessageDialog(null, ex);
9    
10    try 
11       con= DriverManager.getConnection("Jdbc:Odbc:food");
12        stmt= con.createStatement();
13        String sql="select i_name from food where category= '"+ jComboBox3.getSelectedItem().toString()+"'";
14        ResultSet RS= stmt.executeQuery(sql);
15        JcbSub.setSelectedItem("");
16        while(RS.next())
17            
18            JcbSub.addItem(RS.getString("i_name"));
19            
20            
21        
22        
23       
24        
25     catch (SQLException ex) 
26        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
27    
28    
29    
30   
31 

【问题讨论】:

如果您希望人们阅读您的代码,请遵循标准 Java 变量命名约定。变量名(RS、JcbSub)不应以大写字符开头。 感谢您的建议...我以后会处理的... :-) 【参考方案1】:

你必须这样做

 private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) 
    Connection con;
    Statement stmt;
 try 

    Class.forName("sun.jdbc.odbc.JdbcOdbc");
 catch (ClassNotFoundException ex) 
    JOptionPane.showMessageDialog(null, ex);
 
  //add this to remove all Items
     JcbSub.removeAllItems();
 if(jComboBox3.getSelectedItem() == 0) 
  try 


     con= DriverManager.getConnection("Jdbc:Odbc:food");
     stmt= con.createStatement();
     String sql="select i_name from food where category= '"+jComboBox3.getSelectedItem().toString()+"'";
     ResultSet RS= stmt.executeQuery(sql);
     JcbSub.setSelectedItem("");
     while(RS.next())

        JcbSub.addItem(RS.getString("i_name"));


    



 catch (SQLException ex) 
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);


else if(jComboBox3.getSelectedItem()  == 1) 
//etc... 

// or use Switch case    


【讨论】:

以上是关于点击Jtable 后 如何让jcombobox 值为点击Jtable的值的主要内容,如果未能解决你的问题,请参考以下文章

在Jtable单元格中单击JComboBox时调用ActionPerformed

单击以打开 JComboBox 时,JTable 单元格失去价值

如何将JComboBox添加到JTable单元格?

如何在 Swing 中的 JTable 的列中添加不同的 JComboBox 项

如何使用绑定类通过代码将 JComboBox selectedItem 绑定到 Jtable?

怎么在JTable表格中加入如JComboBox之类的控件?有注释加分。