Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.swing.*;

import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

public class Main {
    public static void SetJTableAlignment(javax.swing.JTable jTable, ComponentOrientation componentOrientation) {
        Font tahoma = new Font("Tahoma", Font.PLAIN, 11);
        int labelAlighnment = JLabel.RIGHT;
        int headerAlighnment = JLabel.RIGHT;
        if (componentOrientation == ComponentOrientation.LEFT_TO_RIGHT) {
            labelAlighnment = JLabel.LEFT;
            headerAlighnment = JLabel.LEFT;
        }

        DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
        defaultTableCellRenderer.setHorizontalAlignment(labelAlighnment);
        // defaultTableCellRenderer.setFont(tahoma);

        for (int columnIndex = 0; columnIndex < jTable.getColumnCount(); columnIndex++) {
            if (jTable.getModel().getColumnClass(columnIndex).equals(Boolean.class)) {
                jTable.getColumnModel().getColumn(columnIndex).setWidth(60);
                jTable.getColumnModel().getColumn(columnIndex).setMaxWidth(90);
                jTable.getColumnModel().getColumn(columnIndex).setMinWidth(10);
                jTable.getColumnModel().getColumn(columnIndex).setPreferredWidth(60);
                continue;
            }
            if (jTable.getModel().getColumnClass(columnIndex).equals(ImageIcon.class)) {
                continue;
            }
            jTable.getColumnModel().getColumn(columnIndex).setCellRenderer(defaultTableCellRenderer);

        }
        DefaultTableCellRenderer renderer;
        renderer = (DefaultTableCellRenderer) jTable.getTableHeader().getDefaultRenderer();
        renderer.setHorizontalAlignment(headerAlighnment);

        jTable.getTableHeader().setDefaultRenderer(renderer);

    }
}