Closing a form from another one

View: New views
8 Messages — Rating Filter:   Alert me  

Closing a form from another one

by AlexanderPD :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi, using the netbeans Gui builder i made 2 form, "a" and "b"



If i need to close form "a" from "a" i simply need to use:

this.setVisible(false);



it works! :D

but now i need to close "a" from "b" :|

using a.setVisible(false); makes an "non-static method setVisible(boolean) cannot be referenced from a static context" :(



How can i close "a" from "b"? :(



thank you :)





Re: Closing a form from another one

by Bayless Kirtley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sounds like you are calling setVisible() from a static method. You can't do
that.

Bayless

----- Original Message -----
From: "AlexanderPD" <pdalex@...>
To: <nbusers@...>
Sent: Friday, July 03, 2009 10:14 AM
Subject: [nbusers] Closing a form from another one


> Hi, using the netbeans Gui builder i made 2 form, "a" and "b"
>
>
>
> If i need to close form "a" from "a" i simply need to use:
>
> this.setVisible(false);
>
>
>
> it works! :D
>
> but now i need to close "a" from "b" :|
>
> using a.setVisible(false); makes an "non-static method setVisible(boolean)
> cannot be referenced from a static context" :(
>
>
>
> How can i close "a" from "b"? :(
>
>
>
> thank you :)
>
>
>
>


Re: Closing a form from another one

by Jeffrey H. Coffield :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



AlexanderPD wrote:

> Hi, using the netbeans Gui builder i made 2 form, "a" and "b"
>
>
>
> If i need to close form "a" from "a" i simply need to use:
>
> this.setVisible(false);
>
>
>
> it works! :D
>
> but now i need to close "a" from "b" :|
>
> using a.setVisible(false); makes an "non-static method setVisible(boolean) cannot be referenced from a static context" :(
>
>
>
> How can i close "a" from "b"? :(
>
>
>
> thank you :)
>
>
> .
>
Somewhere you are creating form a and form b. I assume something like a
formA = new a(); and b formB = new b();. So you need to
get formA into formB so it can reference the real object, not just it's
class. If b is being created by a then you could modify b to have a
constructor method of b(a formA). Then in a you could have b formB = new
b(this). Then in b you can save off the formA and when needed do a
formA.setVisible(false);

Without seeing more of your code, this is only a guess.

Jeff Coffield
www.digitalsynergyinc.com


Closing a form from another one

by AlexanderPD :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thank you for the answer :D



i don't think i can do that :(

my classes starts with:



public class a extends javax.swing.JFrame

and

public class b extends javax.swing.JFrame

(netbeans gui builder auto-generate that), so they both extends JFrame :(





Re: Closing a form from another one

by Jeffrey H. Coffield :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



AlexanderPD wrote:

> thank you for the answer :D
>
>
>
> i don't think i can do that :(
>
> my classes starts with:
>
>
>
> public class a extends javax.swing.JFrame
>
> and
>
> public class b extends javax.swing.JFrame
>
> (netbeans gui builder auto-generate that), so they both extends JFrame :(
>
>
> .
>
>
That doesn't prevent you from adding a new method with a parameter or
adding a method to b that will store a reference to a that can then be
called to set setVisble to false.

There may be a better way to do this, and if someone else has an idea,
I'd also like to know.

Jeff Coffield

Jeff


Re: Closing a form from another one

by djohnson-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You could do it with events. Add one form as an ActionListener to the
other form. When you want the other form to set the first one
visible/invisible (or do anything else) fire an event to the listener
telling it what you want it to do.

HTH

David Johnson

On Sat, 2009-07-04 at 13:08 -0700, Jeffrey H. Coffield wrote:

>
> AlexanderPD wrote:
> > thank you for the answer :D
> >
> >
> >
> > i don't think i can do that :(
> >
> > my classes starts with:
> >
> >
> >
> > public class a extends javax.swing.JFrame
> >
> > and
> >
> > public class b extends javax.swing.JFrame
> >
> > (netbeans gui builder auto-generate that), so they both extends JFrame :(
> >
> >
> > .
> >
> >
> That doesn't prevent you from adding a new method with a parameter or
> adding a method to b that will store a reference to a that can then be
> called to set setVisble to false.
>
> There may be a better way to do this, and if someone else has an idea,
> I'd also like to know.
>
> Jeff Coffield
>
> Jeff
>

Closing a form from another one

by AlexanderPD :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


djohnson wrote:
> You could do it with events. Add one form as an ActionListener to the

> other form. When you want the other form to set the first one

> visible/invisible (or do anything else) fire an event to the listener

> telling it what you want it to do.

>

> HTH

>

> David Johnson

>

> On Sat, 2009-07-04 at 13:08 -0700, Jeffrey H. Coffield wrote:

>
> >

> > AlexanderPD wrote:

> >
> > > thank you for the answer :D

> > >

> > >

> > >

> > > i don't think i can do that :(

> > >

> > > my classes starts with:

> > >

> > >

> > >

> > > public class a extends javax.swing.JFrame

> > >

> > > and

> > >

> > > public class b extends javax.swing.JFrame

> > >

> > > (netbeans gui builder auto-generate that), so they both extends JFrame :(

> > >

> > >

> > > .

> > >

> > >

> > >
> > That doesn't prevent you from adding a new method with a parameter or

> > adding a method to b that will store a reference to a that can then be

> > called to set setVisble to false.

> >

> > There may be a better way to do this, and if someone else has an idea,

> > I'd also like to know.

> >

> > Jeff Coffield

> >

> > Jeff

> >
>




i tryed that.. without any success :(

i'll post the first class:




Code:
package progettoos;



public class a extends javax.swing.JFrame {



    public a() {

        initComponents();

    }

    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">

    private void initComponents() {



        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)

            .addGap(0, 400, Short.MAX_VALUE)

        );

        layout.setVerticalGroup(

            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

            .addGap(0, 300, Short.MAX_VALUE)

        );



        pack();

    }// </editor-fold>



    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new a().setVisible(true);

            }

        });

    }

    // Variables declaration - do not modify

    // End of variables declaration

}







it's a totally empty interface, and if i try to use the "a.setvisible(false)" or "a.dispose" i get the "non-static method cannot be referenced from a static context", ever if i type it just after the initComponents();

it works if i use this.setVisible(false) or this.dispose(), but not if i write it in a static method..

so, how i can close it from another jframe or another class?





Re: Closing a form from another one

by Jeffrey H. Coffield :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



AlexanderPD wrote:
>
> i tryed that.. without any success :(
>
> i'll post the first class:
>

I still don't understand exactly what you are trying to do but here is
one possible (and probably not the best) way:


/*
 * a.java
 *
 * Created on Jul 13, 2009, 6:36:33 AM
 */

package javaapplication4;

/**
 *
 * @author jeffrey
 */
public class a extends javax.swing.JFrame {

    /** Creates new form a */
    public a() {
        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.
     */
    @SuppressWarnings("unchecked")
    // <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("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("a");

        javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(164, 164, 164)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(jButton1)))
                .addContainerGap(178, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel1)
                .addGap(73, 73, 73)
                .addComponent(jButton1)
                .addContainerGap(152, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_jButton1ActionPerformed
        // Here is the main change
        b bform = new b(this);
        bform.setVisible(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new a().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

}


/*
 * b.java
 *
 * Created on Jul 13, 2009, 6:39:11 AM
 */

package javaapplication4;

/**
 *
 * @author jeffrey
 */
public class b extends javax.swing.JFrame {

    private a aframe;

    /** Creates new form b */
    public b() {
        initComponents();
    }

// This is the main part I added to get a reference to a from b
    public b(a newAframe) {
        aframe = newAframe;
        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.
     */
    @SuppressWarnings("unchecked")
    // <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("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("b");

        javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(117, 117, 117)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(137, 137, 137)
                        .addComponent(jLabel1)))
                .addContainerGap(195, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addGap(67, 67, 67)
                .addComponent(jButton1)
                .addContainerGap(164, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{//GEN-FIRST:event_jButton1ActionPerformed
        if (aframe != null) {
            aframe.setVisible(false);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new b().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

}