LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java super() main class (https://www.linuxquestions.org/questions/programming-9/java-super-main-class-587327/)

trscookie 09-25-2007 04:59 PM

java super() main class
 
Hello all,
I know that there are probably a million posts about this however I can not find any that can answer my question.
I have two forms in Netbeans( Java ) one of the frames loads up another to enter a text address but the information is not passed back to the main form.

I have tried to use the super() function but it just passes a NullPointerException. I think that I'm close but haven't quite got it:


main.java:
Code:

public class main extends javax.swing.JFrame


    private winTwo          winTwo      = new winTwo();


    public main()
    {
       
        super( "main" );
       
        initComponents();
       
       
    }


    private void initComponents()
    {

        // some generic components

    }

   
    private void button(java.awt.event.ActionEvent evt)
    {

        winTwo ss = new winTwo( this );
       
        ss.main( null );

    }   


    public void setName( String name )
    {

        System.out.println( "Set name: " + name );

    }


}


winTwo.java:
Code:

public class serverSettings extends javax.swing.JFrame
{
   
    private static helpClient              parent      = null;

   
    public serverSettings( JFrame parent, String s )
    {
       
        super( s );
       
        initComponents();
       
    }
   
   
    public serverSettings( JFrame parent )
    {
       
        super();
       
    }


    private void btnAccept(java.awt.event.ActionEvent evt)
    {                                         
       
        parent.setName( "Hello World" );
       
    }     



    public static void main( String args[] )
    {
       
        java.awt.EventQueue.invokeLater( new Runnable()
        {
           
            public void run()
            {
               
                main Main = new main( parent );
               
                Main.setVisible( true );
               
            }
           
        } );
}


trscookie 09-26-2007 02:10 PM

there have got to be some java programmers out there :(

trscookie 09-26-2007 05:34 PM

here is a better example:

Code:

package multiForm;



public class frameOne extends javax.swing.JFrame
{
   
    private static frameTwo two    = new frameTwo();
   
   
   
    public frameOne()
    {
   
        initComponents();
       
    }
   
   
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setText("Click Me!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Should Change");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
   
   
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       
        two.setVisible( true );
       
    }//GEN-LAST:event_jButton1ActionPerformed
   
   
    public static void main( String args[] )
    {
       
        java.awt.EventQueue.invokeLater( new Runnable()
        {
           
            public void run()
            {
               
                new frameOne().setVisible( true );
               
            }
           
        } );
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
   
}




Code:


package multiForm;



public class frameTwo extends javax.swing.JFrame
{
   
   
    public frameTwo()
    {
       
        initComponents();
       
    }
   
   
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jTextField1.setText("Set label on frame one");

        jButton1.setText("send text");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       
        String s = jTextField1.getText();
       
       
        // the string should be sent to the first class and set the label here
        // i have tried to used the functions super and parent but i cant get them to work :(
       
       
        this.dispose();
       
    }//GEN-LAST:event_jButton1ActionPerformed
   
   
    public static void main( String args[] )
    {
       
        java.awt.EventQueue.invokeLater( new Runnable()
        {
           
            public void run()
            {
               
                new frameTwo().setVisible( true );
               
            }
           
        } );
       
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
   
}



All times are GMT -5. The time now is 04:08 AM.