Java Swing How to - Get the dimensions of the visible part of the JScrollPane based window








Question

We would like to know how to get the dimensions of the visible part of the JScrollPane based window.

Answer

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
//from  w  w  w.  j  a  v a 2s  . c om
public class Main {

  public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextPane newsTextPane = new JTextPane();
    newsTextPane.setEditable(false);

    JScrollPane scrollPane = new JScrollPane(newsTextPane);

    frame.add(scrollPane);
    frame.setSize(300, 250);
    frame.setVisible(true);

    System.out.println("Height : " + scrollPane.getViewport().getSize().height
        + "\nWidth :" + scrollPane.getViewport().getSize().width);
  }
}