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

时间:2015-05-05 21:54:40

标签: java swing jtable tablemodel abstracttablemodel

我有一个活动列表,我想要显示内容。 “name,isdone,sdate,endate,description” 我一直在读书,但没有说明为什么我的桌子甚至不会画画

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”仅用于尝试使用默认模型绘制表格,它的工作方式就像魅力“活动”一样

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


}

1 个答案:

答案 0 :(得分:2)

如果要在JScrollPane的视图中显示组件,则应通过视口完成。这可以通过将组件传递给JScrollPane构造函数或调用setViewportView来完成:

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