JOptionPane in the foreground

3

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

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

I saw something with changing null by this in the first parameter of Dialog , but it did not work.

    
asked by anonymous 01.11.2017 / 19:29

2 answers

0

Hello, from what I saw the syntax is wrong.

Try 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

Click here to go to the showMessageDialog() documentation.

    
01.11.2017 / 19:49
0

Hello, correcting a little information from my 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 code example, in which, without closing 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, you may be after JDialog it has an option called AlwaysOnTop which, when true, keeps it always in front of any window regardless of which one to 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);
    
01.11.2017 / 21:06