Comando em Swing equivalente ao confirm no Javascript

10 respostas
Pedrosa

Galera blz, como faço para jogar algo parecido com o confirm do Javascript no evento de fechar o form?

class Window extends WindowAdapter {
		
		public Window() {}
		
		public void windowClosing(WindowEvent windowevent) {
			Object obj = windowevent.getSource();
			if(obj == Principal.this) {
				Principal_WindowClosing(windowevent);
			}
		}
	}
	
	void Principal_WindowClosing(WindowEvent windowevent) {
		
		setVisible(false);
		dispose();
	}

10 Respostas

thiago.correa
JOptionPane.showConfirmDialog(Component parentComponent, Object message);
keller

Da uma olhada nesse topico aqui:
http://www.guj.com.br/posts/list/35644.java

Valeu? Até… :thumbup:
[]s

Pedrosa

Thiago com adaptar ao meu caso?

em javascript eu faria

if(confirm(msg)){

//faz alguma coisa

}
Pedrosa

Que coisa louca, mesmo colocando não, fecha a janela, o valor de não é 1 e fecha, affe

int opcao = JOptionPane.showConfirmDialog(null,"Escolha sim ou não", "Sim ou não?", JOptionPane.YES_NO_OPTION);
System.out.println("opcao" + opcao);
if(opcao == 0){
   setVisible(false);
   dispose();
}
thiago.correa

o zero equivale ao yes, o 1 equivale o no e o 2 equivale ao cancel

Pedrosa

certo mas ta fechando a janela nos 2 casos e ignora meu if, tem algo relacionado com o evento?

public void windowClosing(WindowEvent windowevent) {
			int opcao = JOptionPane.showConfirmDialog(null,"Deseja fechar e deslogar?", "Sim ou não?", JOptionPane.YES_NO_OPTION);
			System.out.println("opcao" + opcao);
			if(opcao == 0){
				Object obj = windowevent.getSource();
				if(obj == Principal.this) {
					Principal_WindowClosing(windowevent);
				}
			}	
}
thiago.correa

use o método consume caso a opção escolhida seja o ‘no’

keller

Pedrosa:
certo mas ta fechando a janela nos 2 casos e ignora meu if, tem algo relacionado com o evento?

[edit]
http://guj.com.br/posts/list/18544.java
[/edit]

Valeu? Até… :thumbup:
[]s

Pedrosa

gui, fiz igual ao exemplo do post indicado e esta fechando tanto no yes quanto no no, joguei no construtor conforme o exemplo:

public Principal(final Telefonia telefonia) {
		setTelefonia(telefonia);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter(){
 			public void windowClosing(WindowEvent e) {
 				int i = JOptionPane.showConfirmDialog(null ,"Deseja fechar e deslogar?", "Saída",JOptionPane.YES_NO_OPTION);
 				if (i == JOptionPane.YES_OPTION) {
 					if(telefonia.getTerminal() != null) {
 						if(telefonia.isStartado()) {
 							telefonia.getHangup();
 						}
 					}
 					System.exit(0);
 				} else {
 					repaint();
 				}
 			}
 		});


		try {
			initComponents();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,"Ocorreu o seguinte erro " + e.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
		}
		setLocationRelativeTo(null);
	}
Pedrosa

No meu método initComponents()

eu tirei essa linha:

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Criado 28 de julho de 2006
Ultima resposta 31 de jul. de 2006
Respostas 10
Participantes 3