3
I’d like to put a label on a Jdialog, but above the existing buttons. How can I do that? All components stay online for me:
private void jFormattedNumMatriculaComercialMouseClicked(java.awt.event.MouseEvent evt) {
JButton botaoSIM = new JButton("Sim");
JButton botaoNAO = new JButton("Não");
JDialog dialog = new JDialog();
JLabel mensagem = new JLabel("TESTE");
botaoSIM.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFormattedNumMatriculaComercial.setText(jFormattedNIPC.getText());
jFormattedNumMatriculaComercial.setForeground(Color.black);
jFormattedNumMatriculaComercial.requestFocus();
dialog.dispose();
}
});
botaoNAO.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
JPanel content = new JPanel();
content.add(botaoSIM);
content.add(botaoNAO);
content.add(mensagem);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.getContentPane().add(content);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
EDIT: The code is like this now. With the first 'if' I already solved the question of opening the window twice, but by pressing no, I put requestFocus to another field but it appears to me twice:
private void jFormattedNumMatriculaComercialFocusGained(java.awt.event.FocusEvent evt) {
if(jFormattedNumMatriculaComercial.getText().equals(" ")){
if(jFormattedNIPC.getText().equals(" ")){}
else{
int opcao = JOptionPane.showOptionDialog(null,
"Número igual ao NIF/NIPC ?",
"Número de Matrícula Comercial",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Sim", "Não"},
"default");
if (opcao == JOptionPane.YES_OPTION) {
jFormattedNumMatriculaComercial.setText(jFormattedNIPC.getText());
nomeOfContasC.requestFocus();
} else { nomeOfContasC.requestFocus();
}
}
}else {}
}
Can you explain to me what purpose you need it for? Like the logic of this operation?
– jsantos1991
Yes I can. I just want a window that shows me a question, and if you click yes, it performs an action, if you click No, perform another. But I liked the name of the buttons in English and not the default Joptionpane.
– Hugo Machado
so I figured if you could change the name of the joptionPane buttons you wouldn’t have to create that Jdialog anymore? right?
– jsantos1991
Yes. The joptionPane I tested had 3 buttons, YES NO and CANCEL. I just need a Yes and a No, and a question, nothing more.
– Hugo Machado
I suppose you’re new to programming, your code is a little fuzzy ;) yet I understand you perfectly... I can tell you to help is, the first if’s I think could be together, then do not compare with the empty spaces try to compare with the size or something of the genre becomes more visible, then whenever you want to leave the function you are using the 'Return;' so empty, this causes it to auto out of function.... For last I suggest you ask another question for that, it’s making sense of this :)
– jsantos1991
Yes I am still new to programming and I get by as I can and I learn from those who know, and so from now on my thanks :) The comparison with empty spaces is due to the fact that the field contains mask ################################################################## If I use isEmpty it returns false even without anything written! Thanks !
– Hugo Machado