How to put a JLabel above the buttons in a JDialog?

3

I would like to put a label in a JDialog, but above the buttons there. How can I do this? I get all the components online:

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:TheCodeislikethisnow.Withthefirst'if'Ihavealreadysolvedthequestionofopeningthewindowtwice,butbypressingno,IhavemaderequestFocusforanotherfieldbutitappearstometwice:

privatevoidjFormattedNumMatriculaComercialFocusGained(java.awt.event.FocusEventevt){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 {}

}         
    
asked by anonymous 07.11.2014 / 13:47

1 answer

3

To change the JoptionPane buttons 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:

edit:

Ifyouneedtochecktheansweryoucanusethisway:

intselectedOption=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");
        }
    
07.11.2014 / 14:06