JFrame hide

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
Reaction score
11
Ok, I'm pretty sure I'm overlooking something stupid...

I have a JFrame that opens, and on button-click, I open another JFrame. I would also like to hide the original JFrame.

My code looks something like:

Code:
public class someClass implements ActionListener
{
JFrame frame1;
JFrame frame2;
public someClass()
{
frame1 = new JFrame("frame1");
//blah
//blah
frame1.setVisible(true)
}
public void ActionPerformed(ActionEvent e)
{
frame1.setVisible(false);
frame2 = new JFrame("frame2");
//blah
//blah
frame2.setVisible(true);
}
}

The ActionPerformed() method triggers fine, but I can't seem to figure out what context to call
Code:
frame1.setVisible(false);
Whatever I try, it can't find it and sends me a java.lang.NullPointerException

Can someone help me out please?
 
Instead of using frame1.setVisible(false); in the actionPerformed method, try using this.setVisible(false);

The object reference is to the frame1 because it is the button object on frame1 that called the actionPerformed method.

I've added some code to make it a workable solution.

eg

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class someClass extends JFrame implements ActionListener
{

JButton buttonFrm2 = new JButton("FRAME 2");
JPanel pane = new JPanel();
public someClass()
{
super("frame1");
pane.add(buttonFrm2);
buttonFrm2.addActionListener(this);
setContentPane(pane);

}



public void actionPerformed(ActionEvent e)
{
this.setVisible(false);
JFrame frame2 = new JFrame("frame2");

frame2.setVisible(true);
}


public static void main(String[] args)
{
someClass frame1 = new someClass();
frame1.setVisible(true);

}
}

I hope that helps.
 
try
Code:
YourClass.[B][COLOR="DarkOrchid"]this[/COLOR][/B].[COLOR="RoyalBlue"]frame1[/COLOR].setVisible([B][COLOR="DarkOrchid"]false[/COLOR][/B]);
 
Last edited:
Instead of using frame1.setVisible(false); in the actionPerformed method, try using this.setVisible(false);

The object reference is to the frame1 because it is the button object on frame1 that called the actionPerformed method.

I've added some code to make it a workable solution.

eg



I hope that helps.

Thanks for the reply, but I tried this, and it didn't work :(
 
try
Code:
YourClass.[B][COLOR="DarkOrchid"]this[/COLOR][/B].[COLOR="RoyalBlue"]frame1[/COLOR].setVisible([B][COLOR="DarkOrchid"]false[/COLOR][/B]);

This doesn't throw an exception, but it also doesn't hide the frame :(
 
Just put this together quickly,ignore the way I go about adding components to the frames- not the best way to go about it.

Code:
import java.awt.event.*;
import javax.swing.*;
public class testApp extends WindowAdapter implements ActionListener
{
	JFrame jFrame1, jFrame2;
	JButton j1 = new JButton("Click Me");
	JButton j2 = new JButton("Click Me");
	public static void main(String args[])
	{
		new testApp();
	}
	
	public testApp()
	{
		jFrame1 = new JFrame("Frame 1");
		
		jFrame1.getContentPane().add(j1);
		j1.addActionListener(this);
		
		jFrame1.setBounds(50,50,200,140);
		
		jFrame1.addWindowListener(this);
		jFrame1.setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource().equals(j1))
		{
			jFrame1.setVisible(false);
			
			if(jFrame2 ==null)
			{
				jFrame2 = new JFrame("Frame 2");
				jFrame2.getContentPane().add(j2);
				jFrame2.setBounds(10,50,200,80);
				
				jFrame2.addWindowListener(this);
				j2.addActionListener(this);
			}
			jFrame2.setVisible(true);
		}
		else
		{
			jFrame2.setVisible(false);
			jFrame1.setVisible(true);
		}
		
	}
	
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}
 
Just put this together quickly,ignore the way I go about adding components to the frames- not the best way to go about it.

Code:
import java.awt.event.*;
import javax.swing.*;
public class testApp extends WindowAdapter implements ActionListener
{
	JFrame jFrame1, jFrame2;
	JButton j1 = new JButton("Click Me");
	JButton j2 = new JButton("Click Me");
	public static void main(String args[])
	{
		new testApp();
	}
	
	public testApp()
	{
		jFrame1 = new JFrame("Frame 1");
		
		jFrame1.getContentPane().add(j1);
		j1.addActionListener(this);
		
		jFrame1.setBounds(50,50,200,140);
		
		jFrame1.addWindowListener(this);
		jFrame1.setVisible(true);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource().equals(j1))
		{
			jFrame1.setVisible(false);
			
			if(jFrame2 ==null)
			{
				jFrame2 = new JFrame("Frame 2");
				jFrame2.getContentPane().add(j2);
				jFrame2.setBounds(10,50,200,80);
				
				jFrame2.addWindowListener(this);
				j2.addActionListener(this);
			}
			jFrame2.setVisible(true);
		}
		else
		{
			jFrame2.setVisible(false);
			jFrame1.setVisible(true);
		}
		
	}
	
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}

Thanks for the reply.

This is essentially what I had, but it doesn't like it.

I just noticed something that might explain the strange behaviour:

I have one frame's definition in one class, and the other frame's definition in another class, and a controlling program that handles them. However I'm still confused, because each class's actionPerformed() method is trying to frame.setVisible(false) its OWN JFrame, so it SHOULD work
 
Ok, what I tried now was adding a method to the frame I'd like to hide:

Code:
JFrame getFrame()
{
     return frame1;
}

But this method always returns null :confused:
I've also tried
Code:
return this.frame1;

Now what?
 
Ok, you guys are gonna be cross with me :o

I searched through my code and found two declarations of the JFrame I wanted to hide; one in the class's scope and another ONLY in the constructor's scope. It works now.

Sorry for the stupidity :(
 
Ok, you guys are gonna be cross with me :o

I searched through my code and found two declarations of the JFrame I wanted to hide; one in the class's scope and another ONLY in the constructor's scope. It works now.

Sorry for the stupidity :(

Hehe I know the feeling. ;)
 
Top
Sign up to the MyBroadband newsletter
X