Qualtagh/JBroTable

Table body editable?

Closed this issue · 2 comments

Hello,

Can the Table Body' cells be edited? Like the row color, cell alignment,... like Table Header?

Hi,

Can the Table Body' cells be edited?

override isCellEditable method to allow editing:

JBroTable table = new JBroTable( data ) {
    @Override
    public boolean isCellEditable( int row, int column ) {
        return true;
    }
};

Do you have spanned cells in table body?
By default, each cell inside spanned group is edited separately.
Currently no support to edit all cells of spanned group at once (subclasses can override it though).


Like the row color, cell alignment

A custom cell renderer needed.
E.g., this one will draw even rows in yellow and will align LAST_NAME column to the right:

table.setDefaultRenderer( Object.class, new DefaultTableCellRenderer() {
    @Override
    public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
        DefaultTableCellRenderer cellComponent = ( DefaultTableCellRenderer )super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
        Object columnId = table.getColumnModel().getColumn( column ).getIdentifier();
        cellComponent.setHorizontalAlignment( columnId.equals( "LAST_NAME" ) ? SwingConstants.RIGHT : SwingConstants.LEFT );
        cellComponent.setBackground( row % 2 == 0 && !isSelected ? Color.YELLOW : null );
        return cellComponent;
    }
});

Note that JBroTable caches rendered cells by their column ID and model row (see caching wiki).
Example above selects color depending on view row (differs from model row if sorter is used), so caching will produce graphical artifacts.
Turn off caching to avoid this:

table.getUI().setCacheUsed( false );

like Table Header

If you need custom rendering then take a look at custom renderer at wiki.

If you need editor inside table header then it's currently not implemented (you can achieve it by implementing custom renderer but it's really complex).
I'd better suggest to show a dialog on clicking a special area of column header.
In that dialog, ask for a new column name and update accordingly.

Thank you very much!
A nice and thorough guide <3