我有 jFrame2,它包含 4 列的 jTable(jTable 从包含 20 列的数据库表中获取数据) 我还有 jFrame1,我用它来填充数据库。 当我在 jTable 中选择行并单击 jButton 时,我想做的是,它必须打开 jframe1 以显示该行的所有数据。 我会用积分清除我想要的
*我想通过 jbutton 从 jframe2 打开 jframe1(这个任务已经完成,这是代码)
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jButton2){
jframe2 regFace =new jframe2();
regFace.setVisible(true);
}}
*一旦 jframe1 在 jframe2 中被 jbutton 打开,它必须在它的字段中显示 jframe2 中选定行的所有数据>>这一点意味着
........-jframe2中的Jbutton打开jfram1后执行的sql查询
.........-在 jtextfield 中显示通过我在上面一行中提到的查询从数据库中获取的数据(此任务已完成,这是代码但未完成)
try {
dbconnect = new myDbConnection();
ResultSet resultSet =null;
resultSet = dbconnect.excuteQuery("SELECT id, area,location, status1 FROM pledges where id='17'");
while (resultSet.next()){
id.setText(resultSet.getString(1));
area.setText(resultSet.getString(2));
location.setText(resultSet.getString(3));
status.setText(resultSet.getString(4));
// i = Long.parseLong(rs1.getString(1));
}
*简而言之,我想了解 jframe1,如果您通过 jframe2 打开,请执行查询并通过该查询填充文本字段 *这是图片会更清楚 here
最佳答案
听起来您遇到问题的部分是如何将表中的选定数据放入 jframe1 的字段中。
这在很大程度上取决于 JTable
中使用的 TableModel
。假设您刚刚使用了 DefaultTableModel
,您可以像这样获取选定的行数据:
@Override
public void actionPerformed(ActionEvent e) {
int viewRow = myJTable.getSelectedRow();
int modelRow = myJTable.convertRowIndexToModel(viewRow);
DefaultTableModel model = (DefaultTableModel) myJTable.getModel();
// You will get a compiler warning on the following line, but there's not much you can do about it beside suppress it
Vector<Object> rowVector = (Vector<Object>) model.getDataVector().get(modelRow);
jframe2 regFace =new jframe2();
regFace.setSelectedRow(rowVector);
regFace.setVisible(true);
}
并且您将在 jframe2 类中使用以下方法:
public void setSelectedRow(Vector<Object> row ) {
id.setText(row.get(0).toString());
area.setText(row.get(1).toString());
location.setText(row.get(2).toString());
status.setText(row.get(3).toString());
// continue for all columns
}
关于java - 在 jFrame 中显示来自 jTable 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5625192/