How to get the size of the visible part of the JScrollPane window

This JScrollPane window JScrollPane based on the top 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 only the visible part.

+6
source share
3 answers

This is an example that prints 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); } } 
+7
source

you are watching JViewport , you can get JViewpor t from JScrollPane

+4
source

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

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

+2
source

Source: https://habr.com/ru/post/915031/


All Articles