0

I have created a demo by swing and I want to show full text in textarea. I want to add two textarea into a scroll bar to show all component within a scroll bar. So to do that, I create a panel and then add two textarea on this one and then I add this panel into scroll bar. When I running demo, it show correctly and it also show correctly when I resize window larger but it have problem when I resize smaller screen. It does not wrap the text when resize the screen smaller (instead of appearing horizontal scroll bar). Can you help me to automatically wrapped when resize the screen smaller.

Here my code: JPanel gui = new JPanel(new GridLayout()); gui.setBorder(new EmptyBorder(2, 3, 2, 3));

 JTextArea area = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
 area.setWrapStyleWord(true);
 area.setLineWrap(true);
 gui.add(area);

 JTextArea area1 = new JTextArea("JTextArea on JPanel inside JScrollPane does not resize properly");
 area1.setWrapStyleWord(true);
 area1.setLineWrap(true);
 gui.add(area1);

 gui.setBackground(Color.WHITE);

 JScrollPane scroller = new JScrollPane(gui);
 //scroller.setBorder(new EmptyBorder(2, 3, 2, 3));
 JFrame f = new JFrame("Big Text Fields");
 f.add(scroller,BorderLayout.CENTER);
 // Ensures JVM closes after frame(s) closed and
 // all non-daemon threads are finished
 f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 // See http://stackoverflow.com/a/7143398/418556 for demo.
 f.setLocationByPlatform(true);

 // ensures the frame is the minimum size it needs to be
 // in order display the components within it
 f.pack();
 // should be done last, to avoid flickering, moving,
 // resizing artifacts.
 f.setVisible(true);
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38

1 Answers1

0

Just omit the scroll pane and make the text area a direct child of the container.

JTextArea does change its preferred size based on the text it contains. From the documentation:

java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane. If the value for rows or columns is equal to zero, the preferred size along that axis is used for the viewport preferred size along the same axis.

Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38