?
Solved

JTable with MultiLine cells using JLabel and Html

Posted on 2007-11-20
8
Medium Priority
?
3,931 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Expert Comment

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

Accepted Solution

by:
objects earned 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

801 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