0
public class Text extends JPanel {

private String text;


    public Text()
    {
    this.setPreferredSize(new Dimension(20,20));
    setFont (new Font(text, Font.PLAIN, 24));
    text = "";
    }

    public void showUnderline()
    {

    Hashtable<TextAttribute, Object> map = new Hashtable
    <TextAttribute, Object>();

    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);   
    }

the text object will be created within another class. in that class I will need to underline it with the showUnderline method. The method seems incomplete.
I'm shooting for the java exclusive approach, meaning no HTML.
How do I link the text to the showUnderline method?

user1320668
  • 183
  • 1
  • 2
  • 6

1 Answers1

4

What do you mean 'java exclusive approach, meaning no HTML' ? You probably are looking for a JLabel, and you can put very simple html in it. Here's the first result on google:

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel.html

It has an example of making text different colors, fonts, and bold or italicized. You could probably just do something like:

JLabel label = new JLabel("<u>MY TEXT</u>",JLabel.CENTER);

From there, you can place it like you would place any other JComponent.

If you really don't want HTML, you could use a JTextPane. Here's an example:

http://www.exampledepot.com/egs/javax.swing.text/style_hilitewords2.html

  • 1
    `new JLabel("MY TEXT",JLabel.CENTER);` Nu-uh, I think it would be more like.. `new JLabel("MY TEXT");` Note that 1) unless the String starts with the `html` element, it is not interpreted as HTML. 2) `JLabel` alignment constraints are ignored over the style set in HTML. – Andrew Thompson Apr 08 '12 at 19:35