gpt4 book ai didi

java - 如何将一行从 JTable 复制到另一个 JTable

转载 作者:行者123 更新时间:2023-11-29 13:20:26 27 4
gpt4 key购买 nike

我在我的项目上工作,我需要将一行从 JTable 复制到另一个 JTable,第二个 JTable 应该只是单行表。我为第一个 JTable 创建了 mouselistener,双击它应该复制行并将其插入另一个 JTable 但它不能正常工作,有什么解决办法吗?我从第一个表中的数据库中获取数据。代码:

public void cputable() {
try {
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test","postgres","postgres");
stat = conn.createStatement();
result = stat.executeQuery("SELECT name,bus_speed,socket,cores,chipset,price*1.3 FROM CPU");

cputable.setModel(DbUtils.resultSetToTableModel(result));
result.close();
stat.close();
}
catch (Exception e){
e.printStackTrace();
}
}

这是我尝试复制行的代码:

 cputable.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
JTable cputable =(JTable) me.getSource();
int row = cputable.getSelectedRow();
int col = cputable.getColumnCount();

if (me.getClickCount() == 2) {
cputablebottom.repaint();
for(int i = 0; i < col; i++) {
DefaultTableModel model = (DefaultTableModel) cputable.getModel();
List<String>list = new ArrayList<String>();
list.add( cputable.getValueAt(row, i).toString());
model.addRow(list.toArray());
cputablebottom.setModel(model);
}

前后结果:

编辑:

我重新编写了一些代码,现在它复制了整个列表,而不是只复制了一行。

cputable.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
JTable cputable =(JTable) me.getSource();
int row = cputable.getSelectedRow();
int col = cputable.getColumnCount();

if (me.getClickCount() == 2) {
cputablebottom.repaint();
DefaultTableModel model1 = (DefaultTableModel) cputable.getModel();
List<String>list = new ArrayList<String>();

model1.addRow(list.toArray());
for(int i = 0; i < col; i++) {
list.add( cputable.getValueAt(row, i).toString());
cputablebottom.setModel(model1);
System.out.println(model1);
System.out.println(list);
}
cputablebottom.setModel(model1);

最佳答案

我没试过,但是...

DefaultTableModel model1 = (DefaultTableModel) cputable.getModel();
Vector data = model1.getDataVector();
Object rowObj = data.get(row);

Vector newData = new Vector(1);
newData.add(rowObj);

DefaultTableModel model2 = (DefaultTableModel) cputablebottom.getModel();
model2.setRowCount(0);
model2.addRow(newData);

这应该保留第二个表的列信息。

或者,您可以创建一个新的DefaultTableModel,但您每次都必须重新配置列信息

我建议您阅读 JavaDocs for DefaultTableModel

测试示例

我不经常使用 DefaultTableModel,我更喜欢在每一行中放置一个实际的对象,然后我可以定义它的显示方式,这通常会使它更简单,但是,如果 DefaultTableModel 就是你所拥有的...

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private JTable top;
private JTable bottom;

public TestPane() {
setLayout(new GridLayout(2, 0));

String[][] rowData = new String[10][10];
for (int row = 0; row < 10; row++) {
String[] data = new String[10];
for (int col = 0; col < 10; col++) {
data[col] = row + "x" + col;
}
rowData[row] = data;
}
String[] names = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
DefaultTableModel model = new DefaultTableModel(rowData, names);
top = new JTable(model);
add(new JScrollPane(top));

DefaultTableModel emptyModel = new DefaultTableModel(new String[10][10], names);
bottom = new JTable(emptyModel);
add(new JScrollPane(bottom));

top.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
int row = top.rowAtPoint(e.getPoint());
if (row > -1) {
DefaultTableModel topModel = ((DefaultTableModel)top.getModel());
DefaultTableModel bottomModel = ((DefaultTableModel)bottom.getModel());
bottomModel.setRowCount(1);
for (int col = 0; col < topModel.getColumnCount(); col++) {
bottomModel.setValueAt(topModel.getValueAt(row, col), 0, col);
}
}
}
}
});
}

}

}

关于java - 如何将一行从 JTable 复制到另一个 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42773776/

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