My GUI will not display when I hit run in NetBeans - java

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)
}

JPanel forms are not created with main methods, in GUI Builder, which you do need.
JPanel is not a top-level container, which you do need to run a Swing app.
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.
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.
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

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:
icon in JButton is not shown at the running of the program, what could be?
Java Swing: multiple windows
What's the difference between the implements & extends keywords in Java

change JPanel to JFrame. It will work.

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.

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

Related

Java swing form does not open when launched from another class

I'm new to swing, and I'm trying to implement a simple GUI with one form (HospitalGUI.form). When I run the associated Java file (HospitalGUI.java) as main, the form is visible.
I want to be able to launch it from my controller so that either the GUI or a console interface can be selected. When my controller instantiates it, the code in the constructor is executed, but the form doesn't open.
Is there anything special or additional that needs to be done for a form to be opened by another Object?
I would greatly appreciate any help understanding this.
My GUI's main is
public static void main(String[] args) {
final int FRAME_WIDTH = 300;
final int FRAME_HEIGHT = 400;
JFrame frame = new JFrame("Hospital System");
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setContentPane(new HospitalGUI().rootPanel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
and the call in the controller is
HospitalGUI hospitalInterface = new HospitalGUI();
When I run the associated Java file (HospitalGUI.java) as main, the form is visible.
That is because you main() method creates the HospitalGUI class and a frame for the panel. Then you add the panel to the frame and make the frame visible.
and the call in the controller is
HospitalGUI hospitalInterface = new HospitalGUI();
When you just use the above statement that means the constructor of your class still essentilly needs to invoke all the code from the main() method to create a frame and add the panel to the frame and make the frame visible.
Since you didn't post all that code we can't guess what you are missing.
So in reality you need to change your design. You need two classes:
one class to create the panel you want to add to the frame. So you can add the panel to the frame created in the main() method.
the controller class. This class will be responsible for creating the frame and adding the panel to the frame.

how to set components invisible in netbeans drag and drop design view

I want to set components invisible in the netbeans design view and then show then from coding when some event occurs. Is it possible?
When you create NewJFrameForm using Netbeans, In Design view you can drag and drop all the components available in palette.
In order to set initially invisible / hidden at starting you have to do it manually.
Click on source above, Now you can see generated source of that frame you designed.
You will see constructor as:
public NewJFrame() {
initComponents();
}
Generated itself.
now you have to put your own code to update like in my case i will set invisible my compnents like:
jPanel1.setVisible(false);
OR
specific components:
jButton1.setVisible(false);
jToggleButton1.setVisible(false);
jLabel1.setVisible(false);
if prefer this like:
public NewJFrame() {
initComponents();
mySettings();
}
public void mySettings(){
//Hide or set initial Values of components
}
Note:
all your generated code is in
initComponents();
you can not edit it in source, have to do it in design view

Display pop-up message window in Java?

I read about JDialogs and JOptionPane messages but I still can't get it to work. I have a GUI class that extends JFrame. All I want to do is have a popup at the beginning of my program which informs the user about a couple of things. In my main I create the following gui:
GUI g = new GUI();
Right after that I was to display the window. I have tried the following in the main method:
JOptionPane.showMessageDialog(g, "work?");
JOptionPane.showMessageDialog(frame, "work?"); //(frame was used in documentation example so I tried it)
I also tried to add the pop up into the GUI class with the following
JOptionPane.showMessageDialog(this, "work?"); //(I'm not exactly sure what the Frame Owner parameter is supposed to be, unless I'm confusing this with JDialog.)
In any case, how would I make this window appear? Every single one of the methods I tried compiled, and nothing happened.
public class GUI extends JFrame implements ActionListener{
private Container background;
private static buttons etc...
private static JLabel disp,edisp;
private static JTextArea info;
//setting up the GUI for my program, adding action listeners, I can post more if necessary
}
And then I have the main where I want to call the pop up window
public static void main(String[] args){
GUI g = new GUI();
JOptionPane.showMessageDialog(g,"Work?");
}
Make sure that these are called near the beginning, be it in the main method or not.
Also, try just setting the first parameter as null.
So it reads:
JOptionPane.showMessageDialog(null,"Work?");
Also, remember to import it!

Call method in another class to set label text (don't use netbeans' default stuff)

I'm using netbeans and want to edit the text in a label. I want to edit this label from another class in the main driver class. I have about 7 or 8 JDialog pages and let's say the label is on one of those pages.
When I try to call the method from one of those JDialogs so that I can edit it, it keeps asking for a java.awt.Frame. Where would I find that Frame name? Or, is there an easier way of editing labels from another class?
Netbeans makes labels private by default so I looked online and people have said making a setter method would be easiest.
QuickScreen is a .java file for instance...
public static void resetNumbers(){
QuickScreen qs = new QuickScreen(some frame);
qs.editLabel("Hello");
}
Please refer to my last comment on bmoran's solution.
If you change the label you want to set to either default or protected level access, then you can set it from your dialog as long as both classes are in the same package (for default) or your dialog box extends the class that the label is in (protected).
public class FrameClass extends JFrame {
JLabel label1;// package access
MyDialog dialog;
//constructor *** Netbeans may have an init() method ***
public FrameClass(){
label1=new JLabel("Hello!");
...
}
...
}
public class MyDialog{
public void changeLabel(){
FrameClass.label1.setText("Good Bye!");
}
}

Display a form from another class file

I have a running java GUI application right now in a single class file, in this application I have a button that when clicked it is used to instantiate and display a separate form from a different class file in the same project. I am confused with how I actually access this other .java file in order to instantiate and display the form from it. Hope you can help.
Thanks, Beef
How do you access any class file? JFrame, JPanel, JTextField are all examples of Java source code contained in separate files. You would use:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextField textField = new JTextField();
So to access your custom form your would use:
CustomForm form = new CustomForm();
As long as the class file is found on your classpath it should not be a problem.
If you are having compile or run time problems then you need to display the message so we can give further help.
I am somewhat new to Java but can't you just create another class that contains a GUI and then when you click on a JButton component you can just create an instance of that class.
if (clicked == myButton) then {
myGUIClass = new myGUIClass(); //if the GUI is in the constructors this will create
//the frame.
}
Then when you are finished with the JFrame or class then you should have a dispose() method that tidies up all of your files and exits the JFrame.
Best Regards,
Doug Deines Hauf
I think that creating a new form in Java is a bit easier than C# or Visual Basic. Basically you can just create another class and build your form in that class. Once your GUI is built then you can just create an instance of that method in another class to show the gui.
Such as:
if (ButtonClick == true) {
MyGui m = new myGui(...);
m.show
} else
//no GUI shown here
}
Or you can just create an anonymous class to instantiate the GUI.
new myGui(...);
The above would create an anonymous class but I think it is better coding practice to create an actual variable of the class and then to call some method parameter that will show the GUI.

Categories

Resources