JScrollPane Content Not Displaying

I have a JTextArea inside a JPanel, which is then placed in a JScrollPane. When the JPanel containing the JScrollPane first shows that the JScrollPane is displayed, but not the content. As soon as the size of the JFrame changes, the content appears.

JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");

JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);

JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
+3
source share
3 answers
pane.setBounds(20, 20, WIDTH - 40, 300 - 40); 
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40)); 

These two lines of code do not make sense (although they are not the cause of your problem)

The first line is used when you use the "zero layout".

The second is used when you use layout managers.

They should not be used together.

The second is preferable since you should use layout managers.

+4
source

JPanels , -. , - :

panel.remove(slide1);
panel.add(slide2);
panel.repaint();

, slide2 . ,

frame.validate();

- .

+2

new JScrollPane(panel);

I believe that you need to add a panel to the scrollbar's constructor.

0
source

All Articles