Code example for Scrollbar

Methods: getPreferredSizesetBoundsgetValuesetValues

0
// with additional replacement of deprecated methods 
    public void setBounds(int x, int y, int w, int h) {
 
        super.setBounds(x, y, w, h);
 
        iSbHeight = sbHoriz.getPreferredSize().height;
        iSbWidth  = sbVert.getPreferredSize().width;
        iHeight   = h - iSbHeight;
        iWidth    = w - iSbWidth;
 
        sbHoriz.setBounds(0, iHeight, iWidth, iSbHeight);
        sbVert.setBounds(iWidth, 0, iSbWidth, iHeight);
        adjustScroll(); 
 
        iImage = null;
 
        repaint();
    } 
 
    /** 
     * Method declaration 
     * 
     */ 
    public void removeAll() { 
 
        vData     = new Vector();
        iRowCount = 0;
 
        adjustScroll(); 
 
        iMaxTextLength = 10;
 
        repaint();
    } 
 
    /** 
     * Method declaration 
     * 
     * 
     * @param key 
     * @param value 
     * @param state 
     * @param color 
     */ 
    public void addRow(String key, String value, String state, int color) {
 
        String[] row = new String[4];
 
        if (value == null) {
            value = "";
        } 
 
        row[0] = key;
        row[1] = value;
        row[2] = state;    // null / "-" / "+"
        row[3] = String.valueOf(color);
 
        vData.addElement(row);
 
        int len = fMetrics.stringWidth(value);
 
        if (len > iMaxTextLength) {
            iMaxTextLength = len;
        } 
 
        iRowCount++;
    } 
 
    /** 
     * Method declaration 
     * 
     * 
     * @param key 
     * @param value 
     */ 
    public void addRow(String key, String value) {
        addRow(key, value, null, 0);
    } 
 
    /** 
     * Method declaration 
     * 
     */ 
    public void update() { 
        adjustScroll(); 
        repaint();
    } 
 
    /** 
     * Method declaration 
     * 
     */ 
    void adjustScroll() { 
 
        iTreeHeight = iRowHeight * (iRowCount + 1);
 
        // correct would be iMaxTextLength + iMaxIndent*iIndentWidth 
        iTreeWidth = iMaxTextLength * 2;
 
        sbHoriz.setValues(iX, iWidth, 0, iTreeWidth);
 
        int v = iY / iRowHeight,
            h = iHeight / iRowHeight;
 
        sbVert.setValues(v, h, 0, iRowCount + 1);
 
        iX = sbHoriz.getValue();
        iY = iRowHeight * sbVert.getValue();
    } 
 
    /** 
     * Method declaration 
Connect your IDE to all the code out there  Get Codota for Java