以下代码在 Mac OS X 10.6.8 上不起作用,但在我测试的所有其他版本(Mac OS X Lion 和 Windows 7)上都起作用。显而易见的解释是 Apple 的 java.lang. 表格数据在 Mac 上无法正确显示(如果有的话)。这是代码
package com.dramble.dance;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
public class TableDemo extends JPanel {
public TableDemo() {
super(new GridLayout(1,0));
TableModel tableModel = new TableModel();
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
for(int i = 0 ; i <= 1900 ; i ++) {
DataRow row = new DataRow(i,"Firstname"+i,"Lastname"+i);
tableModel.appendRow(row);
tableModel.fireTableRowsInserted(i, i);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private class DataRow {
private String fname, lname;
private Integer id;
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public Integer getId() {
return id;
}
public DataRow(Integer id, String fname, String lname) {
super();
this.fname = fname;
this.lname = lname;
this.id = id;
}
}
private class TableModel extends AbstractTableModel {
List<DataRow> data;
private String[] columnNames = {"ID #","First Name","Last Name"};
private Class[] columnTypes = {int.class, String.class, String.class};
public TableModel() {
this.data = new ArrayList<DataRow>();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public int getRowCount() {
return this.data.size();
}
@Override
public Object getValueAt(int row, int col) {
DataRow dataRow = this.data.get(row);
Object returnValue = null;
switch (col) {
case 0:
returnValue = dataRow.getId();
break;
case 1:
returnValue = dataRow.getFname();
break;
case 2:
returnValue = dataRow.getLname();
break;
}
return returnValue;
}
public void appendRow(DataRow row) {
this.data.add(row);
}
@Override
public boolean isCellEditable(int rowIndex, int vColIndex) {
return false;
}
@Override
public Class getColumnClass(int col) {
return columnTypes[col];
}
}
}
这是在 Windows 中展示的预期行为的图像
这是 Mac OS X 10.6.8 上的相同代码