NetBeans perpetually "running..." and will not compile - java

This is my Java code:
import javax.swing.JFrame;
public class Objects {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setVisible(true);
}
}
It does not compile when I try to run the file (it simply sits there with "running..." forever). When I remove the final line, it does compile.
Any thoughts?
I am attempting to follow the lesson below:
https://www.youtube.com/watch?v=rT-J-0nGyzU

It did in fact successfully compile. The reason it say "running ..." is because when you call setVisible(true) your window becomes visible, and the program keeps running until it's closed.
It sounds like you couldn't see the window, even though the window you created became "visible".
Try adding
// Set the size of the window.
window.setSize(600, 400);
// Position the window in the middle of the screen.
window.setLocationRelativeTo(null);
// End the application when X is pressed.
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
before calling setVisible(true). This should ensure you can see the window you created.

Related

JFileChooser inside a JFrame makes setVisible() freeze

I have an assignment to show JFileChooser as part of a JFrame. So showing it as a dialog box is out.
I'm doing the most basic approach to adding it as a component to a yet invisible frame, and then the setVisible() call freezes instead of showing the frame.
What irks me the most is that one time out of ten the frame appears with the FileChooser just fine. This makes me think this is a concurrency issue.
Here's the minimal source code that still has the issue.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class ApplicationFrame extends JFrame {
JFileChooser fileChooser;
public ApplicationFrame(String frameName) {
super(frameName);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
fileChooser = new JFileChooser();
fileChooser.setControlButtonsAreShown(false);
panel.add(fileChooser, BorderLayout.CENTER);
getContentPane().add(panel);
}
}
public class lab7{
public static void main(String args[])
{
ApplicationFrame windowForApplication = new ApplicationFrame("lab7");
windowForApplication.setSize(600,600);
windowForApplication.setVisible(true);
}
}
If you put a println after the final setVisible, it doesn't get called.
If you comment out panel.add(), the frame displays just fine.
What else should I do to display the file chooser?
What irks me the most is that one time out of ten the frame appears with the FileChooser just fine.
All Swing component should be created on the Event Dispatch Thread. So the GUI creating code should be wrapped in a SwingUtilities.invokeLater(...).
Read the section from the Swing tutorial on Concurrency for more information and an example of how this is done.
Your code (as is) actually works for me without problem. I'm using JDK7 on Windows 7, so it could be a version/platform issue. Again make sure the code executes on the EDT.
Also, class names ("lab7") should start with an upper case character. Doesn't matter if this is a SSCCE or not, be consistent.

Jframe not showing anything and "cannot be cast to java.applet.Applet"

I just want to add a button in my empty frame and it's very very simple.
The frame is not showing anything and although the program runs, IDE tells me :"
java.lang.ClassCastException: Spots cannot be cast to java.applet.Applet
at sun.applet.AppletPanel.createApplet(AppletPanel.java:793)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:722)
at sun.applet.AppletPanel.run(AppletPanel.java:379)
at java.lang.Thread.run(Thread.java:744)"
So I extend the JApplet for it and it doesn't complain anymore, the frame now is grey and still nothing. Also, the title is not showing.
What is interesting is that even if I fully copy the example code on the Oracle tutorial site(Official) : Tutorial Site, the same happens and it compains the applet thing.
Please help and Thank you very much!!!
public class Spots{
private static void createAndShowGUI() {
JFrame frame = new JFrame();
JButton jButton = new JButton("Click Me");
jButton.setSize(20,20);
jButton.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(jButton);
frame.setSize(500, 500);
frame.setTitle("Bar Code Scanner");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
createAndShowGUI();
}
}
To everybody who has this issue-- please check your IDE running setting to see it runs as an applet or application. Thanks for your kindly help again!
If you're making an applet your code shouldn't even have a JFrame variable. Instead in the init method, add your JButton to the applet's contentPane() or to a JPanel that is added to the contentPane as just about any tutorial will tell you.
Of course this doesn't hold if you're not trying to create an applet, but if so, why would you be running it as if it were an applet. Please clarify this for us.
Edit
You ask in comment:
Thanks for the help! but what if I want to do it in Jframe way? Because I don't want to create an applet so I didn't extends Japplet or create init(). But the ide complains "you need to extends applet" Is anywhere in my code telling it I'm creating an applet? I dont want to :(
The IDE shouldn't care what type of class you're creating as long as it compiles. It's when you try to run the code that the JVM might complain that it's not an applet, if 1) you try to run it as an applet called in some HTML code, or 2) try to run it with your IDE's applet simulator. If the former, don't do it. Run it as a stand alone program. If the latter, don't do it. Tell the IDE that you're trying to run a Java program, and for both, make sure that you've got a valid main method.
filename cannot be cast to java.applet.Applet:
check points:
1. extends JFrame -> Extends JApplet
2. public constructor() -> public void init()
3. /public static void main(String[] args) { ... }/. It means no need this method.
4. When you've done all above, then save and compile. Because When you run(appletviewer index.html), you need filename.class. This filename.class should be the one that you've done 1~3 and compile already.

