Results 1 to 5 of 5

Thread: Frames

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Frames

    For this project, I'm using NetBeans 5.0. I came across a problem with referencing instances. I have one frame where the user clicks the register button and this shows the second frame. When they click the button, the first frame's setVisible method is set to false and the second frame is shown. On the second frame, there is a back button to view the first frame. My question is, how can I set the property of the first frame to set visibilty to true? The second frame has no reference to the instance of the first frame, so I was wondering how you would pass that reference to the second form.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Frames

    To pass info from one to another maybe you could do it throught the constructor? Have an argument there? Can you post a bit of your code to see exactly how you are doing it?


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Frames

    You can always have a private variable such as a JFrame and pass the form through a setter method or constructor:

    Code:
    class Test extends something implements something
    {
       private JFrame handle;
    
       public Test(JFrame h)
       {
           this.handle = h;
       }
    
       public void actionPerformed(actionevent ae)
       {
           handle.setVisible(true);
        }
    }
    I THINK that might work, but since most objects are passed by value it might not; however, you can always work with inner classes.

  4. #4

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: Frames

    To be honest with you, i'm new to java (i'm in an object oriented programming class here at Penn State... its a long story and we were already supposed to know java from a previous class but that instructor didnt teach us it so im behind and trying to catch up in this class). Anyway, here is the code:

    Code:
    //Just instantiates the main login form (First Frame)
    public class Main {
        
        /** Creates a new instance of Main */
        public Main() {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            LoginFrame newLoginFrame = new LoginFrame();
            
            newLoginFrame.setVisible(true);
                
                  
                    
        }
        
    }
    Code:
    //This class is for the login frame (First Frame) - Most of the code was cut out to save space.. its just autogenerated code from NetBeans
    public class LoginFrame extends javax.swing.JFrame {
        
        public LoginFrame() {
            initComponents();        
            
        }
        
        private void btnRegisterMouseClicked(java.awt.event.MouseEvent evt) {                                         
            RegisterFrame newRegisterFrame = new RegisterFrame();
            
            newRegisterFrame.setVisible(true);
            
            this.setVisible(false);
        }
    Code:
    //This is the code for the register frame (Second Frame)
    //Again, some code was removed
    public class RegisterFrame extends javax.swing.JFrame {
        
        /** Creates new form RegisterFrame */
        public RegisterFrame() {
            initComponents();
        }
    
     private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
            //This is where I would like to insert code that will set the First (Login) Frame back to visible and dispose of this frame
    
            this.dispose();
            
        }
    Also, is it proper coding style in java to dispose of a frame using this.dispose(); if you do not want to access the frame again? If you do want to access the frame again, would setting the instance of that frames visible property to false be proper coding style or would that be wrong due to the fact it utilizes more memory?

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Frames

    Try this small example and see if you can adapt it in your program:

    OtherFrame.java -- Frame you'll be setting the visibility of
    Code:
    import javax.swing.*;
    import java.awt.*;
    public class OtherFrame extends JFrame
    {  
       public OtherFrame()
       {
           JButton btnVisible = new JButton("I do Nothing");
           JButton btnInvis = new JButton("Neither do I");
           Container content = getContentPane();
           content.setLayout(new FlowLayout());
           content.add(btnVisible);
           content.add(btnInvis);
           setSize(200,60);
           setVisible(true);
       }
    }

    Test2.java -- The main frame with buttons that set the visibility
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Test2 extends JFrame implements ActionListener
    {
       private OtherFrame otherFrame;
       private JButton btnVisible = new JButton("Visible");
       private JButton btnInvis = new JButton("Invisible");
       
       public Test2()
       {
           otherFrame = new OtherFrame();
      
           btnVisible.addActionListener(this);
           btnInvis.addActionListener(this);
           
           Container content = getContentPane();
           content.setLayout(new FlowLayout());
           content.add(btnVisible);
           content.add(btnInvis);
           
           setSize(200,60);
           setVisible(true);
       }
       
       public void actionPerformed(ActionEvent ae)
       {
          if (ae.getSource() == btnVisible)
          {
               otherFrame.setVisible(true); 
          }
          else if (ae.getSource() == btnInvis)
          {
               otherFrame.setVisible(false);
          }
       }
       
       public static void main(String[] args)
       {
          Test2 t = new Test2();
       }    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width