[JTable] Alignment in custom cell renderer

Discussion in 'Java' started by Branko Kaucic, Sep 3, 2005.

  1. 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
    --
     
    Branko Kaucic, Sep 3, 2005
    #1
    1. Advertising

  2. Branko Kaucic

    jan V Guest

    > I used custom rendered, because I need dots in values (for example:
    > 1.000.000).


    Use a DecimalFormat object to format the String, then render as usual.
     
    jan V, Sep 3, 2005
    #2
    1. Advertising

  3. Branko Kaucic

    Roland Guest

    On 3-9-2005 16:55, Branko Kaucic wrote:
    > 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_/ /__/
    / \ /_/ / \
     
    Roland, Sep 3, 2005
    #3
  4. Branko Kaucic

    Roedy Green Guest

    On Sat, 03 Sep 2005 16:55:29 +0200, Branko Kaucic
    <> wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 6, 2005
    #4
  5. It works.
    Thanks!

    B

    Roedy Green wrote:
    > On Sat, 03 Sep 2005 16:55:29 +0200, Branko Kaucic
    > <> wrote or quoted :
    >
    >
    >>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.
    >
     
    Branko Kaucic, Sep 13, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RobE
    Replies:
    2
    Views:
    2,065
    =?ISO-8859-1?Q?J=F6rg_Marti?=
    Aug 12, 2003
  2. Tivo Escobar
    Replies:
    1
    Views:
    7,422
    manusa
    Apr 12, 2007
  3. John M

    Table cell renderer

    John M, Jan 27, 2004, in forum: Java
    Replies:
    1
    Views:
    671
    Christian Kaufhold
    Jan 28, 2004
  4. Bryan R. Meyer
    Replies:
    4
    Views:
    3,917
    Bryan R. Meyer
    Sep 6, 2004
  5. Replies:
    2
    Views:
    2,307
Loading...

Share This Page