Set horizontal alignment of a certain cell in a certain JTable row. - Java Swing

Java examples for Swing:JTable Row

Description

Set horizontal alignment of a certain cell in a certain JTable row.

Demo Code


//package com.java2s;

import javax.swing.JTable;

import javax.swing.table.DefaultTableCellRenderer;

public class Main {
    /**/*ww w.  ja  v a2s .c om*/
     * Set horizontal alignment of a certain cell in a certain row.
     *
     * @param table
     * @param row
     * @param column
     * @param alignment
     */
    public static void setHorizontalColumnAlignment(JTable table, int row,
            int column, int alignment) {
        ((DefaultTableCellRenderer) table.getCellRenderer(row, column))
                .setHorizontalAlignment(alignment);
    }

    /**
     * Set alignment of a cell in all rows of a table.
     *
     * @param table
     * @param column
     * @param alignment
     */
    public static void setHorizontalColumnAlignment(JTable table,
            int column, int alignment) {

        for (int row = 0; row < table.getRowHeight() - 1; row++) {
            setHorizontalColumnAlignment(table, row, column, alignment);
        }

    }
}

Related Tutorials