每次鼠标点击时更改JComboBox和JTextfield的值?

时间:2016-04-07 16:09:39

标签: java swing jdbc jtable

每次单击我的JTable的单元格时,如何正确更改我的Swing控件的值,如JComboBox和JTextfields?如果有多个记录与我选择的第一个字母相同,我想要做什么。每次我根据行位置点击时会改变值吗?

截图

enter image description here

当我单击JTable中的一个值时,它会显示JTextfield和JComboBox中的当前值但是如果我再次单击它它没有改变?有什么帮助吗?

SELECT(用于检索)

private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String searchSection = Section_SearchSection_Textfield.getText();

String searchSECTIONSETTINGS = "SELECT allsections_list.SECTION_ID, allsections_list.SECTION_NAME, allsections_settings.ADVISER_ASSIGNED, allsections_settings.SECTION_POPULIMIT, allsections_settings.ROOM_ASSGN,\n" +
    "allsections_settings.YRLEVEL_ASSGN,allsections_settings.SCHOOL_YEAR,allsections_settings.SESSION_ASSIGNED\n" +
    "FROM allsections_list\n" +
    "RIGHT JOIN allsections_settings\n" +
    "ON allsections_list.SECTION_ID = allsections_settings.SECTION_ID\n" +
    "WHERE SECTION_NAME LIKE ?";

    try (Connection myConn = DBUtil.connect();
                PreparedStatement myFirstPs = myConn.prepareStatement(searchSECTIONSETTINGS);)
               {
                    myFirstPs.setString(1, '%'+searchSection+'%' );
                try (ResultSet myFirstRs = myFirstPs.executeQuery();)
                {
                    sectionJTable.setModel(DbUtils.resultSetToTableModel(myFirstRs));
                    int result = 0;
                    while (myFirstRs.next())
                    {
                        String myName = myFirstRs.getString(2);
                        System.out.println(myName);
                        result++;
                    }
       }//end of try myFirstRs (ResultSet)
       }//end of try myFirstPs (PreparedStatement)
 }

JTable(鼠标点击)

private void sectionJTableMouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 1) {
        final JTable target = (JTable)evt.getSource();
        final int row = target.getSelectedRow();
        final int column = target.getSelectedColumn();
        // Cast to ur Object type
        Object value = target.getValueAt(row,column);
        JOptionPane.showMessageDialog(null, value);

String selectSections = "SELECT * FROM allsections_list a JOIN allsections_settings b ON b.SECTION_ID = a.SECTION_ID";

      try (Connection myConn = DBUtil.connect();
                PreparedStatement myPs = myConn.prepareStatement(selectSections);)
        {
            try (ResultSet myRs = myPs.executeQuery())
            {
                int resultCounter = 0;
                while(myRs.next())
                {
                    Section_SectionName_TextField.setText(myRs.getString("SECTION_NAME"));  
                    Section_Student_Limit_ComboBox.setSelectedItem(myRs.getString("SECTION_POPULIMIT"));
                    Section_Room_Assignment_ComboBox.setSelectedItem(myRs.getString("ROOM_ASSGN"));
                    Section_Student_Limit_ComboBox1.setSelectedItem(myRs.getString("ADVISER_ASSIGNED"));
                    Section_Session_Settings_ComboBox.setSelectedItem(myRs.getString("SESSION_ASSIGNED"));
                    Section_Session_Level_ComboBox.setSelectedItem(myRs.getString("YRLEVEL_ASSGN"));
                    Section_SchooYear_ComboBox.setSelectedItem(myRs.getString("SCHOOL_YEAR"));
                    resultCounter++;
                }
            }
        }
}

当我尝试在第二个结果集中添加System.out.print(myRs.getString("SECTION_NAME"));时,它会打印出SECTION_NAME的所有值,而不是我选择的当前值。

1 个答案:

答案 0 :(得分:1)

我想我发现了问题:

while(myRs.next())
{
    Section_SectionName_TextField.setText(myRs.getString("SECTION_NAME"));  
    Section_Student_Limit_ComboBox.setSelectedItem(myRs.getString("SECTION_POPULIMIT"));
    Section_Room_Assignment_ComboBox.setSelectedItem(myRs.getString("ROOM_ASSGN"));
    Section_Student_Limit_ComboBox1.setSelectedItem(myRs.getString("ADVISER_ASSIGNED"));
    Section_Session_Settings_ComboBox.setSelectedItem(myRs.getString("SESSION_ASSIGNED"));
    Section_Session_Level_ComboBox.setSelectedItem(myRs.getString("YRLEVEL_ASSGN"));
    Section_SchooYear_ComboBox.setSelectedItem(myRs.getString("SCHOOL_YEAR"));
    resultCounter++;
}

在上面的部分中,您将在while语句中将所有记录设置为组件。因此,组件将仅显示最后一条记录,在本例中为" Gold"。

但实际上没有必要在sectionJTableMouseClicked中查询数据库。您可以按getValueAt获取所有值并设置为组件。