Window and Button
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Pedro_Dias
Posted On:   Sunday, November 10, 2002 03:55 PM

class Teste extends JFrame implementes ActionListener { JFrame f2; public Teste() { JButton botao = new JButton("Fechar"); getContentPane().add(botao); botao.addActionListener(this); } public void mostrar() { f2 = new Teste(); f2.getContentPane().setLayout(new FlowLayout()); f2.setSize(230, 120); //tamanho da janela f2.setResizable(false); f2.show(); } public void actionPerformed(ActionEvent ev) { f2.hide(); } } I have this code, but w   More>>
			

class Teste extends JFrame implementes ActionListener

{

JFrame f2;


public Teste()

{

JButton botao = new JButton("Fechar");

getContentPane().add(botao);

botao.addActionListener(this);


}


public void mostrar()

{

f2 = new Teste();

f2.getContentPane().setLayout(new FlowLayout());

f2.setSize(230, 120); //tamanho da janela

f2.setResizable(false);

f2.show();

}



public void actionPerformed(ActionEvent ev)

{

f2.hide();

}

}


I have this code, but when I click in button to hide the
window give a error in prompt like this
java.lang.NullPointerException in "f2.hide()", but if I put the
line "f2.hide()" out of actionPerformed
already works, but not when I click in button.

Someone tell me why?
   <<Less

Re: Window and Button

Posted By:   Anonymous  
Posted On:   Wednesday, November 13, 2002 05:50 AM

I think you have a conceptual problem in mostrar method,
it doesn't need to instanciate a new object, for example you can get rid of f2 variable (the this in the code below is not necesary):


void mostrar() {
// f2 = new Teste();
this.getContentPane().setLayout(new FlowLayout());
this.setSize(230, 120); //tamanho da janela
this.setResizable(false);
this.show();
}


and


public void actionPerformed(ActionEvent ev) {
this.hide();
}


Your problem is that you have two objects, one 'original' (ie. you instanciate in the main method, I call it o1) and then you call 'mostrar' on that object and mostrar instanciate a new object (o2). o1.f2 == o2, but o2.f2 == null!, and the one you show (o2) is the one that has shows the button and the one where actionPerformed is called.

Re: Window and Button

Posted By:   George_Tzallas  
Posted On:   Tuesday, November 12, 2002 02:31 AM

Pedro,


Out of curiosity, could you please show me your main (String argv[]) method?


Regards


George T

About | Sitemap | Contact