How to put a Jlabel above the buttons on a Jdialog?

Asked

Viewed 612 times

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);
}                              

PRINT JDIALOG

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?

  • 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.

  • so I figured if you could change the name of the joptionPane buttons you wouldn’t have to create that Jdialog anymore? right?

  • 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.

  • 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 :)

  • 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 !

Show 1 more comment

1 answer

3


To change the buttons on JoptionPane you can use this code:

    JOptionPane.showOptionDialog(null, 
    "Aceitas este codigo como correto?", 
    "JoptionPane", 
    JOptionPane.OK_CANCEL_OPTION, 
    JOptionPane.INFORMATION_MESSAGE, 
    null, 
    new String[]{"Sim aceito", "Não aceito"}, // this is the array
    "default");

Final result:

inserir a descrição da imagem aqui

Edit:

If you need to verify what the answer was you can use it like this:

int selectedOption = JOptionPane.showOptionDialog(null,
                "Aceitas este codigo como correto?",
                "JoptionPane",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Sim aceito", "Não aceito"}, // this is the array
                "default");

        if (selectedOption == JOptionPane.YES_OPTION) {
            System.out.println("Eu aceitei");
        } else {
            System.out.println("Nao!!! Eu nao aceito este codigo");
        }
  • That’s exactly what I wanted. Just one question: By placing this code in the Onfocusgain text field, it appears to me jOptionPane twice, how can I close jOptionPane? Dispose(); closes the whole program. Thanks in advance!

  • I understood what you said but I’m not sure how to help you... first to close the whole program is because you havesetDefaultCloseOperation in this frame set to close, change to Hide...(anything)... This solves the problem of closing the whole program, now the problem of joptionPane appearing 2 times is more difficult because either in some part of the code flames 2 times or you are executing a function that calls you without you... I could tell?

  • My code is exactly the same as the second suggestion you gave me. If I leave it as it is, in the 'Yes' option, I get an infinite cycle of jOptionPane, because the focus of the field should not be leaving. This way, I put after System.out.println(), a fieldName.requestFocus(), and in this case the window appears twice :S If instead of putting in the method onFocusGain and put the code in onMouseClick it works perfectly.

  • Ask your question and publish that part of the code to see if you can understand better, because this is hard...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.