gpt4 book ai didi

java - 将文本写入 AbstractTableModel 单元格

转载 作者:行者123 更新时间:2023-12-01 14:07:48 25 4
gpt4 key购买 nike

我已经实现了一个 AbstractTableModel 对象,它是一种时间表。我想将当时将通过使用方法教授的主题写入单元格(因此我不想将行声明为已编译)。我能怎么做?这是我的 AbstractTableModel 对象。

private class MyTableModel extends AbstractTableModel {

private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

private String[][] data = {{"8:30 - 9:30","","","","","",""},
{"9:30 - 10:30","","","","","",""},
{"10:30 - 11:30","","","","","",""},
{"11:30 - 12:30","","","","","",""},
{"12:30 - 13:30","","","","","",""},
{"13:30 - 14:30","","","","","",""},
{"14:30 - 15:30","","","","","",""},
{"15:30 - 16:30","","","","","",""},
{"16:30 - 17:30","","","","","",""}};

public int getColumnCount() {
return columns.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columns[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

}

最佳答案

您必须覆盖 setValueAt方法。您还需要覆盖 isCellEditable方法(如果您希望表格编辑相关单元格)

看看How to use tables了解更多详情

已更新示例

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;

public class TestTable20 {

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

public TestTable20() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JTable table = new JTable(new MyTableModel());

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class MyTableModel extends AbstractTableModel {

private String[] columns = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

private String[][] data = {{"8:30 - 9:30", "", "", "", "", "", ""},
{"9:30 - 10:30", "", "", "", "", "", ""},
{"10:30 - 11:30", "", "", "", "", "", ""},
{"11:30 - 12:30", "", "", "", "", "", ""},
{"12:30 - 13:30", "", "", "", "", "", ""},
{"13:30 - 14:30", "", "", "", "", "", ""},
{"14:30 - 15:30", "", "", "", "", "", ""},
{"15:30 - 16:30", "", "", "", "", "", ""},
{"16:30 - 17:30", "", "", "", "", "", ""}};

@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex > 0;
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
String[] row = data[rowIndex];
row[columnIndex] = (String)aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}

@Override
public int getColumnCount() {
return columns.length;
}

@Override
public int getRowCount() {
return data.length;
}

@Override
public String getColumnName(int col) {
return columns[col];
}

@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}

}
}

关于java - 将文本写入 AbstractTableModel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758961/

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