Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super, subscript with JLabel

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Is there anyway to use superscript and subscript with a JLabel combining bold , italic etc. I do not wish to use HTML because the JLabel is in a cell in a Jtable and the user sees the text when he/she edits it.

What is the best approach? The JTable is used for a user to add , edit data that will be used in a catalouge. Every cell can

* Edit alignment
* bold
* Italic
* Underline
* subscript
* superscript
* maybe bullets


If I implement TableCellEditor and extends another component that can handle the input above would this do the trick? Some leads?


// Mathias
[ December 05, 2005: Message edited by: Mathias Nilsson ]
 
Brian Cole
Author
Ranch Hand
Posts: 906
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a TableCellRenderer that extends JTextPane but,
depending on exactly how you want it to behave, it could be a lot
of work.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.... I have realized that...

Thanks!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your editor and your renderer could use different components, so I
don't see what's so bad about a JLabel with text like:

<html>Sample <b>bold</b>, <i>italics</i> etc...</html>

As for your editor, how do you imagine entering superscripts and
supscripts? As a first approximation I would just edit the raw markup,
I mean if your users are untrained, anything else is at least as complex.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The users don't know HTML. I must see to that the input isn't wrong because I must parse the html tags and replace the tags against other tags. The result is a txt file with data marked up without <html><body> etc.

Do you think the JFrame will be heavy if every cell is a JTextPane?

NOTE: The JFrame is opened from a JApplet

// MAthias
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Do you think the JFrame will be heavy if every cell is a JTextPane?

it doesn't work that way. When you use a cell renderer, it can *reuse* a
single component to draw all its cells, like a rubber stamper.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok!

Thanks.... but will i still be able to use the HTMLEditorKit in the JTextPane?

I have difficulties in getting the swing
 
saager mhatre
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should solve all your problems...
The Element Interface @ The Swing Connection

This works with the DefaultStyledDocument implementaion of the Document Model used by the JTextComponent Component(Controller) in Swing.

Here's some scratchy code to help you along.
I'm sorry there aren't too may comments.


I went a little overboard with the renderer , but I know you'll forgive me!
The key mappings are:
  • Superscript: Alt + Up
  • Normal: Alt + Down
  • Bold: Ctrl + B

  • [ December 07, 2005: Message edited by: saager mhatre ]
    [ December 07, 2005: Message edited by: saager mhatre ]
     
    Mathias Nilsson
    Ranch Hand
    Posts: 367
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi!

    I saw this post a little late but i was in desparate need of it know.

    Thanks alot. Hope I can help you sometime....
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic