set JTable Column Alignment - Java Swing

Java examples for Swing:JTable Column

Description

set JTable Column Alignment

Demo Code


import java.awt.Color;
import java.awt.Component;
import java.text.NumberFormat;
import java.util.regex.Pattern;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class Main{
    /**/*www .  ja  va 2s .c  om*/
     * @param table
     * @param columnIndex
     * @param alignment
     */
    public static void setColumnAlignment(JTable table, int columnIndex,
            int alignment) {
        AlignmentRenderer renderer = new AlignmentRenderer(alignment);
        TableColumn tc = table.getColumnModel().getColumn(columnIndex);
        tc.setCellRenderer(renderer);
    }
}

Related Tutorials