JTable 的自定义 TableModel 和渲染器不会显示任何内容

Custom TableModel and render for JTable wont display anything

我有一个要显示内容的活动列表。 "name, isdone, sdate, endate, description" 我一直在阅读,但不知道为什么我的 table 甚至不会画画

https://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableCellRenderer.html http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

变量 "test" 仅用于尝试使用默认模型绘制 table,它的效果很好 "activities" 不要

public class ActivityToDoScreen implements IScreen {
    private int width=1200;
    private int height=800;
    private JPanel activityToDo;
    private IUser loggedinUser;

    @Override
    public void initializePanel(ProjectControlAppGui gui, Container pane) {
        loggedinUser = gui.loggedInUser();
        activityToDo = new JPanel();
        activityToDo.setBackground(Color.GRAY);
        activityToDo.setPreferredSize(new Dimension(width, height));
        Object[][] bah = {{new String("hej"), new Integer(5)},{new String("ss"),new Integer(7)}};
        String[]  buh ={"hej","med dig"};
        JTable test = new JTable(bah,buh);
        test.setDefaultRenderer(Object.class, new ActivityTableRender());
        activityToDo.add(test,BorderLayout.NORTH);
        ActivityTableModel model = new ActivityTableModel();
        try {
            model.Initialize(loggedinUser.getActivities());
        } catch (NotAllowedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JTable activities = new JTable(model);
        activities.setSize(400, 400);
        activities.setDefaultRenderer(Object.class, new ActivityTableRender());

        JScrollPane scroll = new JScrollPane();
        scroll.add(activities);
        activityToDo.add(scroll,BorderLayout.CENTER);


        // TODO Auto-generated method stub

    }
    @Override
    public JPanel getContentPane() {
        // TODO Auto-generated method stub
        return activityToDo;
    }

}

public class ActivityTableModel extends AbstractTableModel implements ITableModel<IActivity>, TableModelListener {
     private List<IActivity> projectData;
     private final String[] columnNames = {"Name","Done","Start date", "End date","Description"};
     private final Class[] columnClasses = {Object.class, Boolean.class, Object.class, Object.class,Object.class};



       public boolean isCellEditable(int row, int column) {
          return false;
       }


    @Override
    public void tableChanged(TableModelEvent e) {
        int row = e.getFirstRow();
        int column = e.getColumn();
        TableModel model = (TableModel)e.getSource();
        setValueAt(model.getValueAt(row, column),row,column);
    }
       @Override
        public Object getValueAt(int row, int col) {
            IActivity activity= projectData.get(row);
            Object value = new Object();
            switch(col){
            case 0:
                value = activity.getName();
                break;
            case 1:
                value = activity.isDone();
                break;
            case 2:
                value = activity.getStartDate();
                break;
            case 3:
                value = activity.getEndDate();
                break;
            case 4:
                value = activity.getDescription();
                break;
            default:
                break;



            }
        return value;
        }
       @Override
        public void setValueAt(Object value,int row, int col){
            IActivity activity = projectData.get(row);
            switch(col){
            case 0:
                activity.setName((String)value);
                break;
            case 1:
                activity.setIsDone((boolean)value);
                break;
            case 2:
                activity.setStartDate((DateTime)value);
                break;
            case 3:
                activity.setEndDate((DateTime)value);
                break;
            case 4:
                activity.setDescription((String)value);
                break;
            default:
                break;
            }

            fireTableCellUpdated(row, col);
        }
    @Override
       public Class getColumnClass(int c) {
            return columnClasses[c];
        }

    @Override
    public int getColumnCount() {
    return columnNames.length;
    }   
    @Override
    public int getRowCount() {
        if(projectData== null) return 0;
    return projectData.size();
    }
    public final String[] getColumnNames(){
        return columnNames;
    }

    @Override
    public void Initialize(List<IActivity> list) {

        projectData=new ArrayList<IActivity>();
        for(IActivity activity: list){
            projectData.add(activity);
            fireTableRowsInserted(projectData.size()-1, projectData.size()-1);
        }
    }


}

当你想在 JScrollPane 的视图中显示一个组件时,应该通过视口来完成。这可以通过将组件传递给 JScrollPane 构造函数或调用 setViewportView:

来完成
JScrollPane scroll = new JScrollPane(activities);
//or
scroll.setViewportView(activities);