tags:

views:

78

answers:

3

I created a GUI (called ParameterUI) with the Netbeans GUI Builder and now I want to create an instance of it and display it. However, using

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

doesn't cause any window to appear... Testing shows that after those commands, gui.isVisible() returns true, but gui.isValid() is false. Calling gui.revalidate() has no effect either.

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

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

initComponents is simply a listing of where each jPanel etc. will be placed.

The strange thing is that when I made a practice GUI with the tutorial at http://netbeans.org/kb/docs/java/gui-functionality.html , the GUI was set as the main class despite having no main method and the GUI appeared of its own accord.

Unfortunately I'm a novice with GUIs (I'm using the builder cause I haven't got time to learn how to make a proper hand-made GUI), but can someone tell me how to make my GUI visible? I can provide more code if necessary...

EDIT: I tried

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

having read a short tutorial on JFrames, but it doesn't seem to change anything...

A: 

Is that control added to a parent container ?

thelost
+2  A: 

setVisible() on a Component sets a flag in that component (among other things you don't really care about at this point). This flag is checked by the container that contains your component to see whether the component needs to be shown.

setVisible() on a Window controls whether the window is displayed on the screen. Now, all it does is make your window appear or disappear. Typically, you want to given it some size and location before making it visible. pack() and setLocationRelativeTo() are useful here.

So to see your gui, ParameterUI either has to extend Window (probably JFrame) or it has to be contained in a window and you should call setVisible(true) on the window instead of the ParameterUI instance.

jackrabbit
ParameterUI seems to extend a JPanel, and not a JFrame. So it needs a Window (JFrame or so) to make it visible.
extraneon
ParameterUI does extend a JPanel, since that's how Netbeans generates the code. Um, so how do I put it in a Window / JFrame? Should I change it to extend JFrame, or should I create one and add my "gui" instance in somehow?
Stella
I have modified my answer hopefully explaining how to move the panel to a JFrame :)
npinti
+1  A: 

Are you using a JFrame or did you create a Desktop application with Netbeans? Because if you created a desktop application, Netbeans has its own class that it uses and I have had many problems with it as well... thus, I suggest you use a JFrame. Any how, you can try this to see if the UI launches:

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

Since you are extending the JPanel, you will need to put your panel onto a JFrame to be visible. To do this, in netbeans, simply create a new JFrame (right click on the package and select "New JFrame". Now, go back to your panel, on your left margin (under Project, Files, etc) you should have an item named "Inspector" Click that and you should see a tree view of your components. Right click on the JPanels you want to have visible and select "Copy". Go back to the JFrame that has just been created, find the "Inspector" button from the left margin, click it and on top you should have an item named "[JFrame]". Right click on that item and select paste. You should now see the panel you have created.

To view the panel then simply put the name of the JFrame instead of ParameterUI

npinti
The EDT shouldn't affect anything
TheLQ
It's not a desktop application.
Stella
Yes, it finally works! Thank you so much, this has been bothering me for ages now.
Stella
You're welcome :). Just keep in mind that whenever you want to build a GUI, go for the JFrame ;)
npinti