Java examples for Swing:JTable
Set JTable Alignment
//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; }// w w w . j a v a2 s. c om 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); } /* JTableHeader header = jTable.getTableHeader(); DefaultTableCellHeaderRenderer defaultTableCellHeaderRenderer = (DefaultTableCellHeaderRenderer) header.getDefaultRenderer(); defaultTableCellHeaderRenderer.setHorizontalTextPosition(headerAlighnment); defaultTableCellHeaderRenderer.setFont(tahoma);*/ DefaultTableCellRenderer renderer; renderer = (DefaultTableCellRenderer) jTable.getTableHeader() .getDefaultRenderer(); renderer.setHorizontalAlignment(headerAlighnment); jTable.getTableHeader().setDefaultRenderer(renderer); } }