0

When I select "run" in Netbeans, my GUI does not display. It just displays a box on the bottom of the screen that says "Build successful".

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package modelrange;

import javax.swing.DefaultBoundedRangeModel;

public class RangedModel extends javax.swing.JPanel {

    DefaultBoundedRangeModel myModel;

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RangedModel().setVisible(true);
            }
       });
    }

    /**
     * Creates new form RangedModel
     */
    public RangedModel() {
        myModel = new DefaultBoundedRangeModel(123, 100, 0, 1000);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    private void initComponents() { 
    This is just the automated netbeans code from the GUI builder (edited out for the post)            
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
user2923395
  • 327
  • 4
  • 12
  • 23

5 Answers5

2
  1. JPanel forms are not created with main methods, in GUI Builder, which you do need.

  2. JPanel is not a top-level container, which you do need to run a Swing app.

  3. A top-level container is, for instance, a JFrame. So you should have created a JFrame form instead of a JPanel form. When you do this in Netbeans GUI Builder, a main method will be provided for you.

  4. A simple fix would be just to create a new JFrame form, then just drag and drop the JPanel form to the JFrame form, as seen here, get rid of the main method in your JPanel form, then run the JFrame form class.

  5. You may also need to set/change the Main class to the new JFrame form you just created. You can that by looking at this answer

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

First of all, you are extending JPanel, it's wrong because as peeskillet wrote at points 2 and 3. Kind of top-level container are:

  • JFrame : the window with the bar
  • JWindow : the window without bar
  • JDialog : the window usually used to create option window

So you have to extend one of them, probably the first.

Than in this top-level container you can create JPanel, one or more, everyone will be a container of another object which will be the contenent.

Morover, remember to setVisible every JPanel that you implement and also the top-level container.

Useful links:

Community
  • 1
  • 1
Mitro
  • 1,230
  • 8
  • 32
  • 61
0

change JPanel to JFrame. It will work.

Sushin PS
  • 93
  • 1
  • 11
  • 1
    Please see the comment from Steve Kuo – Jabir Mar 26 '14 at 04:42
  • even nothing in main. It will work. Because your constructor do all your GUI – Sushin PS Mar 26 '14 at 04:43
  • But who calls the constructor - also by default, a `JFrame` constructor doesn't add components to itself or show itself, so I hardly think it would make a difference. Besides, you shouldn't need to extend from `JFrame`, you're not adding any functionality to, simply create an instance and add the `RangedModel` to it... – MadProgrammer Mar 26 '14 at 04:45
  • user3301269 Please try and than share the resulting GUI. – Jabir Mar 26 '14 at 04:45
  • I added the main method I've used from another GUI I built, but it still does not display. Changing it to JFrame did not work either btw – user2923395 Mar 26 '14 at 04:46
  • constructor automatically invokes when object is created. initcomponents have the code you designed using NetBeans which is inside the constructor. try to create a new JFrame and design from first. if the you dont want to design again, add the already designed JPanel to JFrame – Sushin PS Mar 26 '14 at 04:50
0

Follow path YourProject/packacge which your java file is in then, You can right click to your project then click from over there "run file".This worked for me.

0

If you work in NetBeans, after building, check that you are running the file you need from the project. To do this, press shift + F6

F. Müller
  • 3,969
  • 8
  • 38
  • 49