Equivalent to C # Form.ShowDialog () in Java

0

I'm starting now in Java, there's a feature I use a lot in C # which is NomeDaTela.ShowDialog(); . I wanted to know a similar code that does the same thing in Java.

    
asked by anonymous 16.01.2017 / 16:28

2 answers

3

Depends heavily on the api you are using. In swing, the top level components that inherit from java.awt.Window (as JFrame and JDialog ) have a setVisible(boolean) method, which serves to make the component visible or not in the application.

Already components that inherit from JComponent , is also a component of the Container type (such as JPanel ) or not (such as JButton and JLabel ), also have this method to set their visibility inside other containers.

In the documentation there are more details about each component and its use, you can see in the links below:

Lesson: Getting Started with Swing

Lesson: Using Swing Components

    
16.01.2017 / 17:14
0

I solved my problem like this: I created a JDialog and modified it until it was the way I wanted it.

                JDialog d = new JDialog(this, true);
                JButton BtnOK = new JButton("OK");
                JLabel lb1 = new JLabel();

                d.setLayout(null); // libera a edição do layout

                BtnOK.addActionListener(new ActionListener() { // atribui ação para o botão
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        d.dispose();
                    }
                });

                lb1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interfaces/Imagens/hqdefault.jpg"))); // NOI18N
                lb1.setSize(485,360);
                lb1.setLocation(10, 11);

                BtnOK.setForeground(Color.WHITE);
                BtnOK.setBackground(Color.RED);
                BtnOK.setSize(480,36); 
                BtnOK.setLocation(10,380);

                d.add(BtnOK);
                d.add(lb1);
                d.getContentPane().setBackground(Color.BLACK);
                d.setTitle("Errrrrrrrrrrrrrrou!");
                d.setSize(514, 460);
                d.setLocationRelativeTo(null);
                d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                d.setVisible(true);
    
17.01.2017 / 15:34