JPanel を含む JScrollPane があります。この JPanel にたくさんのボタンを詰め込みます。
現在表示されているボタンを取得する可能性はありますか?
経由で JPanel の子にアクセスできることはわかっていますjpanel.getComponents()
が、それらはすべてこのペインのコンポーネントです。現在画面に表示されているものだけが必要です。
JPanel を含む JScrollPane があります。この JPanel にたくさんのボタンを詰め込みます。
現在表示されているボタンを取得する可能性はありますか?
経由で JPanel の子にアクセスできることはわかっていますjpanel.getComponents()
が、それらはすべてこのペインのコンポーネントです。現在画面に表示されているものだけが必要です。
@mKorbelの回答にすでにコメントしたように:
JComponents には、現在表示されている方法/場所とは関係なく、現在表示されている部分を取得するための API があるため、「何か」は JComponent の visibleRect です。
Rectangle visibleRect = myPanel.getVisibleRect();
for (Component child : myPanel.getComponents()) {
Rectangle childBounds = child.getBounds();
if (childBounds.intersects(visibleRect)) {
// do stuff
}
}
このコンテナはすでに画面に表示されていると想定しています。
1) JScrollPaneからJViewPortを抽出するには、
2)にChangeListenerを追加JViewPort
3) 可視の各JComponent(s)
戻り値Rectangle
4) Rectangle#intersectsは、可視か不可視かのBoolean
値を返します。JComponent(s)
JViewPort
コンポーネントが表示されているかどうかを尋ねるのはどうですか。
for ( Component component : jpanel.getComponents() ) {
if ( component instanceof JButton && component.isShowing() ) {
// We've found a button that is showing...
}
}
scrollPane.getViewport().getView()
scrollPane.getViewport().getViewRect()