Java – how to get the size of the visible part of a JScrollPane based window

This JScrollPane - based window is the top - level part of jsplitpane

Getbounds(), getwidth(), getheight() return the full size of the window, including the invisible (scrollable) part

I just want to know the size of the visible part

Solution

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

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);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>