user372743
user372743

Reputation:

JScrollPane Contents Not Showing

I have a JTextArea inside of a JPanel that is then placed into the JScrollPane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents. As soon as the JFrame is resized the contents show up.

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);

Upvotes: 3

Views: 6116

Answers (3)

John Walgreen
John Walgreen

Reputation: 29

new JScrollPane(panel);

I believe that you need to add the panel to the scroll pane constructor.

Upvotes: 1

user372743
user372743

Reputation:

In the application different JPanels are swapped out in a manner similar to a slide-show. So something like this would be found in the code:

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

The problem being that all of the contents of the second slide, slide2, would not show up. The solution is to add

frame.validate();

Where frame is the parent window of panel.

Upvotes: 2

camickr
camickr

Reputation: 324118

pane.setBounds(20, 20, WIDTH - 40, 300 - 40); 
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40)); 

Those two lines of code doen't make sense (although they are not the cause of your problem)

The first line is used when you are using a "null layout".

The second is used when you are using layout managers.

They should not be used together.

The second is preferred since you should be using layout managers.

Upvotes: 3

Related Questions