简体   繁体   中英

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?

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 (eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM