I was wondering if there was a way to use a JLabels entire area for text, Whenever I use a JLabel all of the text is in the center even if the amount of text exceeds the size of JLabel. Is there a way to tell the text in a JLabel to use the whole Jlabel?
Asked
Active
Viewed 37 times
0
-
Do you mean to have the text either spread out, or change its font to something larger? I'm wondering if you may want to do custom painting, and use FontMetrics to help decide what size Font to use. – DontKnowMuchBut Getting Better Jan 27 '18 at 22:19
-
spread out thats what I want – ComputerCoderAN Jan 27 '18 at 22:19
-
Perhaps then use a JPanel that uses a `GridLayout(1, 0)` (one row, variable columns), and add each letter as its own JLabel to that same JPanel – DontKnowMuchBut Getting Better Jan 27 '18 at 22:20
-
Use an appropriate layout manager, wrap the text in `html` to have it support multiple lines – MadProgrammer Jan 27 '18 at 22:21
-
1Or if your text is large, then use a JTextArea and change its properties so that it *looks* like a JLabel. You'd need to set the line and word wrap properties of course. – DontKnowMuchBut Getting Better Jan 27 '18 at 22:22
-
[As an example (of using text wrapped in html)](https://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines/14738193#14738193) – MadProgrammer Jan 27 '18 at 22:23
-
First of all you answer via the answer question button and each letter with its own label that would get laggy fast. – ComputerCoderAN Jan 27 '18 at 22:23
-
1@ComputerCoderAN The answer comes down to a combination of things. If you want the label to "expand" it's width to match the size of the text, then you need to use a layout manager which will allow it to do so. If you want the text to wrap in multiple lines, you either need to wrap in html or use and readonly `JTextArea`, but also use a layout manager that supports it – MadProgrammer Jan 27 '18 at 22:27
-
Okay I want the text to go to the next line whenever it exceeds the size – ComputerCoderAN Jan 27 '18 at 22:30
-
Possible duplicate of [JLabel - Show longer text as multiple lines?](https://stackoverflow.com/questions/14737810/jlabel-show-longer-text-as-multiple-lines) – DontKnowMuchBut Getting Better Jan 27 '18 at 22:37
1 Answers
1
You could...
Use wrap you text in html
and set it to a label, it will automatically wrap to the constraints of the container
public class TestHTMLLabel {
public static void main(String[] args) {
new TestHTMLLabel();
}
public TestHTMLLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
StringBuilder sb = new StringBuilder(64);
sb.append("<html>I have something to say, it's beter to burn out then to fade away.").
append(" This is a very long String to see if you can wrap with in").
append("the available space</html>");
JLabel label = new JLabel(sb.toString());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(label);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
You could...
Use a non-editable JTextArea
, which supports line/word wrapping out of the box
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
StringBuilder sb = new StringBuilder(64);
sb.append("I have something to say, it's beter to burn out then to fade away.").
append(" This is a very long String to see if you can wrap with in").
append("the available space");
setLayout(new BorderLayout());
JTextArea ta = new JTextArea(10, 20);
ta.setOpaque(false);
ta.setEditable(false);
ta.setBorder(null);
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
ta.setText(sb.toString());
add(ta);
}
}
}
Also remember, the choice of layout manager will have a large impact on the output. Both examples use a BorderLayout
, but you could also use a GridBagLayout
, just beware

MadProgrammer
- 343,457
- 22
- 230
- 366