Link to home
Start Free TrialLog in
Avatar of static86
static86

asked on

set progress bar from a different class

Is it possible to access GUI component like jProgressBar from a different class?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

sure, all you need is a reference to the JProgressBar instance
For example you could add a getter to your class to allow other classes to access it

public class MyPanel extends JPanel {
   private JProgressBar bar = new JProgressBar();

   public JProgressBar getProgressBar() {
       return bar;
   }

   ....


Then from another class you could access it using the getter


JProgressBar bar = mypanel.getProgressBar();
// do what you need to the progress bar

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of static86
static86

ASKER

thanks objects,
I have created jProgressBar within Netbeans and it is already in mainPanel [JPanel] so is it possible to make progressBar public and refer to that (or something similar)?
you can make it public but its better design to add a (public) getter to access it as I showed above
So how can I show this progress bar on my mainPanel? I can't do drag&drop in netbeans now.
thanks
Thats the problem with using netbeans and its gui builder. it takes a lot of the control away from you.
if you're using netbeans then create your gui with netbeans and once you're done add methods to provide any interaction between classes that you need.