Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JTable with MultiLine cells using JLabel and Html

Posted on 2007-11-20
8
Medium Priority
?
3,974 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

609 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