I want to output text from a .txt file which will have more and more text, however if I do that without modifying the size of the JLabel, the text just goes off screen, is it possible to somehow auto increase a JLabel while somehow detecting how much more is needed to increase to fill all of the text? Or is there any other text holder which is not editable but would fulfill that need?
-
1Use [**JList**](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html) instead lables. – Azad Jul 15 '13 at 07:55
-
21- Use a non-editable `JTextArea` 2- Wrap the text in html – MadProgrammer Jul 15 '13 at 07:55
-
depends on your exact requirement/context - be precise when describing the problem (and how it should behave), best with a SSCCE – kleopatra Jul 15 '13 at 09:29
-
@Azad , I guess I wasn't clear enough, however I do not need any kind of list, only simple text going on and on. MadProgrammer thank you very much for the contribution. kleopatra , I'm sorry, I simply had nothing more to add, it is in planning phase and I did mention I wanted text, no list no nothing, which keeps being added from a .txt file, all of that can be understood as simple methods. The needed thing was that I would have the ability to increase the size of the label as the text increases so I don't go more then it and not see the text. It was all from a logic standpoint,thank you tho – Ted Jul 15 '13 at 10:14
3 Answers
You can make any Swing Component with a Document non-editable, so you´re free to choose. Choosing something that can be nicely wrapped in JScrollPane, like the mentioned JList or JTextArea, is a good choice.

- 1,279
- 4
- 19
- 38
-
Thank you so much, I will accept the answer because of the awesome documentation – Ted Jul 15 '13 at 10:10
Although a variable sized JLabel is probably not good UI style, you could achieve the desired functionality by putting the JLabel in an appropriate layout manager and requesting a new layout. How to force a new layout is described here.
SwingUtilities.invokeLater(new Runnable {
public void run() {
somecomponent.repaint();
}
});
-
1revalidate() do that, code line with repaint() is required to add in some cases – mKorbel Jul 15 '13 at 08:15
Use Text Area if you have long text. Its not advisable to have auto increasing length of components. Because alignments of your other components on screen may suffer.
For these type of situations we rely on scrollBar.
So therefore. Use TextArea, set a preferredSize for them. and Then Use ScollBar if text goes out of Preferred Size.
Another Solution : Decide Min and Max Size for your component. Default display will show text in Min Size. If text increases, then increase size of text Area. Stop increasing size till it hits Max size. Show scrollBars if it hits max. Remember for this you might have to override JTextArea or its UIComponent(preferred).

- 2,444
- 1
- 21
- 30
-
1on the verge of downvoting for [setXXSize - don't, ever](http://stackoverflow.com/a/7229519/203657) JTextArea has api to set its preferred size in terms of rows/columns, a suitable LayoutManager will handle any further fine-tuning if needed – kleopatra Jul 15 '13 at 09:27
-
1@kleopatra i agree. And i think I mentioned preferredSize Only (made it bold) . But if you use custom ComponentUI, you are in very much control of your component and you can use these values judiciously. ComponentUI handles the main responsiblity for rendering the component. see http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/ComponentUI.html – veritas Jul 15 '13 at 09:54