下面列出了javax.swing.JScrollPane#getHorizontalScrollBar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void componentMoved(ComponentEvent e) {
if(timer == null) {
JScrollPane scrollPane = getScrollPane();
if(scrollPane == null) {
change();
} else {
scrollBar = scrollPane.getVerticalScrollBar();
if(scrollBar == null ||
!scrollBar.getValueIsAdjusting()) {
// Try the horizontal scrollbar.
if((scrollBar = scrollPane.getHorizontalScrollBar())
!= null && scrollBar.getValueIsAdjusting()) {
startTimer();
} else {
change();
}
} else {
startTimer();
}
}
}
}
@Override
protected void configureEnclosingScrollPane() {
// Si cette table est la viewportView d'un JScrollPane (la situation habituelle),
// configure ce ScrollPane en positionnant la barre verticale à "always"
// (et en installant le tableHeader comme columnHeaderView).
super.configureEnclosingScrollPane();
final Container parent = getParent();
if (parent instanceof JViewport && parent.getParent() instanceof JScrollPane) {
final JScrollPane scrollPane = (JScrollPane) parent.getParent();
if (scrollPane.getVerticalScrollBar() != null) {
scrollPane.getVerticalScrollBar().setFocusable(false);
}
if (scrollPane.getHorizontalScrollBar() != null) {
scrollPane.getHorizontalScrollBar().setFocusable(false);
}
final JViewport viewport = scrollPane.getViewport();
if (viewport == null || viewport.getView() != this) {
return;
}
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
}
@Override
protected void installDefaults(JScrollPane scrollpane) {
Border b = scrollpane.getBorder();
UIDefaults uidefaults = XToolkit.getUIDefaults();
scrollpane.setBorder(uidefaults.getBorder("ScrollPane.border"));
scrollpane.setBackground(uidefaults.getColor("ScrollPane.background"));
scrollpane.setViewportBorder(uidefaults.getBorder("TextField.border"));
JScrollBar vsb = scrollpane.getVerticalScrollBar();
if (vsb != null) {
if (isLeftToRight(scrollpane)) {
vsbBorder = new CompoundBorder(vsbMarginBorderR,
vsb.getBorder());
}
else {
vsbBorder = new CompoundBorder(vsbMarginBorderL,
vsb.getBorder());
}
vsb.setBorder(vsbBorder);
}
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (hsb != null) {
hsbBorder = new CompoundBorder(hsbMarginBorder, hsb.getBorder());
hsb.setBorder(hsbBorder);
}
}
@Override
protected void installDefaults(JScrollPane scrollpane) {
Border b = scrollpane.getBorder();
UIDefaults uidefaults = XToolkit.getUIDefaults();
scrollpane.setBorder(uidefaults.getBorder("ScrollPane.border"));
scrollpane.setBackground(uidefaults.getColor("ScrollPane.background"));
scrollpane.setViewportBorder(uidefaults.getBorder("TextField.border"));
JScrollBar vsb = scrollpane.getVerticalScrollBar();
if (vsb != null) {
if (isLeftToRight(scrollpane)) {
vsbBorder = new CompoundBorder(vsbMarginBorderR,
vsb.getBorder());
}
else {
vsbBorder = new CompoundBorder(vsbMarginBorderL,
vsb.getBorder());
}
vsb.setBorder(vsbBorder);
}
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
if (hsb != null) {
hsbBorder = new CompoundBorder(hsbMarginBorder, hsb.getBorder());
hsb.setBorder(hsbBorder);
}
}
protected void syncScrollBars() {
JScrollPane jscrollPaneLeft = getjscrollPaneLeft();
JScrollPane jscrollPaneRight = getjscrollPaneRight();
JScrollBar jscrollBarLeftH = jscrollPaneLeft.getHorizontalScrollBar();
JScrollBar jscrollBarLeftV = jscrollPaneLeft.getVerticalScrollBar();
JScrollBar jscrollBarRightH = jscrollPaneRight.getHorizontalScrollBar();
JScrollBar jscrollBarRightV = jscrollPaneRight.getVerticalScrollBar();
jscrollBarRightH.setModel(jscrollBarLeftH.getModel());
jscrollBarRightV.setModel(jscrollBarLeftV.getModel());
}
/**
* Specify how the SmartScroller will function.
*
* @param scrollPane the scroll pane to monitor
* @param scrollDirection indicates which JScrollBar to monitor.
* Valid values are HORIZONTAL and VERTICAL.
* @param viewportPosition indicates where the viewport will normally be
* positioned as data is added.
* Valid values are START and END
*/
public SmartScroller(JScrollPane scrollPane, int scrollDirection, int viewportPosition)
{
if (scrollDirection != HORIZONTAL
&& scrollDirection != VERTICAL)
throw new IllegalArgumentException("invalid scroll direction specified");
if (viewportPosition != START
&& viewportPosition != END)
throw new IllegalArgumentException("invalid viewport position specified");
this.viewportPosition = viewportPosition;
if (scrollDirection == HORIZONTAL)
scrollBar = scrollPane.getHorizontalScrollBar();
else
scrollBar = scrollPane.getVerticalScrollBar();
scrollBar.addAdjustmentListener( this );
// Turn off automatic scrolling for text components
Component view = scrollPane.getViewport().getView();
if (view instanceof JTextComponent)
{
JTextComponent textComponent = (JTextComponent)view;
DefaultCaret caret = (DefaultCaret)textComponent.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
}
}
public @Override void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
if ("document".equals(propName)) { // NOI18N
BaseDocument newDoc = (evt.getNewValue() instanceof BaseDocument)
? (BaseDocument)evt.getNewValue() : null;
modelChanged(listenDoc, newDoc);
} else if (EditorUI.OVERWRITE_MODE_PROPERTY.equals(propName)) {
Boolean b = (Boolean)evt.getNewValue();
overwriteMode = (b != null) ? b.booleanValue() : false;
updateType();
} else if ("ancestor".equals(propName) && evt.getSource() == component) { // NOI18N
// The following code ensures that when the width of the line views
// gets computed on background after the file gets opened
// (so the horizontal scrollbar gets added after several seconds
// for larger files) that the suddenly added horizontal scrollbar
// will not hide the caret laying on the last line of the viewport.
// A component listener gets installed into horizontal scrollbar
// and if it's fired the caret's bounds will be checked whether
// they intersect with the horizontal scrollbar
// and if so the view will be scrolled.
Container parent = component.getParent();
if (parent instanceof JViewport) {
parent = parent.getParent(); // parent of viewport
if (parent instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane)parent;
JScrollBar hScrollBar = scrollPane.getHorizontalScrollBar();
if (hScrollBar != null) {
// Add weak listener so that editor pane could be removed
// from scrollpane without being held by scrollbar
hScrollBar.addComponentListener(
(ComponentListener)WeakListeners.create(
ComponentListener.class, listenerImpl, hScrollBar));
}
}
}
} else if ("enabled".equals(propName)) {
Boolean enabled = (Boolean) evt.getNewValue();
if(component.isFocusOwner()) {
if(enabled == Boolean.TRUE) {
if(component.isEditable()) {
setVisible(true);
}
setSelectionVisible(true);
} else {
setVisible(false);
setSelectionVisible(false);
}
}
} else if (RECTANGULAR_SELECTION_PROPERTY.equals(propName)) {
boolean origRectangularSelection = rectangularSelection;
rectangularSelection = Boolean.TRUE.equals(component.getClientProperty(RECTANGULAR_SELECTION_PROPERTY));
if (rectangularSelection != origRectangularSelection) {
if (rectangularSelection) {
setRectangularSelectionToDotAndMark();
RectangularSelectionTransferHandler.install(component);
} else { // No rectangular selection
RectangularSelectionTransferHandler.uninstall(component);
}
fireStateChanged();
}
}
}
public @Override void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
JTextComponent c = component;
if ("document".equals(propName)) { // NOI18N
if (c != null) {
modelChanged(activeDoc, c.getDocument());
}
} else if (EditorUtilities.CARET_OVERWRITE_MODE_PROPERTY.equals(propName)) {
Boolean b = (Boolean) evt.getNewValue();
overwriteMode = (b != null) ? b : false;
updateOverwriteModeLayer(true);
updateType();
} else if ("ancestor".equals(propName) && evt.getSource() == component) { // NOI18N
// The following code ensures that when the width of the line views
// gets computed on background after the file gets opened
// (so the horizontal scrollbar gets added after several seconds
// for larger files) that the suddenly added horizontal scrollbar
// will not hide the caret laying on the last line of the viewport.
// A component listener gets installed into horizontal scrollbar
// and if it's fired the caret's bounds will be checked whether
// they intersect with the horizontal scrollbar
// and if so the view will be scrolled.
final JViewport viewport = getViewport();
if (viewport != null) {
Component parent = viewport.getParent();
if (parent instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) parent;
JScrollBar hScrollBar = scrollPane.getHorizontalScrollBar();
if (hScrollBar != null) {
// Add weak listener so that editor pane could be removed
// from scrollpane without being held by scrollbar
hScrollBar.addComponentListener(
(ComponentListener) WeakListeners.create(
ComponentListener.class, listenerImpl, hScrollBar));
}
}
}
} else if ("enabled".equals(propName)) {
Boolean enabled = (Boolean) evt.getNewValue();
if (component.isFocusOwner()) {
if (enabled == Boolean.TRUE) {
if (component.isEditable()) {
setVisible(true);
}
setSelectionVisible(true);
} else {
setVisible(false);
setSelectionVisible(false);
}
}
} else if (RECTANGULAR_SELECTION_PROPERTY.equals(propName)) {
boolean origRectangularSelection = rectangularSelection;
rectangularSelection = Boolean.TRUE.equals(component.getClientProperty(RECTANGULAR_SELECTION_PROPERTY));
if (rectangularSelection != origRectangularSelection) {
if (rectangularSelection) {
setRectangularSelectionToDotAndMark();
// RectangularSelectionTransferHandler.install(component);
} else { // No rectangular selection
// RectangularSelectionTransferHandler.uninstall(component);
}
fireStateChanged(null);
}
}
}
public static void themeScrolls(JScrollPane jsp){
if(jsp.getHorizontalScrollBar()!=null)
themedScrollBar(jsp.getHorizontalScrollBar());
if(jsp.getVerticalScrollBar()!=null)
themedScrollBar(jsp.getVerticalScrollBar());
}
public CustomScroll(JComponent component) {
scr = new JScrollPane(component);
scr.setBorder(null);
scr.setViewportBorder(null);
scr.setBorder(BorderFactory.createEmptyBorder());
scr.getViewport().setOpaque(false);
// scr.setOpaque(false);
// scr.setBackground(new Color(0, 0, 0, 5));
verticalScrollBar = scr.getVerticalScrollBar();
verticalScrollBar.setVisible(false);
verticalScrollBar.setOpaque(false);
verticalScrollBar.setUI(new MyScrollBarUI());
verticalScrollBar.setUnitIncrement(16);
horizontalScrollBar = scr.getHorizontalScrollBar();
horizontalScrollBar.setVisible(false);
horizontalScrollBar.setOpaque(false);
horizontalScrollBar.setUI(new MyScrollBarUI());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayer(verticalScrollBar, JLayeredPane.PALETTE_LAYER);
layeredPane.setLayer(horizontalScrollBar, JLayeredPane.PALETTE_LAYER);
scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scr.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scr.setLayout(new ScrollPaneLayout() {
@Override
public void layoutContainer(Container parent) {
viewport.setBounds(0, 0, getWidth(), getHeight());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
displayScrollBarsIfNecessary(viewport);
}
});
}
});
layeredPane.add(horizontalScrollBar);
layeredPane.add(verticalScrollBar);
layeredPane.add(scr);
setLayout(new BorderLayout() {
@Override
public void layoutContainer(Container target) {
super.layoutContainer(target);
int width = getWidth();
int height = getHeight();
scr.setBounds(0, 0, width, height);
int scrollBarSize = 10;
int cornerOffset = verticalScrollBar.isVisible() &&
horizontalScrollBar.isVisible() ? scrollBarSize : 0;
if (verticalScrollBar.isVisible()) {
verticalScrollBar.setBounds(width - scrollBarSize, 0,
scrollBarSize, height - cornerOffset);
}
if (horizontalScrollBar.isVisible()) {
horizontalScrollBar.setBounds(0, height - scrollBarSize,
width - cornerOffset, scrollBarSize);
}
}
});
add(layeredPane, BorderLayout.CENTER);
layeredPane.setBackground(Color.BLUE);
}