Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center in JTable cell a value?

Tags:

java

swing

jtable

How to center a value in JTable cell? I'm using Netbeans.

like image 924
Daniel Pereira Avatar asked Sep 15 '11 15:09

Daniel Pereira


6 Answers

You need to customize the renderer. To center the first column you can do:

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );

To center all columns with String data you can do:

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.setDefaultRenderer(String.class, centerRenderer);
like image 161
camickr Avatar answered Oct 07 '22 20:10

camickr


Another good way to center all columns is with a loop:

 for(int x=0;x<numberofcols;x++){
         table1.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
        }
like image 41
Fernando Mendoza Avatar answered Oct 07 '22 19:10

Fernando Mendoza


Here you go:

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;

public class JTableUtilities
{
    public static void setCellsAlignment(JTable table, int alignment)
    {
        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(alignment);

        TableModel tableModel = table.getModel();

        for (int columnIndex = 0; columnIndex < tableModel.getColumnCount(); columnIndex++)
        {
            table.getColumnModel().getColumn(columnIndex).setCellRenderer(rightRenderer);
        }
    }
}

Usage:

JTableUtilities.setCellsAlignment(table, SwingConstants.CENTER);
like image 24
BullyWiiPlaza Avatar answered Oct 07 '22 18:10

BullyWiiPlaza


I had a similar problem. I wanted to align a single cell depending on the value of another cell. If cell X was NULL, then cell Y should be RIGHT aligned. Else, cell Y should be LEFT aligned.

I found this solution really helpful. It consists on creating a custom Render, extending DefaultTableCellRender.

Here's the code:

public class MyRender extends DefaultTableCellRenderer{

@Override
 public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column){
    super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);
    MyTableModel mtm = (MyTableModel)table.getModel();
    switch(column){
        case Y : 
            if(mla.getValueAt(row,X)!=null)
                setHorizontalAlignment(SwingConstants.RIGHT);
            else
                setHorizontalAlignment(SwingConstants.LEFT);
            break;
     }
    return this;
   }

 }

After that, just create a new instance of MyRender and set it to column Y, in this case. I do this when I load the information on the table.

MyRender render = new MyRender();
table.getColumnModel().getColumn(Y).setCellRender(render);

Hope it's useful!

like image 42
jUNT Avatar answered Oct 07 '22 20:10

jUNT


Instead of JLabel.CENTER, you can use SwingConstants.CENTER.

like image 21
leejsmith1991 Avatar answered Oct 07 '22 19:10

leejsmith1991


((DefaultTableCellRenderer) jTable1.getTableHeader().getDefaultRenderer())
            .setHorizontalAlignment(JLabel.CENTER); // header to center

This code header center jtable

DefaultTableCellRenderer rendar = new DefaultTableCellRenderer();
    rendar.setHorizontalAlignment(jLabel1.CENTER);

    jTable1.getColumnModel().getColumn(0).setCellRenderer(rendar); 

this code to column center

like image 39
Shinwar ismail Avatar answered Oct 07 '22 19:10

Shinwar ismail