JScrollPane中的JTable在Mac OS X中无法正常显示

时间:2013-01-18 12:30:38

标签: java macos swing jtable abstracttablemodel

以下代码在Mac OS X 10.6.8上不起作用,但在我测试的所有其他代码,Mac OS X Lion和Windows 7上都有效。显而易见的解释是Apple的java中存在差异。在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中展示的预期行为的图像 Working table on Windows

以下是Mac OS X 10.6.8上的相同代码 Broken on Mac OS X

2 个答案:

答案 0 :(得分:2)

问题在于AbstractTableModel,由于某种原因让我感到惊讶,我认为它肯定会与EDT和线程相关。问题出在columnTypes的数组中,并使用int.class。将此更改为Integer.class解决了我的问题。

答案 1 :(得分:1)

没有一个完整的例子,很难猜到。你看到的东西看起来还不错,但有两点是突出的:

  • 我对frame.setContentPane(newContentPane)保持警惕,用您的TableDemo替换框架的内容窗格。由于TableDemoJPanel,因此它会继承因平台而异的UI委托。相反,请考虑frame.add(newContentPane)转发到内容窗格。

  • appendRowData()中的DanceTableModel方法应为fireTableRowsInserted(),因此来电者不会忘记这样做。