下面列出了怎么用javax.swing.JScrollBar的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Dimension getMinimumSize(final int rows, final int columns) {
final Dimension size = super.getMinimumSize(rows, columns);
synchronized (getDelegateLock()) {
// JScrollPane insets
final Insets pi = getDelegate().getInsets();
size.width += pi.left + pi.right;
size.height += pi.top + pi.bottom;
// Take scrollbars into account.
final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();
if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {
final JScrollBar vbar = getDelegate().getVerticalScrollBar();
size.width += vbar != null ? vbar.getMinimumSize().width : 0;
}
final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();
if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {
final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
size.height += hbar != null ? hbar.getMinimumSize().height : 0;
}
}
return size;
}
private boolean setPointerIfPointOverScrollbar( JScrollBar bar, Point point ) {
if ( ! bar.getBounds().contains( point ) ) {
return false;
}
current.setBar( bar );
Point local = toLocalSpace( bar, point );
XTextAreaPeer.XAWTScrollBarUI ui =
(XTextAreaPeer.XAWTScrollBarUI) bar.getUI();
if ( ! setPointerIfPointOverButton( ui.getIncreaseButton(), local ) ) {
setPointerIfPointOverButton( ui.getDecreaseButton(), local );
}
return true;
}
private boolean setPointerIfPointOverScrollbar( JScrollBar bar, Point point ) {
if ( ! bar.getBounds().contains( point ) ) {
return false;
}
current.setBar( bar );
Point local = toLocalSpace( bar, point );
XTextAreaPeer.XAWTScrollBarUI ui =
(XTextAreaPeer.XAWTScrollBarUI) bar.getUI();
if ( ! setPointerIfPointOverButton( ui.getIncreaseButton(), local ) ) {
setPointerIfPointOverButton( ui.getDecreaseButton(), local );
}
return true;
}
@Override
void initializeImpl() {
super.initializeImpl();
final Scrollbar target = getTarget();
setLineIncrement(target.getUnitIncrement());
setPageIncrement(target.getBlockIncrement());
setValues(target.getValue(), target.getVisibleAmount(),
target.getMinimum(), target.getMaximum());
final int orientation = target.getOrientation();
final JScrollBar delegate = getDelegate();
synchronized (getDelegateLock()) {
delegate.setOrientation(orientation == Scrollbar.HORIZONTAL
? Adjustable.HORIZONTAL
: Adjustable.VERTICAL);
delegate.addAdjustmentListener(this);
}
}
@Override
public void insert(final String text, final int pos) {
final ScrollableJTextArea pane = getDelegate();
synchronized (getDelegateLock()) {
final JTextArea area = pane.getView();
final boolean doScroll = pos >= area.getDocument().getLength()
&& area.getDocument().getLength() != 0;
area.insert(text, pos);
revalidate();
if (doScroll) {
final JScrollBar vbar = pane.getVerticalScrollBar();
if (vbar != null) {
vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());
}
}
}
repaintPeer();
}
void scrollByBlock(JScrollBar scrollbar, int direction) {
// This method is called from BasicScrollPaneUI to implement wheel
// scrolling, and also from scrollByBlock().
int oldValue = scrollbar.getValue();
int blockIncrement = scrollbar.getBlockIncrement(direction);
int delta = blockIncrement * ((direction > 0) ? +1 : -1);
int newValue = oldValue + delta;
// Check for overflow.
if (delta > 0 && newValue < oldValue) {
newValue = scrollbar.getMaximum();
} else if (delta < 0 && newValue > oldValue) {
newValue = scrollbar.getMinimum();
}
scrollbar.setValue(newValue);
}
public void validateThird() {
JViewport viewport = this.pane.getViewport();
JScrollBar scroller = this.pane.getHorizontalScrollBar();
if (!scroller.getComponentOrientation().equals(ComponentOrientation.RIGHT_TO_LEFT)) {
throw new Error("unexpected component orientation");
}
int value = scroller.getValue();
if (value != 0) {
throw new Error("unexpected scroll value");
}
int extent = viewport.getExtentSize().width;
if (extent != scroller.getVisibleAmount()) {
throw new Error("unexpected visible amount");
}
int size = viewport.getViewSize().width;
if (size != scroller.getMaximum()) {
throw new Error("unexpected maximum");
}
int pos = size - extent - value;
if (pos != viewport.getViewPosition().x) {
throw new Error("unexpected position");
}
}
@Override
protected void uninstallDefaults(JScrollPane c) {
super.uninstallDefaults(c);
JScrollBar vsb = scrollpane.getVerticalScrollBar();
if (vsb != null) {
if (vsb.getBorder() == vsbBorder) {
vsb.setBorder(null);
}
vsbBorder = null;
}
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (hsb != null) {
if (hsb.getBorder() == hsbBorder) {
hsb.setBorder(null);
}
hsbBorder = null;
}
}
public void run() {
if (this.bar == null) {
this.bar = new JScrollBar(JScrollBar.HORIZONTAL, 50, 10, 0, 100);
this.bar.setPreferredSize(new Dimension(400, 20));
JFrame frame = new JFrame();
frame.add(this.bar);
frame.pack();
frame.setVisible(true);
}
else if (40 != this.bar.getValue()) {
System.out.println("name = " + UIManager.getLookAndFeel().getName());
System.out.println("value = " + this.bar.getValue());
}
else {
SwingUtilities.getWindowAncestor(this.bar).dispose();
this.bar = null;
}
}
@Override
public Dimension getMinimumSize(final int rows, final int columns) {
final Dimension size = super.getMinimumSize(rows, columns);
synchronized (getDelegateLock()) {
// JScrollPane insets
final Insets pi = getDelegate().getInsets();
size.width += pi.left + pi.right;
size.height += pi.top + pi.bottom;
// Take scrollbars into account.
final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();
if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {
final JScrollBar vbar = getDelegate().getVerticalScrollBar();
size.width += vbar != null ? vbar.getMinimumSize().width : 0;
}
final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();
if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {
final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
size.height += hbar != null ? hbar.getMinimumSize().height : 0;
}
}
return size;
}
private boolean setPointerIfPointOverScrollbar( JScrollBar bar, Point point ) {
if ( ! bar.getBounds().contains( point ) ) {
return false;
}
current.setBar( bar );
Point local = toLocalSpace( bar, point );
XTextAreaPeer.XAWTScrollBarUI ui =
(XTextAreaPeer.XAWTScrollBarUI) bar.getUI();
if ( ! setPointerIfPointOverButton( ui.getIncreaseButton(), local ) ) {
setPointerIfPointOverButton( ui.getDecreaseButton(), local );
}
return true;
}
@Override
public int getScrollDirection(JScrollBarOperator oper) {
Point toPoint = SwingUtilities.
convertPoint(comp, x, y, getViewport().getView());
int to = (orientation == JScrollBar.HORIZONTAL) ? toPoint.x : toPoint.y;
int ln = (orientation == JScrollBar.HORIZONTAL) ? width : height;
int lv = (orientation == JScrollBar.HORIZONTAL) ? getViewport().getWidth() : getViewport().getHeight();
int vl = oper.getValue();
if (to < vl) {
return ScrollAdjuster.DECREASE_SCROLL_DIRECTION;
} else if ((to + ln - 1) > (vl + lv)
&& to > vl) {
return ScrollAdjuster.INCREASE_SCROLL_DIRECTION;
} else {
return ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION;
}
}
private boolean setPointerIfPointOverScrollbar( JScrollBar bar, Point point ) {
if ( ! bar.getBounds().contains( point ) ) {
return false;
}
current.setBar( bar );
Point local = toLocalSpace( bar, point );
XTextAreaPeer.XAWTScrollBarUI ui =
(XTextAreaPeer.XAWTScrollBarUI) bar.getUI();
if ( ! setPointerIfPointOverButton( ui.getIncreaseButton(), local ) ) {
setPointerIfPointOverButton( ui.getDecreaseButton(), local );
}
return true;
}
@Override
protected void uninstallDefaults(JScrollPane c) {
super.uninstallDefaults(c);
JScrollBar vsb = scrollpane.getVerticalScrollBar();
if (vsb != null) {
if (vsb.getBorder() == vsbBorder) {
vsb.setBorder(null);
}
vsbBorder = null;
}
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (hsb != null) {
if (hsb.getBorder() == hsbBorder) {
hsb.setBorder(null);
}
hsbBorder = null;
}
}
public void run() {
if (this.bar == null) {
this.bar = new JScrollBar(JScrollBar.HORIZONTAL, 50, 10, 0, 100);
this.bar.setPreferredSize(new Dimension(400, 20));
JFrame frame = new JFrame();
frame.add(this.bar);
frame.pack();
frame.setVisible(true);
}
else if (40 != this.bar.getValue()) {
System.out.println("name = " + UIManager.getLookAndFeel().getName());
System.out.println("value = " + this.bar.getValue());
}
else {
SwingUtilities.getWindowAncestor(this.bar).dispose();
this.bar = null;
}
}
private static void init(Container container) {
container.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 1;
JLabel label = new JLabel();
Dimension size = new Dimension(111, 0);
label.setPreferredSize(size);
label.setMinimumSize(size);
container.add(label, gbc);
gbc.gridx = 1;
gbc.weightx = 1;
container.add(new JScrollBar(JScrollBar.HORIZONTAL, 1, 111, 1, 1111), gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 0;
gbc.weighty = 1;
container.add(new JScrollBar(JScrollBar.VERTICAL, 1, 111, 1, 1111), gbc);
}
public JScrollPopupMenu(String label) {
super(label);
setLayout(new ScrollPopupMenuLayout());
super.add(getScrollBar());
addMouseWheelListener(new MouseWheelListener() {
@Override public void mouseWheelMoved(MouseWheelEvent event) {
JScrollBar scrollBar = getScrollBar();
int amount = (event.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
? event.getUnitsToScroll() * scrollBar.getUnitIncrement()
: (event.getWheelRotation() < 0 ? -1 : 1) * scrollBar.getBlockIncrement();
scrollBar.setValue(scrollBar.getValue() + amount);
event.consume();
}
});
}
@Override
public Dimension getMinimumSize(final int rows, final int columns) {
final Dimension size = super.getMinimumSize(rows, columns);
synchronized (getDelegateLock()) {
// JScrollPane insets
final Insets pi = getDelegate().getInsets();
size.width += pi.left + pi.right;
size.height += pi.top + pi.bottom;
// Take scrollbars into account.
final int vsbPolicy = getDelegate().getVerticalScrollBarPolicy();
if (vsbPolicy == ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS) {
final JScrollBar vbar = getDelegate().getVerticalScrollBar();
size.width += vbar != null ? vbar.getMinimumSize().width : 0;
}
final int hsbPolicy = getDelegate().getHorizontalScrollBarPolicy();
if (hsbPolicy == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS) {
final JScrollBar hbar = getDelegate().getHorizontalScrollBar();
size.height += hbar != null ? hbar.getMinimumSize().height : 0;
}
}
return size;
}
@Override
public void insert(final String text, final int pos) {
final ScrollableJTextArea pane = getDelegate();
synchronized (getDelegateLock()) {
final JTextArea area = pane.getView();
final boolean doScroll = pos >= area.getDocument().getLength()
&& area.getDocument().getLength() != 0;
area.insert(text, pos);
revalidate();
if (doScroll) {
final JScrollBar vbar = pane.getVerticalScrollBar();
if (vbar != null) {
vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());
}
}
}
repaintPeer();
}
@Override
protected void uninstallDefaults(JScrollPane c) {
super.uninstallDefaults(c);
JScrollBar vsb = scrollpane.getVerticalScrollBar();
if (vsb != null) {
if (vsb.getBorder() == vsbBorder) {
vsb.setBorder(null);
}
vsbBorder = null;
}
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (hsb != null) {
if (hsb.getBorder() == hsbBorder) {
hsb.setBorder(null);
}
hsbBorder = null;
}
}
public void run() {
if (this.bar == null) {
this.bar = new JScrollBar(JScrollBar.HORIZONTAL, 50, 10, 0, 100);
this.bar.setPreferredSize(new Dimension(400, 20));
JFrame frame = new JFrame();
frame.add(this.bar);
frame.pack();
frame.setVisible(true);
}
else if (40 != this.bar.getValue()) {
System.out.println("name = " + UIManager.getLookAndFeel().getName());
System.out.println("value = " + this.bar.getValue());
}
else {
SwingUtilities.getWindowAncestor(this.bar).dispose();
this.bar = null;
}
}
@Override
protected void uninstallDefaults(JScrollPane c) {
super.uninstallDefaults(c);
JScrollBar vsb = scrollpane.getVerticalScrollBar();
if (vsb != null) {
if (vsb.getBorder() == vsbBorder) {
vsb.setBorder(null);
}
vsbBorder = null;
}
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (hsb != null) {
if (hsb.getBorder() == hsbBorder) {
hsb.setBorder(null);
}
hsbBorder = null;
}
}
@Override
void initializeImpl() {
super.initializeImpl();
final Scrollbar target = getTarget();
setLineIncrement(target.getUnitIncrement());
setPageIncrement(target.getBlockIncrement());
setValues(target.getValue(), target.getVisibleAmount(),
target.getMinimum(), target.getMaximum());
final int orientation = target.getOrientation();
final JScrollBar delegate = getDelegate();
synchronized (getDelegateLock()) {
delegate.setOrientation(orientation == Scrollbar.HORIZONTAL
? Adjustable.HORIZONTAL
: Adjustable.VERTICAL);
delegate.addAdjustmentListener(this);
}
}
@Override
public void insert(final String text, final int pos) {
final ScrollableJTextArea pane = getDelegate();
synchronized (getDelegateLock()) {
final JTextArea area = pane.getView();
final boolean doScroll = pos >= area.getDocument().getLength()
&& area.getDocument().getLength() != 0;
area.insert(text, pos);
revalidate();
if (doScroll) {
final JScrollBar vbar = pane.getVerticalScrollBar();
if (vbar != null) {
vbar.setValue(vbar.getMaximum() - vbar.getVisibleAmount());
}
}
}
repaintPeer();
}
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);
}
}
private JScrollBar getScroller(int column) {
for (Component component : scrollersPanel.getComponents()) {
JScrollBar scroller = (JScrollBar)component;
if (getColumn(scroller) == column) return scroller;
}
return null;
}
/**
* Maps {@code JScrollPane.setHorizontalScrollBar(JScrollBar)} through queue
*/
public void setHorizontalScrollBar(final JScrollBar jScrollBar) {
runMapping(new MapVoidAction("setHorizontalScrollBar") {
@Override
public void map() {
((JScrollPane) getSource()).setHorizontalScrollBar(jScrollBar);
}
});
}
/**
* @see java.awt.peer.TextAreaPeer
*/
@Override
public Dimension getMinimumSize(int rows, int cols) {
/* Dimension d = null;
if (jtext != null) {
d = jtext.getMinimumSize(rows,cols);
}
return d;
*/
int vsbwidth=0;
int hsbheight=0;
JScrollBar vsb = textPane.getVerticalScrollBar();
if (vsb != null) {
vsbwidth = vsb.getMinimumSize().width;
}
JScrollBar hsb = textPane.getHorizontalScrollBar();
if (hsb != null) {
hsbheight = hsb.getMinimumSize().height;
}
Font f = jtext.getFont();
FontMetrics fm = jtext.getFontMetrics(f);
return new Dimension(fm.charWidth('0') * cols + /*2*XMARGIN +*/ vsbwidth,
fm.getHeight() * rows + /*2*YMARGIN +*/ hsbheight);
}
public Dimension getPreferredSize( JComponent c )
{
if ( scrollbar.getOrientation() == JScrollBar.VERTICAL )
{
return new Dimension( scrollBarWidth, scrollBarWidth * 3 + 10 );
}
else // Horizontal
{
return new Dimension( scrollBarWidth * 3 + 10, scrollBarWidth );
}
}
private static void scroll(JScrollBar scrollBar, MouseWheelEvent e) {
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
int unitsToScroll = e.getUnitsToScroll();
int direction = unitsToScroll < 0 ? -1 : 1;
if (unitsToScroll != 0) {
int increment = scrollBar.getUnitIncrement(direction);
int oldValue = scrollBar.getValue();
int newValue = oldValue + increment * unitsToScroll;
newValue = Math.max(Math.min(newValue, scrollBar.getMaximum() -
scrollBar.getVisibleAmount()), scrollBar.getMinimum());
if (oldValue != newValue) scrollBar.setValue(newValue);
}
}
}