6

This JScrollPane based window is a top part of the JSplitPane.

getBounds(), getWidth(), getHeight() all return the full size of the window, including the invisible (scrollable) part.

I want to know the dimensions of the visible part only.

mckeed
  • 9,719
  • 2
  • 37
  • 41
Flot2011
  • 4,601
  • 3
  • 44
  • 61

3 Answers3

9

This is an example which print the height and width of the visible part only ,

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class TestWidth {

    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);
    }
}
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
5

you have look at JViewport, you can derive JViewport from JScrollPane

mKorbel
  • 109,525
  • 20
  • 134
  • 319
4

I think you are looking for JComponent#getVisibleRect().

Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()), and all of its ancestors' visible rectangles.

xehpuk
  • 7,814
  • 3
  • 30
  • 54