Difference between JScrollPane.setviewportview and JScrollPane.add

Today I came across this new thing, and I did not know why. For example, when I want to show something in a panel, I simply add it to the panel; but why can't I add the table directly to the scroll bar and why do I need to call the setviewportview() method? What does the add() method do and what does setViewProtView() do?

+2
java swing jtable jscrollpane
Nov 22 '13 at 6:04 on
source share
1 answer

Basically, you should not use JScrollPane#add .

JScrollPane has one component already connected to it, JViewport , this is what JScrollPane uses to display any component added to the view port.

enter image description here

setViewportView - convenience method for JScrollPane#getViewport#setView

The basic concept is that in terms of scrolling, only one component is displayed, so add doesn't really make any sense for this. This method is the result of an extension from JComponent Container

+12
Nov 22 '13 at 6:09
source share



All Articles