how to set center alignment in JTable?

L

lucy

Hi, I've used the following to set the alignment for cells in JTable...

ttt.setAlignmentX(0.5);
ttt.setAlignmentY(0.5);

where ttt is a JTable object,

but it did not work at all,... (the JTable display shows no change at
all...)

What's the problem?
 
N

Nick Pomfret

lucy said:
Hi, I've used the following to set the alignment for cells in JTable...

ttt.setAlignmentX(0.5);
ttt.setAlignmentY(0.5);

where ttt is a JTable object,

but it did not work at all,... (the JTable display shows no change at
all...)

What's the problem?

The solution is to use a CellRenderer.

Create a class that extends DefaultTableCellRenderer and implement the
getTableCellRendererComponent method, something like:

public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected,
boolean hasFocus, int
row, int column) {
JLabel renderedLabel = (JLabel) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
renderedLabel.setHorizontalAlignment(SwingConstants.RIGHT);
return renderedLabel;
}


The DefaultTableCellRenderer is actually just a JLabel which JTable uses to
render each cell (a flywieght). You can then manipulate this JLabel as you
feel (set the alignment, text, colors, icon etc).

Then you need to apply this renderer to your JTable. You can do this by
overriding the JTable.getCellRenderer method:

public class MyJTable extends JTable {
private TableCellRenderer renderer;

public MyJTable(CellRenderer renderer) {
this.renderer = renderer;
}

public TableCellRenderer getCellRenderer(int row, int column) {
return renderer;
}
}
 
N

Nick Pomfret

lucy said:
Hi, I've used the following to set the alignment for cells in JTable...

ttt.setAlignmentX(0.5);
ttt.setAlignmentY(0.5);

where ttt is a JTable object,

but it did not work at all,... (the JTable display shows no change at
all...)

What's the problem?

Apologies if my post comes though twice, my PC died as I was sending the
last one.

The solution is to use a CellRenderer. By extending
DefaultTableCellRenderer and implementing the getTableCellRendererComponent
you can format cells in a JTable as you wish.

The DefaultTableCellRenderer is a JLabel that a JTable uses to render each
of its cells. The following will render a cell as you need:

public class MyRenderer extends DefaultTableCellRenderer {

...

public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected,
boolean hasFocus, int
row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
setHorizontalAlignment(SwingConstants.CENTER);
return this;
}
}

Now you need to add this into your JTable. Simplest way is to override the
JTable.getCellRenderer method:

public class MyJTable extends JTable {
private MyCellRenderer renderer;

public MyJTable(MyCellRenderer renderer) {
this.renderer = renderer;
}

public TableCellRenderer getCellRenderer(int row, int column) {
return renderer;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top