gpt4 book ai didi

java - 如何更新 JTable?

转载 作者:行者123 更新时间:2023-12-01 10:16:10 24 4
gpt4 key购买 nike

我正在为我的高中编程课创建基本上是 excel 的东西。我希望用户能够选择一个单元格,以 01/01/00 的形式输入日期,按 Enter 键,然后将单元格更新为显示 2000 年 1 月 1 日。

客户是

package client;

import javax.swing.JFrame;
import spreadsheet.GUI;

public class Program {
public static void main(String[] args) {
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(1000, 618);
gui.setVisible(true);
}

}

JTable 类是

package spreadsheet;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;

import cell.*;

public class GUI extends JFrame implements TableModelListener {

JTable mainTable;
TableModel dtm;

public static final int ASCII_SHIFT = 64;

public GUI() {

super("TextExcel");
setLayout(new BorderLayout());

String[] letter = new String[7];
for (int i = 0; i < letter.length; i++) {
byte[] character = {(byte) (i + ASCII_SHIFT + 1)};
letter[i] = new String(character);
}

Cell[][] sheet = new Cell[10][8];

mainTable = new JTable(sheet, letter);

JTableHeader header = mainTable.getTableHeader();
header.setDefaultRenderer(new HeaderRenderer(mainTable));

dtm = mainTable.getModel();

for (int i = 0; i < sheet.length; i++) {
mainTable.setRowHeight(i, 35);
}

JScrollPane scrollPane = new JScrollPane(mainTable);
JTable rowTable = new RowNumberTable(mainTable);
scrollPane.setRowHeaderView(rowTable);

scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowTable.getTableHeader());
mainTable.setFillsViewportHeight(true);

add(scrollPane);
}

private static class HeaderRenderer implements TableCellRenderer {

DefaultTableCellRenderer renderer;

public HeaderRenderer(JTable table) {
renderer = (DefaultTableCellRenderer)
table.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
}

@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
return renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
}
}

@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
}

}

Cell 类是

package cell;

public abstract class Cell {

public abstract String toString();

}

DateCell 类是

package cell;

public class DateCell extends Cell{

private int month, day, year;
public static final String[] MONTH = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};

public DateCell(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}

public DateCell(String s) {
int firstSlash = s.indexOf('/');
int secondSlash = s.lastIndexOf('/');
month = Integer.parseInt(s.substring(0, firstSlash));
day = Integer.parseInt(s.substring(firstSlash + 1, secondSlash));
if (s.substring(secondSlash + 1).length() < 4) {
year = Integer.parseInt(s.substring(secondSlash + 1)) + 2000;
} else {
year = Integer.parseInt(s.substring(secondSlash + 1));
}
}

public String toString() {
return MONTH[month-1] + " " + day + ", " + year;
}
}

我最终会有其他 Cell 子类,但目前我只使用 DateCell。

我想我的主要问题是如何处理可编辑单元格中的用户输入,修改对象[][](或我的情况下的单元格[][]),并刷新表格以显示数据改变?

我已经查看了有关 JTables 的官方 java 教程以及一些有关更新的堆栈溢出帖子,但我仍然不知道如何将其放入我的代码中。

最佳答案

将您的表模型放入默认表模型引用中。像这样。

DefaultTableModel dtm = (Default Table Model)jtable.getModel();

然后就很容易在表中更新了。

关于java - 如何更新 JTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35882113/

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