[JTable] Alignment in custom cell renderer

B

Branko Kaucic

Hi all!

I have a JTable grid, like the one in Java Swing tutorial (see code
below). I created custom cell renderer for cells that contain values of
type Integer, but the text is aligned to te left insted of usual right.
What must I do to align the values on the right.
I used custom rendered, because I need dots in values (for example:
1.000.000).

Ragards,
Branko

--
import javax.swing.table.*;
import java.text.*;

public class MyCellRenderer extends DefaultTableCellRenderer {

private NumberFormat nf;

public MyCellRenderer() { super(); }

public void setValue(Object value) {

if (nf == null)
nf = NumberFormat.getIntegerInstance();
if (value == null)
setText("");
else
setText(nf.format(value));
}

} // end class
--
 
R

Roland

Hi all!

I have a JTable grid, like the one in Java Swing tutorial (see code
below). I created custom cell renderer for cells that contain values of
type Integer, but the text is aligned to te left insted of usual right.
What must I do to align the values on the right.
I used custom rendered, because I need dots in values (for example:
1.000.000).

Ragards,
Branko
Use
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
in MyCellRenderer's constructor (after the call to super();).

[Or, if that doesn't work (though it should ;-) ), add the line in your
setValue method.]
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
R

Roedy Green

Hi all!

I have a JTable grid, like the one in Java Swing tutorial (see code
below). I created custom cell renderer for cells that contain values of
type Integer, but the text is aligned to te left insted of usual right.

I think you can solve it like this:

public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{

Component cell = super.getTableCellRendererComponent( table,
value,
isSelected,
hasFocus,
row,
column);


This beast I think you will find cell is just a JLabel. So you can
cast it to JLabel. Be aware that Swing reuses the same JLabel
component over and over, so anything you fiddle will have general
repercussions.

JLabel.setAlignment( JLabel. LEFT ) or JLabel.setAlignment(
JLabel.RIGHT ) lets you control the justification.
 
B

Branko Kaucic

It works.
Thanks!

B

Roedy said:
I think you can solve it like this:

public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{

Component cell = super.getTableCellRendererComponent( table,
value,
isSelected,
hasFocus,
row,
column);


This beast I think you will find cell is just a JLabel. So you can
cast it to JLabel. Be aware that Swing reuses the same JLabel
component over and over, so anything you fiddle will have general
repercussions.

JLabel.setAlignment( JLabel. LEFT ) or JLabel.setAlignment(
JLabel.RIGHT ) lets you control the justification.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,109
Latest member
JanieMalco
Top