Leon
Leon

Reputation: 97

JComponent height before painting

I have a Jpanel where I add many Components (lets say 100 JButtons). When I now request the height of the added Components there are all 0. Here come Code:

void AddComponents(){

//add 100 Buttons to my jpanel

for (Component component : jpanel.getComponents()) {

      Rectangle test = component.getBounds();

      DebugTextArea.append("\nx: " + test.x + "- y:" + test.y + 
                          "- height: " + test.height + "- width: " + test.width);
        }

}

All values are 0. I know it hase something to do with the fact that the painting of the components will take some time, but how can I get the height becouse I will need the height of the Components to set the VerticalScrollBar to a certain point.

any idea?

Upvotes: 1

Views: 119

Answers (1)

kleopatra
kleopatra

Reputation: 51535

I will need the height of the Components to set the VerticalScrollBar - no you don't (assuming that's the scrollbar of a JScrollPane which contains the panel with the buttons

  button.scrollRectToVisible(button.getBounds())

after the component is realized (aka: shown)

Upvotes: 5

Related Questions