How do you add a jFrame to your main class in Netbeans?

So I have made a Jframe with a lot of elements and buttons and things in it, but I am new to using NetBeans. Upon creating the java application a main class.java was created and upon adding the jframe another jframe.java was created. How do I get the main class to open, read, and run my jframe.java? I can upload the specific code if need be.
Thanks in advance
To call a certain method from another class, you must first create a new object for that class, like this:
Jframe frame = new Jframe();
frame.setVisible(true); //or whatever the method is in jframe.class
Maybe rename the actual class name from jframe to something like frameone. I've heard that naming classes the same as classes in the Java API will cause trouble.
Or, you could put it all in one class, with either two separate methods or put it all in the main method. If this doesn't help, then please paste the exact code on pastebin.org and give a link.
Look at this sample example and learn how to set frame visible
import java.awt.*;
import javax.swing.*;
public class exp{
public static void main(String args[]){
JFrame jf=new JFrame("This is JFrame");
JPanel h=new JPanel();
h.setSize(100,100);
h.add(new JButton("Button"));
h.add(new JLabel("this is JLabel"));
h.setBackground(Color.RED);
jf.add(h);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Useful Links
Designing a Swing GUI in NetBeans IDE
Creating a GUI With Swing (As #MadProgrammer Commented)
Learning Swing with the NetBeans IDE
I'm new to this, but I got a form up. Woo hoo!
1) The project created my main function in japp1.java
2) I created a JFrame, file jfMain.java
3) While there was probably a way to reference it as it was, I didn't see how right away, so I moved it to a peer level with the japp1 file, both in a folder called japp1 which will cause them to get built together, having the same parent reference available.
src\
japp1\
japp1.java
jfMain.java
4) Then instead of creating a generic JFrame with a title, I created an instance of my class...
5) I gave it a size...
7) Then showed it...
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new japp1.jfMain();
frame.setPreferredSize(new Dimension(700, 500));
frame.pack();
frame.setVisible(true);
}
I had already put some code in my jframe... to show a messagedialog with JOptionPane from a mouseclick event on a button and set some text for some textfields.
Hope that helps.

I forget to stop a gui program and run it again. Now I can't close the first program without killing eclipse

