下面列出了怎么用javax.swing.Scrollable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = super.preferredLayoutSize(parent);
OutlineView ov = (OutlineView) parent;
int thsbPolicy = ov.treeHorizontalScrollBarPolicy;
if ((thsb != null) && (thsbPolicy != HORIZONTAL_SCROLLBAR_NEVER)) {
if (thsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS) {
dim.height += thsb.getPreferredSize().height;
}
else {
Dimension extentSize = null;
Dimension viewSize = null;
Component view = null;
if (viewport != null) {
extentSize = viewport.getPreferredSize();
viewSize = viewport.getViewSize();
view = viewport.getView();
}
if ((viewSize != null) && (extentSize != null)) {
boolean canScroll = true;
if (view instanceof Scrollable) {
canScroll = !((Scrollable)view).getScrollableTracksViewportWidth();
}
if (canScroll && (viewSize.width > extentSize.width)) {
dim.height += thsb.getPreferredSize().height;
}
}
}
}
return dim;
}
private boolean isEditorScrollable(PropertyPanel p) {
Component[] comps = p.getComponents();
for (int i = 0; i < comps.length; i++) {
if (comps[i] instanceof Scrollable || isInstanceOfTopComponent(comps[i])) {
return true;
}
}
return false;
}
private int getDefaultIncrement() {
Component content = scrollPane.getViewport().getView();
Point position = scrollPane.getViewport().getViewPosition();
int increment = isHorizontal ? 30 : 8;
Dimension viewSize = scrollPane.getViewport().getExtentSize();
if( content instanceof Scrollable ) {
increment = ((Scrollable)content).getScrollableUnitIncrement( new Rectangle( position, viewSize ),
isHorizontal ? SwingConstants.HORIZONTAL : SwingConstants.VERTICAL,
isScrollLeft ? -1 : 1 );
}
return increment;
}
private void mouseWheelMoved(MouseWheelEvent e) {
/* The code in this method is taken directly from Pavel Fatin's original IntelliJ patch.
Some formatting changes have been applied. */
/* The shift modifier will be enabled for horizontal touchbar scroll events, even when the
actual shift key is not pressed, on both Windows and MacOS (though not on Java 8 on
Windows). */
JScrollBar scrollbar = e.isShiftDown()
? scrollpane.getHorizontalScrollBar() : scrollpane.getVerticalScrollBar();
int orientation = scrollbar.getOrientation();
JViewport viewport = scrollpane.getViewport();
if (viewport == null || !(viewport.getView() instanceof Scrollable)) {
return;
}
Scrollable view = (Scrollable) viewport.getView();
double rotation = e.getPreciseWheelRotation();
/* Use (0, 0) view position to obtain constant unit increment (which might otherwise be
variable on smaller-than-unit scrolling). */
Rectangle r = new Rectangle(new Point(0, 0), viewport.getViewSize());
int unitIncrement = view.getScrollableUnitIncrement(r, orientation, 1);
double delta = rotation * e.getScrollAmount() * unitIncrement;
boolean limitDelta = Math.abs(rotation) < 1.0d + EPSILON;
int blockIncrement = view.getScrollableBlockIncrement(r, orientation, 1);
double adjustedDelta = limitDelta
? Math.max(-(double) blockIncrement, Math.min(delta, (double) blockIncrement))
: delta;
int value = scrollbar.getValue();
int newValue = Math.max(scrollbar.getMinimum(),
Math.min((int) Math.round(value + adjustedDelta), scrollbar.getMaximum()));
if (newValue != value) {
scrollbar.setValue(newValue);
}
}
public JScrollPane makeScrollPane(Component component) {
// the component is not scrollable, wraps it in a ScrollableJPanel
JScrollPane scroll = new JScrollPane();
scroll.setBorder(BorderFactory.createEmptyBorder());
if (component instanceof Scrollable) {
scroll.getViewport().setView(component);
} else {
scroll.getViewport().setView(new ScrollableJPanel(component));
}
scroll.setOpaque(false);
scroll.getViewport().setOpaque(false);
return scroll;
}
private void mouseWheelMovedSmooth( MouseWheelEvent e ) {
// return if there is no viewport
JViewport viewport = scrollpane.getViewport();
if( viewport == null )
return;
// find scrollbar to scroll
JScrollBar scrollbar = scrollpane.getVerticalScrollBar();
if( scrollbar == null || !scrollbar.isVisible() || e.isShiftDown() ) {
scrollbar = scrollpane.getHorizontalScrollBar();
if( scrollbar == null || !scrollbar.isVisible() )
return;
}
// consume event
e.consume();
// get precise wheel rotation
double rotation = e.getPreciseWheelRotation();
// get unit and block increment
int unitIncrement;
int blockIncrement;
int orientation = scrollbar.getOrientation();
Component view = viewport.getView();
if( view instanceof Scrollable ) {
Scrollable scrollable = (Scrollable) view;
// Use (0, 0) view position to obtain constant unit increment of first item
// (which might otherwise be variable on smaller-than-unit scrolling).
Rectangle visibleRect = new Rectangle( viewport.getViewSize() );
unitIncrement = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
blockIncrement = scrollable.getScrollableBlockIncrement( visibleRect, orientation, 1 );
if( unitIncrement > 0 ) {
// For the case that the first item (e.g. in a list) is larger
// than the other items, get the unit increment of the second item
// and use the smaller one.
if( orientation == SwingConstants.VERTICAL ) {
visibleRect.y += unitIncrement;
visibleRect.height -= unitIncrement;
} else {
visibleRect.x += unitIncrement;
visibleRect.width -= unitIncrement;
}
int unitIncrement2 = scrollable.getScrollableUnitIncrement( visibleRect, orientation, 1 );
if( unitIncrement2 > 0 )
unitIncrement = Math.min( unitIncrement, unitIncrement2 );
}
} else {
int direction = rotation < 0 ? -1 : 1;
unitIncrement = scrollbar.getUnitIncrement( direction );
blockIncrement = scrollbar.getBlockIncrement( direction );
}
// limit scroll amount (number of units to scroll) for small viewports
// (e.g. vertical scrolling in file chooser)
int scrollAmount = e.getScrollAmount();
int viewportWH = (orientation == SwingConstants.VERTICAL)
? viewport.getHeight()
: viewport.getWidth();
if( unitIncrement * scrollAmount > viewportWH )
scrollAmount = Math.max( viewportWH / unitIncrement, 1 );
// compute relative delta
double delta = rotation * scrollAmount * unitIncrement;
boolean adjustDelta = Math.abs( rotation ) < (1.0 + EPSILON);
double adjustedDelta = adjustDelta
? Math.max( -blockIncrement, Math.min( delta, blockIncrement ) )
: delta;
// compute new value
int value = scrollbar.getValue();
double minDelta = scrollbar.getMinimum() - value;
double maxDelta = scrollbar.getMaximum() - scrollbar.getModel().getExtent() - value;
double boundedDelta = Math.max( minDelta, Math.min( adjustedDelta, maxDelta ) );
int newValue = value + (int) Math.round( boundedDelta );
// set new value
if( newValue != value )
scrollbar.setValue( newValue );
/*debug
System.out.println( String.format( "%4d %9f / %4d %4d / %12f %5s %12f / %4d %4d %4d / %12f %12f %12f / %4d",
e.getWheelRotation(),
e.getPreciseWheelRotation(),
unitIncrement,
blockIncrement,
delta,
adjustDelta,
adjustedDelta,
value,
scrollbar.getMinimum(),
scrollbar.getMaximum(),
minDelta,
maxDelta,
boundedDelta,
newValue ) );
*/
}
private void initComponents() {
table = new DetailsTable();
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JViewport viewport = new Viewport(table);
final JScrollPane tableScroll = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tableScroll.setViewport(viewport);
tableScroll.setBorder(BorderFactory.createEmptyBorder());
tableScroll.setViewportBorder(BorderFactory.createEmptyBorder());
tableScroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new HeaderPanel());
scrollBar = new ScrollBar(JScrollBar.VERTICAL) {
public int getUnitIncrement(int direction) {
JViewport vp = tableScroll.getViewport();
Scrollable view = (Scrollable)(vp.getView());
Rectangle vr = vp.getViewRect();
return view.getScrollableUnitIncrement(vr, getOrientation(), direction);
}
public int getBlockIncrement(int direction) {
JViewport vp = tableScroll.getViewport();
Scrollable view = (Scrollable)(vp.getView());
Rectangle vr = vp.getViewRect();
return view.getScrollableBlockIncrement(vr, getOrientation(), direction);
}
public void setValues(int newValue, int newExtent, int newMin, int newMax) {
setEnabled(newExtent < newMax);
if (isEnabled() && !isSelectionChanging() && isTrackingEnd())
newValue = newMax - newExtent;
super.setValues(newValue, newExtent, newMin, newMax);
}
};
tableScroll.setVerticalScrollBar(scrollBar);
dataContainer = tableScroll;
JLabel noDataLabel = new JLabel("<No probe selected>", JLabel.CENTER);
noDataLabel.setEnabled(false);
noDataContainer = new JPanel(new BorderLayout());
noDataContainer.setOpaque(false);
noDataContainer.add(noDataLabel, BorderLayout.CENTER);
setOpaque(false);
setLayout(new BorderLayout());
add(noDataContainer, BorderLayout.CENTER);
}
private void initComponents() {
table = new DetailsTable();
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JViewport viewport = new Viewport(table);
final JScrollPane tableScroll = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tableScroll.setViewport(viewport);
tableScroll.setBorder(BorderFactory.createEmptyBorder());
tableScroll.setViewportBorder(BorderFactory.createEmptyBorder());
tableScroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new HeaderPanel());
scrollBar = new ScrollBar(JScrollBar.VERTICAL) {
public int getUnitIncrement(int direction) {
JViewport vp = tableScroll.getViewport();
Scrollable view = (Scrollable)(vp.getView());
Rectangle vr = vp.getViewRect();
return view.getScrollableUnitIncrement(vr, getOrientation(), direction);
}
public int getBlockIncrement(int direction) {
JViewport vp = tableScroll.getViewport();
Scrollable view = (Scrollable)(vp.getView());
Rectangle vr = vp.getViewRect();
return view.getScrollableBlockIncrement(vr, getOrientation(), direction);
}
public void setValues(int newValue, int newExtent, int newMin, int newMax) {
setEnabled(newExtent < newMax);
if (isEnabled() && !isSelectionChanging() && isTrackingEnd())
newValue = newMax - newExtent;
super.setValues(newValue, newExtent, newMin, newMax);
}
};
tableScroll.setVerticalScrollBar(scrollBar);
dataContainer = tableScroll;
JLabel noDataLabel = new JLabel("<No probe selected>", JLabel.CENTER);
noDataLabel.setEnabled(false);
noDataContainer = new JPanel(new BorderLayout());
noDataContainer.setOpaque(false);
noDataContainer.add(noDataLabel, BorderLayout.CENTER);
setOpaque(false);
setLayout(new BorderLayout());
add(noDataContainer, BorderLayout.CENTER);
}
private void initComponents() {
table = new DetailsTable();
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JViewport viewport = new Viewport(table);
final JScrollPane tableScroll = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tableScroll.setViewport(viewport);
tableScroll.setBorder(BorderFactory.createEmptyBorder());
tableScroll.setViewportBorder(BorderFactory.createEmptyBorder());
tableScroll.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new HeaderPanel());
scrollBar = new ScrollBar(JScrollBar.VERTICAL) {
public int getUnitIncrement(int direction) {
JViewport vp = tableScroll.getViewport();
Scrollable view = (Scrollable)(vp.getView());
Rectangle vr = vp.getViewRect();
return view.getScrollableUnitIncrement(vr, getOrientation(), direction);
}
public int getBlockIncrement(int direction) {
JViewport vp = tableScroll.getViewport();
Scrollable view = (Scrollable)(vp.getView());
Rectangle vr = vp.getViewRect();
return view.getScrollableBlockIncrement(vr, getOrientation(), direction);
}
public void setValues(int newValue, int newExtent, int newMin, int newMax) {
setEnabled(newExtent < newMax);
if (isEnabled() && !isSelectionChanging() && isTrackingEnd())
newValue = newMax - newExtent;
super.setValues(newValue, newExtent, newMin, newMax);
}
};
tableScroll.setVerticalScrollBar(scrollBar);
dataContainer = tableScroll;
JLabel noDataLabel = new JLabel("<No probe selected>", JLabel.CENTER);
noDataLabel.setEnabled(false);
noDataContainer = new JPanel(new BorderLayout());
noDataContainer.setOpaque(false);
noDataContainer.add(noDataLabel, BorderLayout.CENTER);
setOpaque(false);
setLayout(new BorderLayout());
add(noDataContainer, BorderLayout.CENTER);
}