Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

java - Difference between JScrollPane.setviewportview vs JScrollPane.add

I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basically, you should not use JScrollPane#add.

JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.

enter image description here

setViewportView is a convenience method for for JScrollPane#getViewport#setView

The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...