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

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.

Related

JFrame in full screen without undecorated

I am using Java8 on Windows machine developing with latest community edition of IntelliJ. To make JFrame full screen I find below solution where I faced one different behavior which I want to get verified.
Solution I took from JFrame full screen
As per the solution I need to put three below lines to make JFrame full screen:
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
But in my project I have created a class AppFrame.java that extends JFrame. And in default constructor I set some basic properties like font etc and importantly visibility to true.
import javax.swing.*;
import java.awt.*;
public class AppFrame extends JFrame {
AppFrame() {
Font baseFont = new Font("Dialog", Font.PLAIN, 12);
setFont(baseFont);
setLocationRelativeTo(null);
setBackground(Color.WHITE);
setForeground(Color.black);
setLayout(new FlowLayout());
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
And in class that extends AppFrame when I try to put above three lines (with or without setVisible, which is already coming from AppFrame) to maximize it get below error:
Exception in thread "main" java.awt.IllegalComponentStateException: The frame is displayable. at java.awt.Frame.setUndecorated(Frame.java:923)
As a part of solution (which I want to verify) - Experimentally I removed setVisible (true) from AppFrame.java and it worked, but this would be gonna impact all classes extending AppFrame, so I removed frame.setUndecorated(true); as well from my class and put back setVisible in AppFrame. And exception is gone. Also frame.setUndecorated(true); I believe removes title bar of JFrame.
Also, below is excerpt from javadoc of JFrame:
A frame may have its native decorations (i.e. Frame and Titlebar)
turned off with setUndecorated. This can only be done while the frame
is not displayable.
It would be great if someone can verify this behavior.
By design you have to invoke setUndecorated before and only before setVisible. So you have no other choice but to remove from base class invocation of setVisible and invoke it every time in children classes.
Modify the initializer to use parameters.
AppFrame() { should be changed to AppFrame(boolean undecorated, boolean visible) { then in the initializer add setUndecorated(undecorated); and setVisible(visible);
Completed Solution:
import javax.swing.*;
import java.awt.*;
public class AppFrame extends JFrame {
AppFrame(boolean undecorated, boolean visible) {
Font baseFont = new Font("Dialog", Font.PLAIN, 12);
setFont(baseFont);
setLocationRelativeTo(null);
setBackground(Color.WHITE);
setForeground(Color.black);
setLayout(new FlowLayout());
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(undecorated);
setVisible(visible);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
In regards to:
A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated. This can only be done while the frame is not displayable.
This is just stating that you have to do this before calling setVisible(true);. To determine if you can call setUndecorated safely, you can use if (!isDisplayable()) { ... }

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.

Making a custom icon for a JFrame

Well I was wondering if I could make an icon image for a JFrame. I do know its posible, because, let me say, I am NOT digging the java logo.
Well if I just hava to use a Frame object I will.
Can someone tell me, I know its possible!
Use an ImageIcon.
ImageIcon icon = new ImageIcon( pathToIcon );
yourFrame.setIconImage(icon.getImage());
Good Luck!
First, you have to have an image file on your computer. It can be named anything. For this example, we will call this one "pic.jpg".
Next, you need to include it in the files that your application is using. For example, if you're using NetBeans, you simply click on "Files" in the left hand side of the IDE (not File as in the menu, mind you). Drag the picture's file over to the folder that houses the main package. This will include it for available use in the code.
Inside the method where you define the JFrame, you can create an image like this:
Image frameImage = new ImageIcon("pic.jpg").getImage();
You can now set it as the IconImage for the frame like this:
JFrame frame = new JFrame("Title");
frame.setIconImage(frameImage);
Hope that's helpful.
Note: the reason that the Image object has to be created like this is because Image is abstract and cannot be instantiated by saying new Image();
Props to you, btw, kid. I wish I would have started learning programming when I was your age. Keep at it!
You can do the following.
public Test {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setIconImage(new ImageIcon(Test.class.getResource("image.png"));
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setVisible(true);
frame.setSize(100, 100);
//other stuffs....
}
}

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