How to center a value in JTable
cell? I'm using Netbeans.
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);
Another good way to center all columns is with a loop:
for(int x=0;x<numberofcols;x++){
table1.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
}
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);
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!
Instead of JLabel.CENTER
, you can use SwingConstants.CENTER
.
((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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With