gpt4 book ai didi

java - 刷新框架上的表行(不是行的值)?

转载 作者:行者123 更新时间:2023-12-01 17:15:53 27 4
gpt4 key购买 nike

抱歉,我太业余了,我在框架上创建了一个“按钮”,这样当我们第一次单击该按钮时,就会在框架上插入一行。但我想要的是,当我第二次单击按钮时,行被刷新(不是创建新行!),我知道 '.addRow(new Object[] { "", "", ""}) ;'原因是,因为每次单击按钮时都会创建对象,所以我怎么可能修改代码呢?感谢您的关注。

*我弱小的大脑说,如果我可以清空“模型”对象(或销毁该对象,类似的东西),那么我就能找到解决方案,但是我怎么可能清空/销毁该“模型”对象呢?

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

public class MyTable extends JFrame {

private JTable table;
private DefaultTableModel model;
private JScrollPane scroll;
private JButton createRow;
private String headers[] = {"a", "b", "c"};

public MyTable() {
createRow = new JButton("Create Row");
model = new DefaultTableModel();
model.setColumnIdentifiers(headers);
table = new JTable();
table.setModel(model);
scroll = new JScrollPane(table);
add(createRow, java.awt.BorderLayout.NORTH);
add(scroll, java.awt.BorderLayout.CENTER);
createRowOn();
}

// while button is pressed then create A ROW
public void createRowOn() {
createRow.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == createRow) {
model.addRow(new Object[]{"", "", ""});
}
}
});
}

public static void main(String args[]) {
MyTable t = new MyTable();
t.setVisible(true);
t.setSize(300, 300);
}
}

最佳答案

Sorry for being so amateur

I created a 'button' on frame so that when we click the button for the first time, A ROW will be inserted on frame. But all i want that when i click the button on THE SECOND TIME , the ROW IS REFRESHED (Not creating a new row !) , i suspect 'new Object[] { "", "", ""}' is the cause , because the object is created everytime i click the button, so how could i possibly modify the code ?

  • 如果您想的话,问题不清楚(没有任何内容添加到 JTable 中):

    1. 添加新行(model.addRow)

    2. 删除所有行(setDataVector/setRowCount)

    3. 在 JTables View 中重置值 (setValueAt)

    但是Oracle教程中已经描述了所有内容,其余的在DefaultTableModel

关于java - 刷新框架上的表行(不是行的值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22196566/

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