开发者

Align text in a JTable cell while maintaining JTable default renderer?

开发者 https://www.devze.com 2023-01-24 03:08 出处:网络
I have a JTable that d开发者_如何学JAVAoes lovely alternations in coloration thanks to this code:

I have a JTable that d开发者_如何学JAVAoes lovely alternations in coloration thanks to this code:

table.setDefaultRenderer(Object.class, new BorderRenderer(new ColorRenderer(table
                .getDefaultRenderer(Object.class), colorProvider)));

(Here the "colorProvider" contains code that turns every other row light grey.) I have one column in which I would like to align text to center. If I simply set a DefaultCellRenderer on that column with center horizontal alignment, it overrides the table's default renderer and I lose my lovely stripes.

Can I keep the stripes and still change the alignment of a column?

To further complicate things, I have the above code in a super class. It is my sub classes that know what column(s) they would like to change the alignment on.


Can I keep the stripes and still change the alignment of a column?

An easier approach is to use Table Row Rendering. Then you can customize the alignment of individual renderers without affecting the row colors.

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.getColumnModel().getColumn(???).setCellRenderer( centerRenderer );


If you try to set the alignment on the default renederer that is set in your superclass's code, you will affect every cell. An option you have is to create a new subclass for either BorderRenderer or ColorRenderer (whichever is most appropriate), and in its getTableCellRendererComponent get the value returned from calling super.getTableCellRendererComponent and set the alignment on that before returning. You can then set that an instance of that renderer for the column that you need aligned.


Thanks both to afk and camickr who led me to my final decision to write a TableCellRendererWrapper:

public class ColumnAlignmentRenderer implements TableCellRenderer {

    private TableCellRenderer mWrappedRenderer;
    private int mDefaultAlignment = SwingConstants.LEFT;
    private Map<Integer, Integer> mSpecialColumnAlignmentMap = new Hashtable<Integer, Integer>();

    public ColumnAlignmentRenderer(TableCellRenderer pWrappedRenderer) {
        mWrappedRenderer = pWrappedRenderer;
    }

    public Component getTableCellRendererComponent(JTable pTable, Object pValue, boolean pIsSelected,
            boolean pHasFocus, int pRow, int pColumn) {

        // Use the wrapped renderer
        Component renderedComponent = mWrappedRenderer.getTableCellRendererComponent(pTable, pValue, pIsSelected, pHasFocus,
                pRow, pColumn);

        // Set the alignment
        Integer alignment = mSpecialColumnAlignmentMap.get(pColumn);
        if (alignment != null) {
            ((JLabel)renderedComponent).setHorizontalAlignment(alignment);
        } else {
            ((JLabel)renderedComponent).setHorizontalAlignment(mDefaultAlignment);
        }

        return renderedComponent;
    }

    public void setSpecialColumnAlignment(Integer pColumn, Integer pAlignment) {
        mSpecialColumnAlignmentMap.put(pColumn, pAlignment);
    }

    public void setDefaultAlignment(int pAlignment) {
        mDefaultAlignment = pAlignment;
    }
}

It's not perfect, but it gets the job done for me. To use it I do this:

        // Center alignment for "selected" column
        ColumnAlignmentRenderer cellRenderer = new ColumnAlignmentRenderer(getTable().getDefaultRenderer(Object.class));
        cellRenderer.setSpecialColumnAlignment(MyTableModel.COL_TO_CENTER, SwingConstants.CENTER);
        getTable().setDefaultRenderer(Object.class, cellRenderer);

        // Center alignmet for "selected" column header
        ColumnAlignmentRenderer headerRenderer = new ColumnAlignmentRenderer(getTable().getTableHeader().getDefaultRenderer());
        headerRenderer.setSpecialColumnAlignment(MyTableModel.COL_TO_CENTER, SwingConstants.CENTER);
        getTable().getTableHeader().setDefaultRenderer(headerRenderer);


use this method, it will align both header and table content to centered alignment. However you can change the CENTER to LEFT or RIGHT alignment.

    public void setTableAlignment(JTable table){
    // table header alignment
    JTableHeader header = table.getTableHeader();
    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer();
    header.setDefaultRenderer(renderer);
    renderer.setHorizontalAlignment(JLabel.CENTER);

   // table content alignment
   DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
   centerRenderer.setHorizontalAlignment( JLabel.CENTER );
   int rowNumber = table.getColumnCount();
   for(int i = 0; i < rowNumber; i++){
   table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer );
   }
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消