Solved

JTable with MultiLine cells using JLabel and Html

Posted on 2007-11-20
8
3,818 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
Comment Utility
Try using a <table> markup instead
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:TheMajestic
Comment Utility
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
Comment Utility
> 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
Comment Utility
Did you try the <table> markup?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now