![](/img/trans.png)
[英]How to change a value in the Jtextfield or JComboBox using the data structure linkedlist?
[英]Change the value of JComboBox and JTextfield every time the mouse clicked?
每次单击作为我的数据的 JTable 的单元格时,如何正确更改我的 Swing 控件(如 JComboBox 和 JTextfields)的值? 如果有多个记录与我选择的第一个字母相同,我想做什么。 每次单击时都会根据行位置更改值吗?
截屏
当我单击 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
所有值,而不是我选择的当前值。
我想我发现了问题:
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
获取所有值并设置为组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.