So say I have a really long line that I want to display in a JLabel
. How can I do it?
Currently, longer lines come up as this:
I have to resize the window to see the complete text.
How can I make it so that there's linebreaks when the text almost reaches the width of my JFrame
?
I'm not sure if any code is required here for you to answer this, but still:
my frame properties:
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(450, 400));
frame.setLocation(new Point(400, 300));
frame.setLayout(new BorderLayout());
The label I want to modify:
question = new JLabel("Question:");
question.setFont(new Font("Serif", Font.BOLD, 15));
question.setHorizontalAlignment(JLabel.CENTER);
EDIT: More details:
I am reading lines from a file and then displaying them. The size of lines is not fixed, and so I do not know where to put <br>
at.
EDIT 2:
I ended up using JTextArea
.
private JTextArea textAreaProperties(JTextArea textArea) {
textArea.setEditable(false);
textArea.setCursor(null);
textArea.setOpaque(false);
textArea.setFocusable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
return textArea;
}
Use HTML to display the text within the Label.
(Taskmaster-suggested example added in)
Format with HTML. Works great.
Just another example, showing that, with the right layout manager, text wrapped in
HTML
tags will automatically wrap to the available space...Something like this. The answer give by rcook is very correct. Its just example to show how it can be done.