将数据加载到组合框中的问题

时间:2014-12-05 19:39:56

标签: java swing combobox

public void muestraRegistro(JTextField t1 ,JTextField t2 , JTextField t3 , JTextField t4 , JTextField t5,JTextField t6 , JComboBox cmb) {
    try {

        int row = ProductoGUI.tblDetalle.getSelectedRow();  
        String muestra = ProductoGUI.tblDetalle.getModel().getValueAt(row, 0).toString();
        //String muestra = ProductoGUI.tblDetalle.getValueAt(ProductoGUI.tblDetalle.getSelectedRow(), 0).toString();
        rs = cargaRegistro();
        while (rs.next()) {                 
                t1.setText(rs.getString(1));
                t2.setText(rs.getString(2));                                     
                cmb.setSelectedItem(rs.getString(3));
                System.out.print(cmb);
                t3.setText(rs.getString(4));
                t4.setText(rs.getString(5));
                t5.setText(rs.getString(6));
                t6.setText(rs.getString(7));

            habilitaTexto(false, t2, t3, t4, t5, t6);
        }
    } catch (SQLException | HeadlessException ex) {             
        JOptionPane.showMessageDialog(null,ex);
        Logger.getLogger(ProductoDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
}   

我有代码,但是在从combobox jtable加载数据时我遇到了以下错误:

javax.swing.JComboBox[,113,91,200x18,layout=com.sun.java.swing.plaf.windows.WindowsComboBoxUI$3,alignmentX=0.0,alignmentY=0.0,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=1]javax.swing.JComboBox[,113,91,200x18,layout=com.sun.java.swing.plaf.windows.WindowsComboBoxUI$3,alignmentX=0.0,alignmentY=0.0,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=1]javax.swing.JComboBox[,113,91,200x18,layout=com.sun.java.swing.plaf.windows.WindowsComboBoxUI$3,alignmentX=0.0,alignmentY=0.0,border=,flags=328,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=1]

1 个答案:

答案 0 :(得分:-1)

这个简单的示例显示了一个组合框,其中包含一个包含三个字符串的组合框模型。 println在我的笔记本电脑上打印字符串cmb.getSelectedIndex(): 1(对应于模型中的第二个字符串)。

final JComboBox<String> cmb = new JComboBox<>(new String[]{"123", "456", "789"});
cmb.setSelectedItem("456");
System.out.println("cmb.getSelectedIndex(): " + cmb.getSelectedIndex());