Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How i close my frame when i click in the button???  RSS feed

 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
I have a programa that have one button, when I click in the button open a frame and into the frame have a other jbutton and I want to close my frame when I click in the button...
I tried this, but it closes my program all:
btn4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

How I do??? to close my frame only...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yourframe.dispose()
 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ocurred an error:
local variable janela is accessed from within inner class; needs to be declared finaljanela.dispose();
what is this???
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code for you JFrame class you are trying to close.
 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here...
JDialog janela = new JDialog(this, botao.getText(),true);
janela.getContentPane().setLayout(null);
janela.setSize(550,150);
janela.setLocation(200,200);
janela.setResizable(false);
/** Inserir dados sobre serviļæ½os da tela aqui */
JLabel label1 = new JLabel ("URL: "+url);
label1.setBounds(30,10,540,30);
janela.getContentPane().add(label1);
JLabel label2 = new JLabel ("TEMPO DE RESPOSTA: "+servico.getTempoResposta()+" segundos ");
label2.setBounds(30,10,540,90);
janela.getContentPane().add(label2);
janela.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
//mostra subjanela
janela.show();
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method-local variable (the dialog) is being accessed from within an anonymous class. You must declare the method-local variable as final:
JDialog janela = new JDialog(this, botao.getText(),true);
<becomes>
final JDialog janela = new JDialog(this, botao.getText(),true);

Then you'll be fine.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana is correct. Or you could make your JDialog variable a global variable. Either way will work. Then just call dispose() on the JDialog instead of System.exit(0) as this will cause the JVM to kill your application.
 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank's Danna and friends, you are correct....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!