Search code examples
javauser-interfaceprogress-barnetbeans-8

Why can't I set the value of a progress bar in another class?


I am trying to set the value of the progress bars in math1a and math2a through math1a, but only the value of the progress bar in math1a is set and the one in the math2a class didn't. I tried using set and get method for the matha2 progressbar but it doesn't seem to let me set it's value?

///////////////////////////////////////////////////////////////////////////math1 main class//////////////////////////////////////////////////////

public class math1 extends javax.swing.JFrame {


public math1() {
    initComponents();
}


@SuppressWarnings("unchecked")
 private void connectActionPerformed(java.awt.event.ActionEvent evt) {                                        

    math1a m1 = new math1a();
    m1.setVisible(true);
    m1.pbStart();

    math2a m2 = new math2a();
    m2.setVisible(true);
}                                       


public static void main(String args[]) {
    /* Set the Nimbus look and feel */
/* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new math1().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton connect;
// End of variables declaration                   
}

///////////////////////////////////////////////////////////////////////////math1a class//////////////////////////////////////////////////////////////

public class math1a extends javax.swing.JFrame {


public math1a() {
    initComponents();
}


@SuppressWarnings("unchecked")
math2a m2 = new math2a();

public void pbStart(){
    //attempt 1
    pb1.setValue(100);
    m2.setPB(100);
   m2.pb2.setValue(m2.getPB());
   //attempt 2
   m2.setBar(100);

}

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
 /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new math1a().setVisible(true);
        }
    });
 }

// Variables declaration - do not modify                     
private javax.swing.JProgressBar pb1;
// End of variables declaration                   
}

///////////////////////////////////////////////////////////////////////////math2a class//////////////////////////////////////////////////////////////

public class math2a extends javax.swing.JFrame {

private int bar;

public math2a() {
    initComponents();
}

@SuppressWarnings("unchecked")

//attempt 1
public void setPB(int bar){
    this.bar = bar;
}

public int getPB(){
    return this.bar;
}

 //attemp2
 public void setBar(int pb2){
   this.pb2.setValue(pb2);
}

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
 /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new math2a().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
public javax.swing.JProgressBar pb2;
// End of variables declaration                   
}

Solution

  • In connectActionPerformed you create an instance of math2a and make it visible, this is the window that is in the screen...

    private void connectActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
        math1a m1 = new math1a();
        m1.setVisible(true);
        m1.pbStart();
    
        math2a m2 = new math2a();
        m2.setVisible(true);
    }                                       
    

    But in math1a, you create a second instance of math2a, which is not visible on the screen...

    @SuppressWarnings("unchecked")
    math2a m2 = new math2a();
    
    public void pbStart(){
        //attempt 1
        pb1.setValue(100);
        m2.setPB(100);
       m2.pb2.setValue(m2.getPB());
       //attempt 2
       m2.setBar(100);
    
    }
    

    You either need to pass the reference of math2a you created in connectActionPerformed or simply use the instance you have already created in math1a.

    Personally, I'd do neither. Instead, I'd use some kind of model that is shared between math1a and math2a, this would allow math1a to update the model and through the use of an Observer Pattern, the model would notify math2a which would then be able to update it's progress bar.

    You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others