gpt4 book ai didi

java - 如何在 java eclipse 中刷新 JTable 后从 JTABLE 获取值并将其传递给文本字段

转载 作者:行者123 更新时间:2023-11-29 03:26:44 25 4
gpt4 key购买 nike

在此输入代码没有错误。当表格未刷新时它工作正常但是当我搜索一个项目并且表格刷新时,当我单击表格中的列时它不会传递到文本字段。请帮我。谢谢。 :)

reloadData();
model = new DefaultTableModel(data,columnNames);
table.setModel(model);
TableColumn column;
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setMaxWidth(1000);
}

//get value from JTABLE and pass it to the text fields
table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 1)
{
try{

JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
String table_click =(target.getModel().getValueAt(row, 0).toString());
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionURL = "jdbc:sqlserver://;Database='';user='';password='';";
Connection con = DriverManager.getConnection(connectionURL);
ResultSet rs = null;
PreparedStatement pst =null;
// Statement stmt=con.createStatement();
String sql ="select * from inventory where Id='"+table_click+"'";
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
textField.setText(rs.getString("Name"));
textField_1.setText(rs.getString("Category"));
textField_2.setText(rs.getString("Brand"));
textField_3.setText(rs.getString("Price"));
textField_4.setText(rs.getString("ExDate"));
textField_5.setText(rs.getString("Tags"));
textField_6.setText(rs.getString("Barcode"));
textField_8.setText(rs.getString("Id"));
}
}catch(Exception e1){
e1.printStackTrace();
}
}
}
});

//SEARCH
btnNewButton_3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value1=textField_7.getText();
try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionURL = "jdbc:sqlserver://;Database='';user='';password='';";
Connection con = DriverManager.getConnection(connectionURL);
PreparedStatement pst =null;
//Statement stmt=con.createStatement();

//PreparedStatement pst=con.prepareStatement("select * from inventory where Id=? or Name=? ");
String sql ="select * from inventory where CAST(Id as NVARCHAR(50))=? or Name=? ";
pst=con.prepareStatement(sql);
pst.setString(1, textField_7.getText());
pst.setString(2, textField_7.getText());


ResultSet rs = pst.executeQuery();
if(value1.equals("")){
reloadData();
model.fireTableDataChanged();
JOptionPane.showMessageDialog(null,"Please Enter a Name or Id of the Item","ERROR",JOptionPane.ERROR_MESSAGE);

}else{


table.setModel(DbUtils.resultSetToTableModel(rs));
}

}


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

}


});

}

//Table with SQL
private void reloadData() {

columnNames.clear();
data.clear();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionURL = "jdbc:sqlserver://;Database='';user='';password='';";
Connection con = DriverManager.getConnection(connectionURL);
Statement stmt=con.createStatement();
ResultSet rs = stmt.executeQuery("select * from inventory");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}

rs.close();
stmt.close();
} catch (Exception e){
e.printStackTrace();
}
table = new JTable(data,columnNames);
scrollPane.setViewportView(table);
}

}

最佳答案

您正在丢失 MouseListener,因为您正在创建一个新的 JTable。

在您的reloadData() 方法中。不要创建新的 JTable。而是创建一个新的 TableModel 然后使用:

table.setModel( theNewModel );

此外,

model.fireTableDataChanged();

切勿直接调用 fireXXX() 方法。这是调用适当方法的 TableModel 的工作。

关于java - 如何在 java eclipse 中刷新 JTable 后从 JTABLE 获取值并将其传递给文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673663/

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