• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access Swing stuffs from another class

 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I access to a jLabel of a class from another class or edit its text?
 
Vince Valentin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a public method within the class you have your JLabel object, something like

 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I have a lot of graphical items.

How can I unlock variables declaration section in NetBeans? I think it is a much more easier solution to make elements public.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "unlock"? What is the connection to NetBeans?

If you need to access GUI elements from other classes it might be that the design could be improved. Normally you would not want to access GUI elements from all over the place.
 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variable declarations are locked for modifications. All variables are declared as private. I would like to modify some to public, so I could modify them from another class.
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't mess about with the variables like that. If this other class (or more precisely, an object of another class) needs to work on an object (notice I said object, not variable) then your controller could pass a reference to that object to the object of the other class when it creates it.

There are many other possible solutions other than just making variables public, which is probably the worst in terms of design. Perhaps you could show us your design, i.e. the classes which you have created, and explain their relationship?
 
Kovacs Akos
Ranch Hand
Posts: 86
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is ok now, I can access to my jlabels from another class seemly, but the modifications are not working. So if I use jlabelx.setText in the other class it is not working in the main class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!