JScrollPane: getVerticalScrollBar() : JScrollPane « javax.swing « Java by API






JScrollPane: getVerticalScrollBar()

  

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cornering Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("+");
    
    JLabel bigLabel = new JLabel("A");
    bigLabel.setPreferredSize(new Dimension(400,400));
    bigLabel.setBorder(new LineBorder(Color.red));
    
    JScrollPane scrollPane = new JScrollPane(bigLabel);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button);
    scrollPane.setColumnHeaderView(new JLabel("B"));
    scrollPane.setRowHeaderView(new JLabel("C"));

    ActionListener actionListener = new JScrollPaneToTopAction(scrollPane);
    button.addActionListener(actionListener);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}


class JScrollPaneToTopAction implements ActionListener {
  JScrollPane scrollPane;
  public JScrollPaneToTopAction(JScrollPane scrollPane) {
    if (scrollPane == null) {
      throw new IllegalArgumentException("JScrollPaneToTopAction: null JScrollPane");
    }
    this.scrollPane = scrollPane;
  }
  public void actionPerformed(ActionEvent actionEvent) {
    JScrollBar verticalScrollBar   = scrollPane.getVerticalScrollBar();
    JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
    verticalScrollBar.setValue(verticalScrollBar.getMinimum());
    horizontalScrollBar.setValue(horizontalScrollBar.getMinimum());
  }
}

           
         
    
  








Related examples in the same category

1.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
2.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
3.new JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
4.JScrollPane: getHorizontalScrollBar()
5.JScrollPane: getHorizontalScrollBarPolicy()
6.JScrollPane: getVerticalScrollBarPolicy()
7.JScrollPane: setColumnHeaderView(Component view)
8.JScrollPane: setCorner(String key, Component corner)
9.JScrollPane: setRowHeaderView(Component com)
10.JScrollPane: setViewportView(Component view)