gpt4 book ai didi

java - 使用 Jcombobox 更改项目后编辑 jtable 不起作用

转载 作者:行者123 更新时间:2023-12-01 11:59:20 25 4
gpt4 key购买 nike

我正在制作一个关于学校管理系统的项目,我有一个jframe来设置标记,在jframe内部我有一个连接到sql数据库的jtable,在jframe中我有一个包含6个不同主题的JCombobox,在我的数据库中我已经为主题创建了 6 个表,如果单击任何主题,则 jTable 将连接到数据库并将表更改为该主题,更改后如果您双击任何行并插入一个值,那么它将自动更新数据库中的表,但我的问题是,如果我从 jcombox 选择第一个选项(英语)并编辑值,那么它工作正常,但如果我选择任何其他选项,例如数学或科学,那么我尝试编辑表然后它编辑英语表,我在代码中评论了英语选项,看看会发生什么,我看到它只编辑第一个选项,如果你尝试更改主题并编辑,它会编辑组合框中的第一个主题,所以我该如何解决这个问题?请帮忙

代码:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.event.TableModelEvent;
import javax.swing.event.*;
import javax.swing.table.TableModel;
import net.proteanit.sql.DbUtils;

public class Marks implements TableModelListener {

JFrame frame;
JTable table;
JComboBox comboBox;
static Connection connection = null;
static Statement stmt = null;
static ResultSet rs;
String item = "";
String id = "";
String name = "";


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Marks window = new Marks();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Marks() throws SQLException {

connection = DriverManager.getConnection("jdbc:odbc:Project");

initialize();
table();
ComboItem();
stateChanged();
Combobox();
item = comboBox.getSelectedItem().toString();
System.out.println("Final Item: "+item);
}

public void table() {
try {
String a = comboBox.getSelectedItem().toString();
//a = a.substring(0, a.length() - 1);
String query = " Select * from " + a;
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
//item = comboBox.getSelectedItem().toString();

} catch (Exception ex) {
ex.printStackTrace();
}

}

public void stateChanged(){
itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
System.out.println("Item after stateChanged: " + item);
if (comboBox.getSelectedItem().equals("English")) {
try {
String query = "Select * from English";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "English";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("Math")) {
try {
String query = "Select * from Math";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Math";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("Science")) {
try {
String query = "Select * from Science;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Science";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("History")) {
try {
String query = "Select * from History;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "History";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("IT")) {
try {
String query = "Select * from IT;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "IT";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("Geography")) {
try {
String query = "Select * from Geography;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Geography";
} catch (Exception ex) {
ex.printStackTrace();

}

}
}
};
}

@Override
public void tableChanged(TableModelEvent e) {

try {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
Object roll = model.getValueAt(row, 0);
System.out.println("Subject Changed: " + item);
System.out.println("New Data: " + data.toString());
System.out.println("User Id: " + roll.toString());
System.out.println("Column name: " + columnName);

String query = "UPDATE " + item + " SET " + columnName + " = '"
+ data.toString() + "' WHERE userid='" + roll.toString() + "' ";

PreparedStatement pst;

pst = connection.prepareStatement(query);

pst.executeUpdate();
// pst.close();
} catch (SQLException ex) {
Logger.getLogger(Marks.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}

}
ItemListener itemListener;

public void ComboItem(){
comboBox.addItemListener(itemListener);
}

private void Combobox() {
comboBox.addItemListener(itemListener);

comboBox.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
System.out.println(item);

if (comboBox.getSelectedItem().equals("English")) {
try {
String query = "Select * from English";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Engish";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("Math")) {
try {
String query = "Select * from Math";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Math";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("Science")) {
try {
String query = "Select * from Science;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Science";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("History")) {
try {
String query = "Select * from History;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "History";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("IT")) {
try {
String query = "Select * from IT;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "IT";
} catch (Exception ex) {
ex.printStackTrace();
}

}

if (comboBox.getSelectedItem().equals("Geography")) {
try {
String query = "Select * from Geography;";
PreparedStatement pst = connection.prepareStatement(query);
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
table.getModel().addTableModelListener(new Marks());
item = "Geography";
} catch (Exception ex) {
ex.printStackTrace();
}

}

}
});
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.setBounds(100, 100, 600, 400);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setResizable(false);
frame.getContentPane().setLayout(null);

JLabel lblSetMarks = new JLabel("<html><u>Set Marks</u></html>");
lblSetMarks.setHorizontalAlignment(SwingConstants.CENTER);
lblSetMarks.setFont(new Font("Castellar", Font.BOLD, 25));
lblSetMarks.setBounds(218, 11, 159, 22);
frame.getContentPane().add(lblSetMarks);

comboBox = new JComboBox();
comboBox.setFont(new Font("Book Antiqua", Font.PLAIN, 15));
comboBox.setBounds(459, 47, 125, 22);
comboBox.addItem("English");
comboBox.addItem("Geography");
comboBox.addItem("History");
comboBox.addItem("IT");
comboBox.addItem("Math");
comboBox.addItem("Science");
frame.getContentPane().add(comboBox);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 87, 574, 259);
frame.getContentPane().add(scrollPane);

table = new JTable();

scrollPane.setViewportView(table);

JLabel lblClass = new JLabel("Date :");
lblClass.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblClass.setBounds(10, 51, 58, 14);
frame.getContentPane().add(lblClass);

JLabel lblSubject = new JLabel("Subject :");
lblSubject.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblSubject.setBounds(391, 49, 58, 18);
frame.getContentPane().add(lblSubject);
}
}

最佳答案

问题围绕着 table.getModel().addTableModelListener(new Marks()); 的使用,它正在创建一个 Marks 的新实例,您的主类,每次选择一个新项目

这意味着当调用 tableChanged 时,它不会处理您在原始实例中修改的 item 实例,而是处理某个始终设置的其他实例改为英语

相反,您应该尝试使用诸如 table.getModel().addTableModelListener(this); 之类的东西,但只能在构造函数中设置一次,否则它将被多次调用,这会降低系统的性能...

使用TableModelListener时要小心,当单元格更改、行更改、添加、删除或表模型的整个内容更改时,它将被调用。在尝试更新数据库之前,您应该尝试确定事件的实际类型,因为您最终可能会因错误的原因将数据更新到错误的表

您应该避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计初衷是与布局管理器一起工作,放弃这些管理器将导致无穷无尽的问题,您将花费越来越多的时间来尝试纠正这些问题。

这会严重阻止在 JComboBox 上使用 KeyListener (一般情况下我会严重阻止使用 KeyListener),它对于这种情况,这不是监听器的最佳选择,此外,您现在有两个 ItemListener 和一个 KeyListener 附加到组合框,它们都执行相同的操作...多次...

摆脱 ItemListener 并使用 ActionListener 代替,ItemListener 可能会被触发多次(一次用于取消选定的项目)一旦选择了新项目),坦率地说,您不在乎,您只想知道它何时更改。

看一下:

了解更多详情

关于java - 使用 Jcombobox 更改项目后编辑 jtable 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28103936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com