Joptionpane in the foreground

Asked

Viewed 351 times

4

I have an app that uses one JOptionPane to show an error message, however I would like the JOptionPane always appeared in the foreground.

JOptionPane.showMessageDialog(null, "Usuário ou senha incorreto!", "Erro", JOptionPane.ERROR_MESSAGE);

I saw something with changing the null for this in the first parameter of Dialog, but it didn’t work.

2 answers

1

Hello, from what I saw the syntax is wrong.

Test the code below:

JOptionPane.showMessageDialog(null, //frame de referêcia
    "Usuário ou senha incorreto!",  //mensagem para o usuario
    "Inane error",                  //icone de erro pode deixar em branco ou null se não quiser usá-lo
    JOptionPane.ERROR_MESSAGE);     //tipo de mensagem

Clicking here to go to the documentation of the showMessageDialog().

1

Hello correcting a little information of the colleague:

    JOptionPane.showMessageDialog(null, //Qualquer Componente nao so Frame
    "Usuário ou senha incorreto!",  //mensagem para o usuario
    "Inane error",                  //icone de erro pode deixar em branco ou null se não quiser usá-lo
    JOptionPane.ERROR_MESSAGE);     //tipo de mensagem

A small example of code ,in which, without closing the JOptionPane it is impossible to click the other components in the Frame.

    JFrame frame = new JFrame("teste");
    JPanel painel = new JPanel(new GridLayout(2, 2));
    JButton button1 = new JButton("01");
    JButton button2 = new JButton("02");
    JButton button3 = new JButton("03");
    JButton button4 = new JButton("04");
    button1.addActionListener((ActionEvent e) -> {
        JOptionPane.showMessageDialog(button1, e.getActionCommand(), 
    "Titulo: Mensagem Informação ",JOptionPane.INFORMATION_MESSAGE);
    });
    button2.addActionListener((ActionEvent e) -> {
        JOptionPane.showMessageDialog(button2, e.getActionCommand(), 
    "Titulo: Mensagem Questão ",JOptionPane.QUESTION_MESSAGE);
    });
    button3.addActionListener((ActionEvent e) -> {
        JOptionPane.showMessageDialog(button3, e.getActionCommand(), 
    "Titulo: Mensagem Atenção ",JOptionPane.WARNING_MESSAGE);
    });
    button4.addActionListener((ActionEvent e) -> {
        JOptionPane.showMessageDialog(button4, e.getActionCommand(), 
    "Titulo: Mensagem Erro ",JOptionPane.ERROR_MESSAGE);
    }); 

    painel.add(button1);
    painel.add(button2);
    painel.add(button3);
    painel.add(button4);
    frame.add(painel);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Now, maybe you’re after a JDialog it has an option called AlwaysOnTop which, when true, keeps the same always in front of any window regardless of which select. It can also be used to Replace a Jframe.

    JFrame frame = new JFrame("teste");
    JPanel painel = new JPanel(new GridLayout(2, 1));
    JButton button1 = new JButton("01");
    JButton button2 = new JButton("02");
     button1.addActionListener((ActionEvent e) -> {
       JDialog dialog = new JDialog();
       dialog.setSize(200, 200);
       dialog.add(new JLabel("JDialog Teste"));
       dialog.setAlwaysOnTop(true);//Fica sobre todas as janelas
       dialog.setModal(true);//Impede que seja as janelas em segundo plano sejam acessadas
       dialog.setVisible(true);
     });
     button2.addActionListener((ActionEvent e) -> {
        JOptionPane.showMessageDialog(button2, e.getActionCommand(), 
    "Titulo: Mensagem Erro ",JOptionPane.ERROR_MESSAGE);
     });

    painel.add(button1);
    painel.add(button2);
    frame.add(painel);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  • Very good. Only needed to put correctly the Imports of the classes used in the example and send to EDT.

  • I thought I didn’t need to.

Browser other questions tagged

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