Equivalent to C# Java Form.Showdialog()

Asked

Viewed 404 times

0

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

2 answers

2


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

Already components they inherit from JComponent, is also a component of the Container type (such as JPanel) or not(as JButton and JLabel), also have this method to define its 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

  • I researched about it, the setVisible it opens the form I specify, but it has a detail that the c# showDialog does that helps me a lot, it impossible to click on the previous form that tbm this open, It’s like those dialogue boxes that make you click OK to continue. I want to do this with any form of my JAVA program. I saw that in JAVA has a [Joptionpane.showInputDialog] tool that does this, but not the way I want it because it only opens a warning window where I can make text changes.

  • @Rodolfosouza then his question is too wide and it is not possible to answer, because I would have to explain the functioning of almost all classes that have this behavior and this runs outside the scope of the site. And this behavior is called modal, is possible in java through Jdialogs. Take a look at the link to see some questions about this, some have functional examples. Access the answer links to learn about these components too, it is important to understand their functioning.

  • Really, I will have to create through Jdialog.

  • I based your response on another forum.

0

I solved my problem like this: I created a Jdialog and I was changing until I got the way I wanted.

                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);
  • His answer was out of place, because the question beyond broad, questions something more theoretical, and does not ask the solution of a specific problem.

  • In case to be valid my answer I will have to explain what I did?

Browser other questions tagged

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