So I am trying to create a custom component so I can set the size of my scroll pane. Here is the part with the scroll pane:
JScrollPane scrollPane = new JScrollPane(leftTop);
customScroll t1 = new customScroll();
scrollPane.add(t1);
left.add(scrollPane);
leftTop is a nested panel inside of left. Here is my class for customScroll which is the custom component I am making so I can set the size of the scroll pane:
public class customScroll extends JComponent implements Scrollable {
public customScroll() {
getPreferredScrollableViewportSize();
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return(new Dimension(266,300));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 0;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 0;
}
}
How can I set the preferred scrollable size?
Provide a setter...
Now, obviously, your
getPreferredScrollableViewportSize
will need to return this value...Should also set this before you wrap in your
JScrollPane
, it would just be easier to manage, otherwise, you'll needinvalidate
the container in which theJScrollPane
residesYou might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
No it won't, it will use the
preferredScrollableViewportSize
(assuming you're wrapping in within aJScrollPane
Then you're doing something wrong
You should never add anything to a
JScrollPane
, it's not how they work, instead, you need to wrap the component within itOr some such...
Yes, that's what
preferredScrollableViewportSize
is doing