Solved

JTable with MultiLine cells using JLabel and Html

Posted on 2007-11-20
8
3,846 Views
Last Modified: 2008-09-20
So I have a JTable that is currently displaying multiline rows. I'm using the DefaultTableCellRenderer as my renderer which is a subclass of JLabel. I got the multiline rows working by puting html inside the JLabels and setting the rowHeight to 32. So my issue is that the multiline data in the cells are not correctly aligning the way I want them to. For example:

formattedSectorData.addElement("<html><p align=\"right\">" + value + "<br>" + cost + "%</p></html>");

I want the data to be displayed aligned to the right instead of the default left.

This is what I'm getting.
---------------------
|  Table Header   |
---------------------
|value                 |
|cost                   |
---------------------

This is what I want to achieve.
---------------------
|  Table Header   |
---------------------
|                value |
|                  cost |
---------------------

I must use a JLabel so dont suggest that I sub-class JTextArea or JTextPane, I've already tried those options and they create other problems that would make simple things with JLabel very difficult. Thanks in advance.
0
Comment
Question by:vferreira71
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20322965
Try using a <table> markup instead
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 20323069
label.setHorizontalAlignment(SwingConstants.RIGHT);

or use css

also have an example of a text area renderer if interested

http://www.objects.com.au/java/examples/swing/table/MultiLineCellRenderer.java
0
 

Author Comment

by:vferreira71
ID: 20323240
formattedSectorData.addElement("<html><span style=\"text-align:right\">" + value + "<br>" + cost  + "</span></html>");

This didn't work. I tried setHorizontalAlignment and that worked but it did the whole table and some didn't display correctly, meaning that some of the text would be missing in the cell as if the cell wasn't big enough for the text. So How can I do setHorizontalAlignment for just 1 column?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:TheMajestic
ID: 20325612
hi,

You can control both the horizontalAlignment and verticalAlignment properties of a label's content (text and icon).

The methods setHorizontalAlignment() and setVerticalAlignment() are used with the following SwingConstants:


Horizontal alignment -- LEFT, RIGHT or CENTER
Vertical alignment -- TOP, BOTTOM or CENTER

for example.

     // Create the labels and set alignment
    JLabel label1 = new JLabel("BottomRight", SwingConstants.RIGHT);
    label1.setVerticalAlignment(SwingConstants.TOP);
 this will give the TopRight alignment of the JLable.

hope this will help u
0
 
LVL 92

Expert Comment

by:objects
ID: 20325632
> So How can I do setHorizontalAlignment for just 1 column?

either use a different renderer instance for that column
or check the column number in the renderer
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20326503
Did you try the <table> markup?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question