Peck3277
Peck3277

Reputation: 1423

Accessing swing components created in method

I have a class that builds my gui. One part of the gui is a jtabbed pane, the panels for this are created in a method, they are all the same and have a jtextarea and some jbuttons.

How can I access the components in each tab from other methods/classes?

Upvotes: 1

Views: 143

Answers (2)

Attila
Attila

Reputation: 28762

You can use a combination of getTabCount(), getTitleAt(), indexOfTab(), and getComponent(). See more info here

Upvotes: 2

AlexR
AlexR

Reputation: 115328

You can put this components into class fields. For example if you need to access button make field

private Button myButton;

Create it in method createUI() and then use it in method doSomething().

Other way is to store the container only (e.g. panel) and then access its components using panel.getComponents(). Use this method only if you really have very similar components and do not have to distinguish among them. For example you want to change color of all components etc.

Upvotes: 3

Related Questions