JTable中不可忽略的组件(TableCellRenderer)

时间:2014-10-28 15:27:16

标签: java swing jtable tablecellrenderer

我为JTable使用了一个包含JTextfield和JCombobox的自定义单元格渲染但是我无法点击它们的问题就像被禁用了一样!
这是我的代码:

public class OvertimeAndProjectRender extends JPanel implements TableCellRenderer{

    /**
     * 
     */
    private static final long serialVersionUID = 2079139315339181747L;
    Project p = new Project();
    private List<Project> listProject;
    JComboBox<ComboItem> projectComboBox = new JComboBox<ComboItem>();
    public  OvertimeAndProjectRender(int idChef) {
        listProject = p.getListProjectsForSelectedChef(idChef);
        fillCombobox();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
        this.setLayout(new BorderLayout(0, 0));
        JTextField t = new JTextField();
        this.add(t, BorderLayout.NORTH);
        this.add(projectComboBox, BorderLayout.SOUTH);
        return this;
    }

    private void fillCombobox() {
        int nbIde = listProject.size();
        for(int i=0; i<nbIde; i++){     
            String key = Integer.toString(listProject.get(i).getId());
            String value = listProject.get(i).getDesignation();
            projectComboBox.addItem(new ComboItem(key, value));
        }
    }


}

这就是我调用自定义cellRender的方式:

tablePointage.getColumnModel().getColumn(2).setCellRenderer(new OvertimeAndProjectRender(idChef));

这是JTable的图片:

JTable with cellRender http://img11.hostingpics.net/pics/695045qsdqsd.jpg

0 个答案:

没有答案