gpt4 book ai didi

java - 从另一个 jtable 中选择信息时将信息显示到 jtable

转载 作者:行者123 更新时间:2023-11-30 04:52:15 29 4
gpt4 key购买 nike

美好的一天!我在 Jtable 方面遇到了困难。我已经阅读和浏览了各种教程,但我不太明白它的要点。我的问题是,我必须从 jtable (jTable1) 中选择包含 (ClientID、LastName、FirstName) 的行。一旦我从表中单击/选择,该特定客户端的所有属性将显示在另一个 jtable (tableRealProperty) 上。两个 jtable 都有一个公共(public)列,即 ClientID。我的代码只允许我显示数据库中存在的所有客户端的所有属性。我只想显示所选客户端的属性。

这是我的代码

sql ="SELECT tblrealproperty.ClientID, tblrealproperty.Location, tblrealproperty.Area,"
+ "tblrealproperty.DateAcquired, tblrealproperty.Amount
FROM tblclientinfo Inner Join "
+ "tblrealproperty ON tblrealproperty.ClientID = tblclientinfo.ClientID
where tblClientInfo.ClientID = ?";

try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
tablePersonalProperty.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}

你能帮我解决这个问题吗?谢谢!

最佳答案

刚刚一头雾水,是不是意味着要选择 Java Swing JTable???

enter image description here

enter image description here

enter image description here

来自代码

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;

public class TableSelectionGood implements ListSelectionListener {

private JTable[] tables;
private boolean ignore = false;

public TableSelectionGood() {
Object[][] data1 = new Object[100][5];
Object[][] data2 = new Object[50][5];
//Object[][] data3 = new Object[50][5];
for (int i = 0; i < data1.length; i++) {
data1[i][0] = "Company # " + (i + 1);
for (int j = 1; j < data1[i].length; j++) {
data1[i][j] = "" + (i + 1) + ", " + j;
}
}
for (int i = 0; i < data2.length; i++) {
data2[i][0] = "Company # " + ((i * 2) + 1);
for (int j = 1; j < data2[i].length; j++) {
data2[i][j] = "" + ((i * 2) + 1) + ", " + j;
}
}
/*for (int i = 0; i < data3.length; i++) {
data3[i][0] = "Company # " + (i * 2);
for (int j = 1; j < data3[i].length; j++) {
data3[i][j] = "" + (i * 2) + ", " + j;
}
}*/
String[] headers = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
DefaultTableModel model1 = new DefaultTableModel(data1, headers);
DefaultTableModel model2 = new DefaultTableModel(data2, headers);
//DefaultTableModel model3 = new DefaultTableModel(data3, headers);
final JTable jTable1 = new JTable(model1);
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp1 = new JScrollPane();
sp1.setPreferredSize(new Dimension(600, 100));
sp1.setViewportView(jTable1);
final JTable jTable2 = new JTable(model2);
jTable2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp2 = new JScrollPane();
sp2.setPreferredSize(new Dimension(600, 100));
sp2.setViewportView(jTable2);
/*final JTable jTable3 = new JTable(model3);
jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final JScrollPane sp3 = new JScrollPane();
sp3.setPreferredSize(new Dimension(600, 100));
sp3.setViewportView(jTable3);
TableSelectionGood tableSelection = new TableSelectionGood(jTable1, jTable2, jTable3);*/
TableSelectionGood tableSelection = new TableSelectionGood(jTable1, jTable2);
JPanel panel1 = new JPanel();
//panel1.setLayout(new GridLayout(3, 0, 10, 10));
panel1.setLayout(new GridLayout(2, 0, 10, 10));
panel1.add(sp1);
panel1.add(sp2);
//panel1.add(sp3);
JFrame frame = new JFrame("tableSelection");
frame.add(panel1);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public TableSelectionGood(JTable... tables) {
for (JTable table : tables) {
table.getSelectionModel().addListSelectionListener(this);
}
this.tables = tables;
}

private JTable getTable(Object model) {
for (JTable table : tables) {
if (table.getSelectionModel() == model) {
return table;
}
}
return null;
}

private void changeSelection(JTable table, String rowKey) {
int col = table.convertColumnIndexToView(0);
for (int row = table.getRowCount(); --row >= 0;) {
if (rowKey.equals(table.getValueAt(row, col))) {
table.changeSelection(row, col, false, false);
return;
}
}
table.clearSelection();
}

@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() || ignore) {
return;
}
ignore = true;
try {
JTable table = getTable(e.getSource());
int row = table.getSelectedRow();
String rowKey = table.getValueAt(row, table.convertColumnIndexToView(0)).toString();
for (JTable t : tables) {
if (t == table) {
continue;
}
changeSelection(t, rowKey);
JViewport viewport = (JViewport) t.getParent();
Rectangle rect = t.getCellRect(t.getSelectedRow(), 0, true);
Rectangle r2 = viewport.getVisibleRect();
t.scrollRectToVisible(new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
System.out.println(new Rectangle(viewport.getExtentSize()).contains(rect));
}
} finally {
ignore = false;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
TableSelectionGood tableSelection = new TableSelectionGood();
}
});
}
}

关于java - 从另一个 jtable 中选择信息时将信息显示到 jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9590376/

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