我刚刚开始使用 Java Swing 进行编程。我对众多 Java Swing 类中的每一个都不是很熟悉,并且在创建表方面遇到了障碍。
我正在尝试弄清楚如何将表添加到我正在制作的 Java Swing 程序中。我查看了 docs.oracle.com 来尝试找出答案,但当我尝试访问表的值时出现错误。我将包含我的代码来帮助显示我当前正在尝试执行的操作以及错误所在。
这是我制作表格和表格模型的代码(我认为问题是我的表格模型无法正常工作):
/*Create the Table Entry*/
columnNames = new String[listoffields.size()+1];
columnNames[0] = "Record Number";
for(int a = 1; a < columnNames.length;a++){
columnNames[a] = listoffields.get(a-1).getTitle();
}
int count = 1;
data = new Object[numrecords][listoffields.size()+1];
for(int a = 0; a < numrecords;a++){
for(int b = 0; b < listoffields.size()+1;b++){
if(b == 0){
data[a][b] = count;
count++;
}
else{
data[a][b] = "";
}
}
}
/* create the table */
JTable table = new JTable(data,columnNames);
table.setCellSelectionEnabled(true);
table.setModel(new AbstractTableModel() {
public String getColumnName(int col) {
return columnNames[col].toString();
}
public int getRowCount() { return data.length; }
public int getColumnCount() { return columnNames.length; }
public Object getValueAt(int row, int col) {
return data[row][col];
}
public boolean isCellEditable(int row, int col) {
return (col >= 1);
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
});
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
/* addthe table to the JTable tableentry*/
tabelentry.setLayout(new BoxLayout(ProjectFrame.tabelentry, BoxLayout.Y_AXIS));
tabelentry.add(scrollPane);
tabelentry.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
显示表格并编辑每个单元格的空白值后,单击提交单元格的提交按钮。但是,遇到了以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at Client.GUIComponents.ProjectFrame$1.submit(NameOfFile.java:LineNumber)
尝试提交时遇到错误如下:
public void submit(){
String recordvalues = ""; //contains a String representation of what I want to submit
for(int a = 0; a < numberoffields;a++){
for(int b = 0; b < numberofrecords;b++){
if(a != 0){ //I want to skip the first column
recordvalues = recordvalues + (String) tabelentry.getModel().getValueAt(b, a) + ","; //The error is at this line in the code
}
}
}
}
我提供了我的代码来举例说明我当前遇到的问题以及我当前尝试过的方法。为了概括我的问题,我想知道如何正确编写表格模型类,以便我可以访问表格中每个单元格的值。任何帮助都会有所帮助。非常感谢!
最佳答案
我的第一 react 是您正在创建两个不同的表格模型。
JTable table = new JTable(data,columnNames);
table.setCellSelectionEnabled(true);
table.setModel(new AbstractTableModel() {...
new JTable(data,columnNames)
实际上是根据您提供的信息创建它自己的 DefaultTableModel
。尝试删除 table.setModel(new AbstractTableModel() {...
代码
另一个相关的问题是,我不知道如何声明 data
和 columnNames
。
通读一下How to Use Tables了解更多详情
已更新
正如 Hovercraft 所指出的,另一个问题是您将一个表添加到另一个表,然后访问错误的模型。
表格创建应该看起来更像......
tableEntry = new JTable(data,columnNames);
JScrollPane scrollPane = new JScrollPane(tableEntry );
tableEntry .setFillsViewportHeight(true);
// Don't forget to add the scroll pane to you view !!
那么你的提交方法应该看起来更像......
public void submit(){
String recordvalues = ""; //contains a String representation of what I want to submit
TableModel model = tableEntry.getModel();
for(int a = 0; a < model.getColumnCount();a++){
for(int b = 0; b < model.getRowCount();b++){
if(a != 0){ //I want to skip the first column
recordvalues = recordvalues + (String) model.getValueAt(b, a) + ","; //The error is at this line in the code
}
}
}
}
关于Java Swing JTable : cannot access the values in the table due to incorrect usage/creation of table model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13733572/