import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Main extends JFrame {
public Main() {
final AbstractTableModel model = new MyModel();
final JTable table = new JTable(model);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
model.setValueAt(new Integer(1), 0, 0);
JButton button = new JButton(“Increment selected cell”);
getContentPane().add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int column = table.convertColumnIndexToModel(table.getSelectedColumn());
int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
model.setValueAt(new Integer(currentValue + 1), row, column);
}
});
pack();
}
public static void main(String arg[]) {
Main ex2 = new Main();
ex2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex2.setVisible(true);
}
}
class MyModel extends AbstractTableModel {
private int[][] table = { new int[3], new int[3], new int[3] };
public MyModel() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
table[i][j] = i * j;
}
}
}
public int getColumnCount() {
return table.length;
}
public int getRowCount() {
return table[0].length;
}
public Object getValueAt(int r, int c) {
return new Integer(table[r][c]);
}
public void setValueAt(Object obj, int r, int c) {
table[r][c] = ((Integer) obj).intValue();
fireTableCellUpdated(r, c);
}
}
今天的文章java swing table刷新_Java Swing – 如何更改从AbstractTableModel创建的自定义模型中的数据并刷新JTable…分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31008.html