Reputation: 65
I have relatively big text which is represented by a single JLabel
. The text is added to the JPanel
component with GridBagLayout
.
I would like the text to fill all available space within the panel on the x-axis and provide scrolling only on the y-axis. Also, scrolling and text alignment should change accordingly when the panel is scaled.
So, consider the following code sample :
JPanel panel = new JPanel(new GridBagLayout());
JLabel desc = new JLabel("largelarge text");
JScrollPane scrollPane = new JScrollPane(desc, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scrollPane, gbc constraints here);
Any suggestions?
Upvotes: 0
Views: 237
Reputation: 324197
I would like the text to fill all available space within the panel on the x-axis and provide scrolling only on the y-axis
You would need to create a custom panel and implement the Scrollable
interface.
You would implement the getScrollableTracksViewportWidth()
method to return “true” and the getScrollableTracksViewportHeight()
method to return “false”. You would also need to implement the other methods of the interface to provide reasonable values.
Or, you can use the Scrollable Panel which implements all the Scrollable
interface methods with default behaviour and provides methods to allow you to change the behaviour.
Upvotes: 1