This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable cells and html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable cells and html" Watch "JTable cells and html" New topic
Author

JTable cells and html

Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Hello!

I have a JTable with a unspecified amount of columns and cells.

I want the user to have access to bold, italic , underline and alignment in each and every cell. Is there a way to do it. Please point me in a direction.

If we use html tags in the cell then the users sees the html and we don't want that .

// Mathias
[ December 01, 2005: Message edited by: Mathias Nilsson ]

SCJP1.4
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Mathias Nilsson:
If we use html tags in the cell then the users sees the html


JTable actually does have some limited built-in
html support. For example, if you run this

public static void main(String argv[]) {
TableModel tm = new DefaultTableModel(8, 3);
JTable tab = new JTable(tm);
JFrame f = new JFrame();
f.getContentPane().add(new JScrollPane(tab), BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 200);
f.setVisible(true);
}

and type "<html>a <font color=red>b</font> c"
(without the quotes) into a cell and press return,
you will see "a b c" with the 'b' colored red.

This only works if the first six chars are
"<html>" (again without the quotes). There
are other limitations too, but you may want
to play with it.


bitguru blog
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks!

I was thining... can I use JLabel in cells and use bold on the fin and alignment on the JLabel?

Does anyone know how to do this?

// Mathias
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Mathias Nilsson:
can I use JLabel in cells and use bold on the fin and alignment on the JLabel?


If you take a look at the DefaultTableCellRenderer
class, you'll see that it is a JLabel. So if
you set one as a renderer, you can set its font and
alignment just like you would any other JLabel. You
don't use cell renderers like you use labels, though.
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
Thanks!

How do I use the JLabel to paint bold , Italic? I can only do JLabel.getText()..... Otherwise it just prints javax.swing.Jlabel .... and a lot of data in the cell.

// MAthias
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable cells and html
 
Similar Threads
Border around cells in JTable
JTable "..."
searching a JTable that has editor
JTable
JTABLE