2

Is there a way to align all the column in jtable at the same time? using this:

DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment( JLabel.RIGHT );
JTAB_TABLE.getColumnModel().getColumn(0).setCellRenderer( rightRenderer );

will let me align only one column but i need to align all.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mohammed Falha
  • 967
  • 8
  • 22

2 Answers2

5

Normally, a table contains different kinds of data, (Date, Number, Boolean, String) and it doesn't make sense to force all types of data to be right aligned.

If however you have a table with all the same type of data and you want to force the renderering of all columns to be the same, then you should probably use the same renderer. Assuming you are using the default renderer you can use:

DefaultTableCellRenderer renderer = (DefaultTableCellRenderer)table.getDefaultRenderer(Object.class);
renderer.setHorizontalAlignment( JLabel.RIGHT );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • can we have an answer assuming we are not using `DefaultTableCellRenderer` please? – searchengine27 Nov 10 '15 at 02:07
  • @searchengine27, how can you give an answer when you don't know what the renderer is? – camickr Nov 10 '15 at 02:48
  • why would you assume a single renderer out of all the near-infinite possibilities would be a `DefaultTableCellRenderer` in the first place? I'll grant you it's probably going to be one of the most common, but I'd appreciate an answer more that builds a cell renderer from `TableCellRenderer` so I can see how to build that functionality and how it would work. – searchengine27 Nov 10 '15 at 03:05
  • @searchengine27, This question is about right aligning text. How can you give an example for all possible renderers in existence? That is why I clarified by answer. If you just want to know how to create a custom renderer, then that is a different question and you cam read the section from the Swing tutorial on [Using Custom Renderers]() for an example of creating a custom renderer by implementing the interface. – camickr Nov 10 '15 at 03:22
2

You can do so by overriding prepareRenderer(...) in JTable. This assumes that any custom renderers are JLabels (they're JLabels by default). You'd have to guard against it otherwise.

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

public class TableDemo implements Runnable
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new TableDemo());
  }

  public void run()
  {
    JTable table = new JTable(5, 5)
    {
      @Override
      public Component prepareRenderer(TableCellRenderer renderer,
                                       int row, int col)
      {
        Component comp = super.prepareRenderer(renderer, row, col);
        ((JLabel) comp).setHorizontalAlignment(JLabel.RIGHT);
        return comp;
      }
    };
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    JScrollPane scrollPane = new JScrollPane(table);

    JFrame frame = new JFrame();
    frame.getContentPane().add(scrollPane);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
splungebob
  • 5,357
  • 2
  • 22
  • 45