【问题标题】:Change the value of JComboBox and JTextfield every time the mouse clicked?每次鼠标点击改变JComboBox和JTextfield的值?
【发布时间】:2016-04-07 16:09:38
【问题描述】:

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

截图

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

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 的所有值,而不是我选择的当前值。

【问题讨论】:

  • JOptionPane.showMessageDialog(null, value); 在您第二次点击后这条线是否有效?
  • @ReşitDönük 是的,它改变了我点击的任何单元格的值。但是 JComoBox 和 Textfields 不会。谢谢。
  • 您是否尝试打印此myRs.getString("SECTION_NAME")?它是否返回正确的值?
  • @ReşitDönük 是的,我试过了,但它会打印出我 ID 的所有值。
  • “我的身份证的所有价值”是什么意思?

标签: java swing jdbc jtable


【解决方案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 获取所有值并设置到组件中。

【讨论】:

  • 我看到这是我遇到错误的大部分内容。这就是它返回最后一个值的原因。我使用了sectionJTableMouseClicked,因此我可以确定选择了哪个值,以便轻松更新我的记录。谢谢:)
  • 所以你必须只用选定的id查询,或者只从JTable中获取数据。
  • 从 JTable 获取数据,因为我认为这是更新记录的最简单方法。感谢您的回复。
猜你喜欢
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
相关资源
最近更新 更多