0

I have a JPanel (and I've tried a JTextArea as well) that I would like to have text on it. The text needs to be

  1. Centered
  2. Have word wrap
  3. Use a custom font from a .ttf file that I am currently using that is not installed on the computer. I have already solved this problem, and the font works fine. It does not, however, work when placed in tags as I have seen as a possible solution for my problem.

I can generally get two of these rules to work, but cannot get all three to work at once. I've tried html tags, JLabels, JTextAreas, but none satisfy all conditions. Any suggestions?

    static JTextArea quesLbl;
quesLbl = new JTextArea("");
quesLbl.setWrapStyleWord(true);
quesLbl.setLineWrap(true);
quesLbl.setEditable(false);
quesLbl.setFont(qFont);
quesLbl.setOpaque(true);
quesLbl.setForeground(Color.WHITE);
quesLbl.setBackground(Color.BLUE);

This code results in the text being word wrapped, and the font working, but not being centered.

nicocolton
  • 83
  • 1
  • 10

2 Answers2

2

Assuming you mean horizontally centered, I would suggest using a JTextPane. I did not test, but I would hope, that setting your font would work just as well. For the centering, see Centering Text in a JTextArea or JTextPane - Horizontal Text Alignment, I am quoting the answer there now:

public JTextPane createTextPane(String text){
  JTextPane textPane = new JTextPane();
  tp.setText(text);
  StyledDocument doc = textPane.getStyledDocument();
  SimpleAttributeSet center = new SimpleAttributeSet();
  StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
  doc.setParagraphAttributes(0, doc.getLength(), center, false);+
}

IIRC word wrapping should come free. Hope this helps, if not, clarify the question please.

Community
  • 1
  • 1
Carsten Hoffmann
  • 901
  • 5
  • 14
  • I'm a bit unsure of how to use these. Could you provide another example of how to use the JTextPane? – nicocolton Feb 20 '14 at 17:01
  • @nicocolton added some lines. The code now creates the textpane and sets the text. It also should be centered. Code is untested, so no warranty ;-) If you want to set the font, look here http://javatechniques.com/blog/setting-jtextpane-font-and-color/ – Carsten Hoffmann Feb 20 '14 at 17:09
  • Got it to work, word wrap and centering works, but the custom font does not show (though the font size is correct). Like I said, this font is not installed on the computer, but it does work otherwise on JLabels. – nicocolton Feb 20 '14 at 17:22
  • Open up a new question and show your code there (loading the font etc.) then link it to this one. That will give it more attention. I have no solution currently, sorry. – Carsten Hoffmann Feb 20 '14 at 18:28
0

This did work for the centered and word-wrap part for me:

JLabel label = new JLabel();
label.setText("<html><body style='text-align:center'> This is some very long sentance with many letters and not so much meaning.</body></html>");
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);

The above solution is also working, but I needed that the label is on top of another component of mine (which doesn't work with JTextPane).

Mariana
  • 1,199
  • 1
  • 7
  • 6