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()) { ... }
Related
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.
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.
The following code works when the font line is commented out, and no GUI appears at all when the line is included. From what I can tell its formatted properly but its crashing the GUI. What could cause this?
public class TestCode extends JFrame{
JTextArea jta;
public TestCode(){
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel();
jta = new JTextArea(20, 30);
jta.setFont(new Font("Courier New", Font.PLAIN, 12)); // This line crashes
content.add(jta);
add(content);
pack();
setVisible(true);
}
public static void main (String [] args){
TestCode run = new TestCode();
}
}
I'm beginning to suspect that it has something to do with my system fonts? I have installed extra fonts, perhaps that affects Java's ability to retrieve them?
EDIT:
Just to clarify, there are no errors when I run this program. The GUI never opens and the IDE gets slow and buggy as if I were running an infinite loop. The program must be terminated via the IDE (because no GUI shows to close).
It works fine for me using 1.6 and 1.7.
Some suggestions:
1) Force the EDT for your Swing app as follows:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TestCode run = new TestCode();
}
});
}
Further reading:Concurreny in Swing
2) Place your JTextArea in a JScrollPane, and add the scroll pane to the panel, not the text area itself:
content.add(new JScrollPane(jta));
The problem was caused by an excessive number of downloaded fonts on my system. I had previously downloaded a Font package which contained a few thousand additional fonts which caused the IDE to spend an unnecessarily long amount of time trying to find the right font I presume.
Deleting the unused additional fonts solved the problem and now this code works fine.
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
Currently I have a very basic file viewer working as follows :
- in JOptionPane I browse for files, and set some variables to display (colors, line connecting etc)
- previous windows loads a frame with drawn points
alt text http://img190.imageshack.us/img190/4443/104bu.jpg
Code :
http://paste.pocoo.org/show/220066/
Now I'd like to throw it into one window, with JMenu for selecting files and changing display parameters. How to get started ? Should I rewrite everything to JDialog ?
alt text http://img684.imageshack.us/img684/5264/lab10db.jpg
If you want the JOPtionPane as a child of the main JFrame, then add it as a child. Of course it will then cover your dots. Hence you will have to not draw your dots directly in the content pane of the main JFrame, but rather in a new JPanel that you have also added to the JFRame's content pane. Let me know if I've understood the question whatsoever.
Here's some code for how I see the setup (I'm leaving the layout problem out of this, partly because it depends on what you want to see):
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(400,400));
frame.getContentPane().add(new JOptionPane());
JPanel canvasForDots = new JPanel();
frame.getContentPane().add(canvasForDots);
You might also like to look at How to Use Tool Bars and How to Use Menus. ImageApp is a typical implementation that associates menu items with the corresponding Action instances.
private class ClearAction extends AbstractAction {…}
private class ImageOpenAction extends AbstractAction {}
private Action openAction = new ImageOpenAction("Open");
private Action clearAction = new ClearAction("Clear");
…
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(openAction));
menu.add(new JMenuItem(clearAction));
This related example adds the file chooser directly to the main frame. Here's a more elaborate example of connecting lines and shapes using the same principles.