2010-03-30 58 views
3

我已經創建了JTextpane並在textpane(組件)(例如Jtextarea)中插入了組件。 (垂直滾動條)當我在該JTextpane中插入新組件時,JTextpane的Jscrollpane自動設置爲底部。我想保持它被設置到最高的位置。我怎樣才能做到這一點如何調整滾動窗格中的滾動位置

感謝 蘇尼爾·庫馬爾Sahoo

回答

7

這是我使用的實用程序類。它可以用於滾動到JScrollPane的頂部,底部,左側,右側或水平/垂直中心。

public final class ScrollUtil { 
    public static final int NONE = 0, TOP = 1, VCENTER = 2, BOTTOM = 4, LEFT = 8, HCENTER = 16, RIGHT = 32; 
    private static final int OFFSET = 100; // Required for hack (see below). 

    private ScrollUtil() { 
    } 

    /** 
    * Scroll to specified location. e.g. <tt>scroll(component, BOTTOM);</tt>. 
    * 
    * @param c JComponent to scroll. 
    * @param part Location to scroll to. Should be a bit-wise OR of one or moe of the values: 
    * NONE, TOP, VCENTER, BOTTOM, LEFT, HCENTER, RIGHT. 
    */ 
    public static void scroll(JComponent c, int part) { 
     scroll(c, part & (LEFT|HCENTER|RIGHT), part & (TOP|VCENTER|BOTTOM)); 
    } 

    /** 
    * Scroll to specified location. e.g. <tt>scroll(component, LEFT, BOTTOM);</tt>. 
    * 
    * @param c JComponent to scroll. 
    * @param horizontal Horizontal location. Should take the value: LEFT, HCENTER or RIGHT. 
    * @param vertical Vertical location. Should take the value: TOP, VCENTER or BOTTOM. 
    */ 
    public static void scroll(JComponent c, int horizontal, int vertical) { 
     Rectangle visible = c.getVisibleRect(); 
     Rectangle bounds = c.getBounds(); 

     switch (vertical) { 
      case TOP:  visible.y = 0; break; 
      case VCENTER: visible.y = (bounds.height - visible.height)/2; break; 
      case BOTTOM: visible.y = bounds.height - visible.height + OFFSET; break; 
     } 

     switch (horizontal) { 
      case LEFT: visible.x = 0; break; 
      case HCENTER: visible.x = (bounds.width - visible.width)/2; break; 
      case RIGHT: visible.x = bounds.width - visible.width + OFFSET; break; 
     } 

     // When scrolling to bottom or right of viewport, add an OFFSET value. 
     // This is because without this certain components (e.g. JTable) would 
     // not scroll right to the bottom (presumably the bounds calculation 
     // doesn't take the table header into account. It doesn't matter if 
     // OFFSET is a huge value (e.g. 10000) - the scrollRectToVisible method 
     // still works correctly. 

     c.scrollRectToVisible(visible); 
    } 
} 
1

有,你可以使用各種方法,取決於什麼是滾動窗格內。見the tutorial,最後一節。

5

我發現,要做到這一點最簡單的方法是:

public void scroll(int vertical) {   
    switch (vertical) { 
     case SwingConstants.TOP: 
      getVerticalScrollBar().setValue(0); 
      break; 
     case SwingConstants.CENTER: 
      getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum()); 
      getVerticalScrollBar().setValue(getVerticalScrollBar().getValue()/2); 
      break; 
     case SwingConstants.BOTTOM: 
      getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum()); 
      break; 
    } 
} 

我把這個在擴展JScrollPane的對象,但你也可以全部getVertivalScrollBar()之前添加JScrollPane的名稱。對於CENTER,有兩個setValue() s,因爲getMaximum()返回JScrollBar的底部,而不是其最後的值。這也適用於使用getHorizontalScrollBar()代替getverticalScrollBar()的水平滾動。

0

jScrollPane.getVerticalScrollBar()。setValue(1);