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.
Related
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.
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()) { ... }
import java.awt.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MainClass extends JFrame {
public static void main(String[] args){
JFrame myframe = new JFrame("Mein Programm");
myframe.setSize(600, 400);
myframe.setVisible(true);
myframe.add(new JLabel("Label"));
}
}
I try to make a simple java program and I don´t know how to change the java cup icon. I tried several threads from the forum and all of them don´t work for me.
Thanks for help.
Create a new package/folder called res (resources) with the subfolder icons and put your icon files with different sizes in it. The program will automatically decide at runtime which size is needed, depending on the screen resolution.
// Supply the program with different icon sizes for different resolutions
ArrayList<Image> icons = new ArrayList<>();
icons.add(ImageIO.read(getClass().getResource("/res/icons/tcc_icon-16x16.png")));
icons.add(ImageIO.read(getClass().getResource("/res/icons/tcc_icon-32x32.png")));
icons.add(ImageIO.read(getClass().getResource("/res/icons/tcc_icon-64x64.png")));
icons.add(ImageIO.read(getClass().getResource("/res/icons/tcc_icon-128x128.png")));
setIconImages(icons);
You aren't setting the icon there. To set it, use the JFrame.setIconImage method
myFrame.setIconImage(yourIcon);
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.
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