• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yourframe.dispose()
 
DanielCosta Sobrinho
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's Danna and friends, you are correct....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic