Newsgroup: it.comp.java
Titolo: accedere ai componenti swing

Con Netbeans ho creato un'applicazione di esempio consistente in un JFrame
"NewJFrame.java" contenente due ProgressBar. Il codice di NewJFrame.java è
questo:
-----------------------------------------------------------------------
/*
 * NewJFrame.java
 *
 * Created on 22 luglio 2007, 17.37
 */
/**
 *
 * @author  marco
 */
public class NewJFrame extends javax.swing.JFrame {
 
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
 
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">     
 
    private void initComponents() {
        jDialog1 = new javax.swing.JDialog();
        jProgressBar1 = new javax.swing.JProgressBar();
        jProgressBar2 = new javax.swing.JProgressBar();
        javax.swing.GroupLayout jDialog1Layout = new
javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
 
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
 
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
 
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
                .addContainerGap(134, Short.MAX_VALUE)
 
addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jProgressBar1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(118, 118, 118))
        );
        layout.setVerticalGroup(
 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(128, 128, 128)
                .addComponent(jProgressBar1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44)
                .addComponent(jProgressBar2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(100, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            new NewJFrame().setVisible(true);
            }
        });
 
        NewClass nc1;
        nc1 = new NewClass();  
        NewClass nc2 = new NewClass();
        nc1.barra();  // questa è la riga 79
        nc1.prova = 4;
        nc2.prova = 5;
        System.out.println(nc1.prova);
        System.out.println(nc2.prova);
 
 
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JDialog jDialog1;
    public static javax.swing.JProgressBar jProgressBar1;
    public static javax.swing.JProgressBar jProgressBar2;
    // End of variables declaration                   
 
}
--------------------------------------------------------------------
Avrei la necessità di accedere ai componenti Swing di NewJFrame da
un'altra classe; nell'esempio tento di accedere dalla classe
"NewClass.java" richiamando i metodi delle ProgressBar, ma a runtime
ottengo l'errore 
Exception in thread "main" java.lang.NullPointerException
        at NewClass.barra(NewClass.java:25)
        at NewJFrame.main(NewJFrame.java:79)
Riporto sotto il codice di NewClass:
--------------------------------------------------------------------
/**
 *
 * @author marco
 */
public class NewClass {
 
    int prova;
    /** Creates a new instance of NewClass */
    public NewClass() {
 
            System.out.println("pippo");
 
    }
 
    public void barra() {
            NewJFrame.jProgressBar1.setMaximum(100); // questa è la riga 25
            NewJFrame.jProgressBar1.setValue(30);
 
            NewJFrame.jProgressBar2.setMaximum(200);
            NewJFrame.jProgressBar2.setValue(200);
 
    }
 
 
 
}
----------------------------------------------------------------------
Le ProgressBar sono dichiarate public static, quindi dovrei riuscire ad
accedervi. Sottolineo che javac compila il codice senza segnalare errori.
Immagino si tratti di un quesito comune, se esiste un'alternativa mi date
qualche indicazione?
Grazie
Marco
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

 qualche indicazione?
Per chi sia interessato, credo di aver risolto: il codice va in run()
http://java.sun.com/developer/JDCTechTips/2005/tt0419.html
ciao
