Solved

JTable with MultiLine cells using JLabel and Html

Posted on 2007-11-20
8
3,836 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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