I am working on a simple GUI app that just draws some graphics on a canvas. The environment is Vista 64. When I run the program, the Windows resize and minimize buttons work, but the close button doesn't. So I have to press the stop button in Eclipse to kill the program.
But sometimes I forget to press stop, and run the program again. The first instance gets stuck and I can't get rid of it without closing Eclipse. If I get careless I can end up with several java windows I can't close. Is there a way to get control of and close the windows? Also, why does the close button not work?
I doubt the code matters in this case but here it is:
import java.awt.*;
public class RobotFace extends Canvas{
/**
* #param args
*/
public static void main(String[] args) {
RobotFace c = new RobotFace();
c.setBackground(Color.white);
c.setSize(350, 350);
Frame f = new Frame();
f.add(c);
f.setLayout(new FlowLayout());
f.setSize(350,350);
f.setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.black);
int width = 150;
int height = 200;
g.drawRect((getWidth()-width)/2, (getHeight()-height)/2, width, height);
g.setColor(Color.gray);
g.fillRect((getWidth()-width)/2, (getHeight()-height)/2, width, height);
g.setColor(Color.white);
g.fillRect((getWidth()-80)/2, (getHeight()+50)/2, 80, 20);
g.setColor(Color.yellow);
g.fillOval((getWidth()-105)/2, (getHeight()-100)/2, 30, 30);
g.fillOval((getWidth()+45)/2, (getHeight()-100)/2, 30, 30);
}
}
You can also kill it using the console of eclips , there is a red button(right cornor) when you open console view in eclips.
To open console view
Window --> Show View --> Console
using this way you can close the Frame/window
//add window event adapter
f.addWindowListener(new MyWindowAdapter());
class MyWindowAdapter extends WindowAdapter{
MyWindowAdapter(){
}
//implement windowClosing method
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
Your Eclipse console for the first application instance should still be available, just not visible. On the Console tab, you can click the toolbar button that looks like a little blue monitor to switch between application instances. To stop the first application instance, you would select that instance using the little blue monitor (it's called "Display Selected Console"), then click the "Stop" button.
One option is to change the behavior of your program so that it actually exits with the window is close (which is not the default behavior or Frames in Java.
The Frame javadoc specifies that it can generate WindowClosing events, which you can use to trigger your app to close (using a class like #Pratik does in their answer).
In my opinion, a better solution would be to replace your frame with a JFrame and use it's method f.setDefaultCloseOperation(EXIT_ON_CLOSE) so that you application with exit when the window is closed. JFrame is written such that it should be a drop in replacement for awt.Frame, but is part of the swing toolkit.

JInternalFrame acts weird when windows theme changed?

This is my sample code. I am trying to embed a JInternalFrame without titlebar display into a JFrame.
import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameUI;
class A{
public void doThis(){
JFrame fr = new JFrame();
fr.setSize(300,300);
JInternalFrame f = new JInternalFrame();
f.setSize(200,200);
BasicInternalFrameUI ui = (BasicInternalFrameUI) f.getUI();
ui.setNorthPane(null);
f.setVisible(true);
fr.add(f);
fr.setVisible(true);
}
}
class MainA{
public static void main(String a[]){
A obj = new A();
obj.doThis();
}
}
The code works fine and displays a JInternalFrame within a JFrame without titlebar as per the requirement as shown below.
I still have this UI running and at the same time when I try to change my XP theme (via Properties>>Appearance>>Theme), the UI automatically repaints itself to show the JInternalFrame with a titlebar again as shown below.
I just can't understand this bizarre behavior. I have no clue if this is an issue with Java Swing or if it is something related to the OS. Please help me with this!
Why is the UI repainting upon theme change with an enabled titlebar when I explicitly code for the titleBar to be set as null?
PS: OS used is Windows XP and I am not sure if the same behavior is observed in Linux or other versions of Windows
'do' is a keyword in Java, so that code does not compile for me. This code does.
import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameUI;
class A{
public void doIt(){
JFrame fr = new JFrame();
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setSize(300,300);
JInternalFrame f = new JInternalFrame();
f.setSize(200,200);
fr.add(f);
BasicInternalFrameUI ui = (BasicInternalFrameUI) f.getUI();
ui.setNorthPane(null);
f.setVisible(true);
fr.setVisible(true);
}
public static void main(String a[]){
A obj = new A();
obj.doIt();
}
}
Some notes/questions:
Swing GUIs should be constructed & altered on the EDT.
Why does the code add a JInternalFrame directly to anything other than a JDesktopPane?
There are slight issues with sizing of the JInternalFrame when changing themes. I suspect it has to do with the lack of validate()/pack() in the code. Since that was not the question, I could not be bothered investigating it further.
Results
I got a 'null result' here using Windows 7. The title bar of the JInternalFrame did not re-appear at any time when changing through (in order):
Forbidden Planet (a custom, simple theme)
Windows 7 (Aero)
Architecture (Aero)
Windows 7 Basic (Basic & High Contrast)
Windows Classic (Basic & High Contrast)
Forbidden Planet

Categories

Resources