4

can anyone please tell me how to underline text on JTextField? I create a code that have a JPopupMenu when you right clicked on textfield the menu "bold,italic,underline,plain" will popup and when you clicked underline it will underline the text inside JtextField.

1 Answer 1

5

Yes - by adjusting the Font attributes of the component

Font font = textField.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
textField.setFont(font.deriveFont(attributes));
5
  • cannot find symbol...what do i need to import?
    – PhantomKid
    Oct 20, 2013 at 15:28
  • If you're using an IDE, you can use auto-import, e.g Ctrl+Shift+O for Eclipse
    – Reimeus
    Oct 20, 2013 at 15:33
  • Then look it up in the javadoc, or use an IDE
    – Robin
    Oct 20, 2013 at 15:34
  • TextAttribute cannot find symbol
    – PhantomKid
    Oct 20, 2013 at 15:43
  • First result from Google java.awt.font.TextAttribute
    – Reimeus
    Oct 20, 2013 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.