CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2001
    Posts
    18

    aligning text in JTable

    Does anyone know how to align text in JTable?
    i am retrieving information from the db. and populating them in a JTable.


  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: aligning text in JTable

    It depends on the TableCellRenderer used to render the cell contents to the display. The default table cell renderer, verbosely called DefaultTableCellRenderer (you get this if you don't specify one), is a subclass of JLabel, so you can call the alignment methods of JLabel on it, something like thisefaultTableCellRenderer dtcr = (DefaultTableCellRenderer)myTable.getColumn("Widgets").getCellRenderer();
    dtcr.setHorizontalTextPosition(SwingConstants.CENTER);

    Dave

    To email me remove '_spamjam' from my email address
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jun 2001
    Posts
    18

    Re: aligning text in JTable

    hi,
    when i try implementing the way you suggest, i am getting a null ptr exception. i think the cell renderer is not getting the correct value. in getcolumn method of the cell renderer is "Widgets" the name of the column?


  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: aligning text in JTable

    I put in "Widgets" as an example column name. In a default situation, if an identifier is not set for a TableColumn, the identifier is the column header text. If you have set an identifier for the column, you should use that. The point is just to get the TableColumn you're after... if you don't want to do it this way, you can get the column from the TableColumnModel by its index:TableColumn col2 = myTable.getColumnModel().getColumn(2);

    You'll find all this in the JavaDoc for JTable. It pays to browse this stuff.

    Dave

    To email me remove '_spamjam' from my email address
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Jun 2010
    Posts
    1

    Re: aligning text in JTable

    DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
    dtcr.setHorizontalAlignment(SwingConstants.RIGHT);
    table.getColumn("ABC").setCellRenderer(dtcr);
    table.getColumn("def").setCellRenderer(dtcr);
    table.getColumn("ghi").setCellRenderer(dtcr);
    ......

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: aligning text in JTable

    Congratulations on answering a 9 year old question which had already been answered

    Please do not revive old threads.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured