当te单元格具有JeditorPane时,JTable.getValueAt(i,0)为null

时间:2013-07-12 17:55:47

标签: java swing jtable jeditorpane

我在表格中使用JEditorPaneJscrollPane作为单元格渲染器(因此我可以轻松控制颜色,字体,大小和链接)。问题是我无法通过以下方式获得单元格的内容:

for (int i = 0; i < detalle_jTable.getRowCount(); i++) {
    System.out.println((String) JTable.getValueAt(i, 3));
}

问题是行总是返回null。 这是我的tableCellrendertablecellEditor代码:

class celdaScrollCellRender extends JTextPane implements TableCellRenderer {

    JScrollPane scroll2 ;
    JEditorPane areaDeTexto;

    public celdaScrollCellRender() {
        areaDeTexto = new JEditorPane();
        scroll2 = new JScrollPane(areaDeTexto);     
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, 
    boolean isSelected, boolean hasFocus, int row, int column) {  
        return scroll2;   
    }
}//de la clase

///////////////////////////////**********************************/////
class celdaScrollEditor extends JTextPane implements TableCellEditor {  
    JEditorPane areaDeTexto;
    JScrollPane scroll2;

    //constructor
    public celdaScrollEditor() {
        areaDeTexto = new JEditorPane();
        scroll2 = new JScrollPane(areaDeTexto);
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row, int column) {
        areaDeTexto.setFont(new Font("Arial", Font.PLAIN, 12));
        areaDeTexto.setSize(560, 60);        
        areaDeTexto.setContentType("text/html");
        areaDeTexto.setText((String) value);        

        return scroll2; 
    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {  
       //solo muestra el contenido al hacer un clic por primera vez
    }

    @Override
    public void cancelCellEditing() {
    }

    @Override
    public Object getCellEditorValue() {  
        return areaDeTexto.getText();        
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {
    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean stopCellEditing() {
        return true;
    }   
}  //de la clase

在tablemodel中:

detalle_jTable.getColumnModel().getColumn(3).setCellRenderer(new celdaScrollCellRender());
detalle_jTable.getColumnModel().getColumn(3).setCellEditor(new celdaScrollEditor());

0 个答案:

没有答案