Java - setVisible (true) does not affect GUI

I created a GUI (called ParameterUI) with the NetBeans GUI Builder, and now I want to instantiate it and display it. However, using

ParameterUI gui = new ParameterUI();
gui.setVisible(true);

does not cause a window to appear ... Testing shows that after these commands gui.isVisible () returns true, but gui.isValid () returns false. The call to gui.revalidate () also has no effect.

In the ParameterUI class, the constructor method is generated by Netbeans and simply

public class ParameterUI extends javax.swing.JPanel {
    public ParameterUI() {
        initComponents();
    }
}

initComponents is just a list of where all jPanel, etc. will be placed.

The strange thing is that when I made a practical GUI with a tutorial at http://netbeans.org/kb/docs/java/gui-functionality.html , the GUI was set as the main class despite the absence of the main method, and the GUI appeared itself by oneself.

Unfortunately, I'm a newbie with GUIs (I use the constructor because I don't have time to learn how to make the right manual GUI), but can anyone tell me how to make my GUI visible? If necessary, I can provide more code ...

EDIT: I tried

JFrame window = new JFrame();
ParameterUI gui = new ParameterUI();
window.setContentPane(gui);
window.pack();
window.setVisible(true);

after reading a short tutorial on JFrames, but it doesn’t change anything ...

+3
source share
2 answers

JFrame Desktop Netbeans? , , Netbeans , , ... JFrame. , , , , :

SwingUtilities.invokeLater(new Runnable() {
           public void run()
           {
               ParameterUI gui = new ParameterUI();
               gui.setVisible(true);
           }
       });

JPanel, JFrame. , netbeans JFrame ( " JFrame". , ( "", "" ..) "Inspector" , - . JPanels, , "". JFrame, "Inspector" , "[JFrame]". "". .

, JFrame ParameterUI

+1

setVisible() Component ( , ). , , , .

setVisible() Window , . . , , . pack() setLocationRelativeTo() .

, gui, ParameterUI Window (, JFrame JDialog), , setVisible(true) ParameterUI. ():

// expected to be called on the AWT/Event Dispatch Thread
public void show(ParameterUI ui) {
  JFrame frame = new JFrame();
  frame.setLayout(new BorderLayout());
  frame.add(ui, BorderLayout.CENTER);
  frame.pack();
  frame.setLocationRelativeTo(null); // position in the center of the screen
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.setVisible(true);
}
+3

Source: https://habr.com/ru/post/1758591/


All Articles