1

The following code does not work on Mac OS X 10.6.8 but does on everything else I test, Mac OS X Lion and Windows 7. The obvious explanation would be differences in Apple's java. The table data does not show properly, if at all, on Mac. Here is the code

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];
        }
    }
}

Here is an image of the expected behavior as exhibited in Windows Working table on Windows

Here is the same code on Mac OS X 10.6.8 Broken on Mac OS X

tdramble
  • 101
  • 1
  • 12

2 Answers2

2

The problem lies in the AbstractTableModel, which came as a surprise to me for some reason, I figured it would be EDT and thread related for sure. The problem is in my array for the columnTypes and using int.class. Changing this to Integer.class fixed my issue.

tdramble
  • 101
  • 1
  • 12
  • +1 Interesting: The Mac UI delegate assumes a renderer for `int.class`, and `JTable` throws NPE on the first cell. – trashgod Jan 25 '13 at 19:41
1

Absent a complete example, it's hard to guess. What you have looks OK, but two thing stand out:

  • I'm wary of frame.setContentPane(newContentPane), which replaces the frame's content pane with your TableDemo. As TableDemo is a JPanel, it inherits a UI delegate that varies by platform. Instead, consider frame.add(newContentPane), which forwards to the content pane.

  • Your appendRowData() method in DanceTableModel should fireTableRowsInserted(), so callers can't forget to do so.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • For this example code which recreates my issues, I copied a lot of code from a table demo from java, the replacing of the content frame is how they did it, I get the same issues on Mac no matter how I do it. – tdramble Jan 19 '13 at 13:43
  • I decided to call `fireTableRowsInserted()` outside of `appendRowData()` because I will want to populate that table from a `SwingWorker` (since it will be so large and the data will be coming from the internet, it could take a while) and `fireTableRowsInserted()` needs to be called from the EDT correct? So I will call it from the `process` method of the `SwingWorker` – tdramble Jan 19 '13 at 13:47
  • I am unable to reproduce the platform disparity you describe. Yes, `process()` runs on the EDT, where you _want_ the effect of firing the event. – trashgod Jan 19 '13 at 19:00
  • I have the issue on Mac OS X 10.6.8 with java version 1.6.0_37. When I test on a Lion machine, it works fine – tdramble Jan 25 '13 at 14:21
  • 1
    Try this [example](http://stackoverflow.com/a/7054627/230513). Edit your question to include an [sscce](http://sscce.org/) that exhibits the problem on 10.6.8/1.6.0_37, and add a [screenshot](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post). – trashgod Jan 25 '13 at 14:44
  • I see what you did there, amazing how making those sscce's leads to finding and solving your own issues, thanks a ton. – tdramble Jan 25 '13 at 19:29