Why is my Java swing window not showing up? - java

so I'm starting to learn Java Swing, following a YouTube video.. installed Java 8 and NetBeans 8.0 in Linux Ubuntu 14.04. Made a new Java application, and wrote the following code:
package basicswing;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class BasicSwing extends JFrame {
JPanel p = new JPanel();
JButton b = new JButton("Hello");
public static void main(String[] args) {
new BasicSwing();
}
public BasicSwing() {
super("Basic Swing App");
setSize(400,300);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.add(b);
add(p);
setVisible(true);
}
}
When I clean, build, and then run the project, it shows that a Java project is running, but I don't see the Frame, it doesn't pop up.. I don't actually think it's a code issue, I've tried it with different code, and the frame still doesn't show. I can't figure out if it's a NetBeans issue or a Java issue.. or maybe something else?

It is an environment (NetBeans maybe?) issue. Try running from command line:
$ javac BasicSwing.java
$ java BasicSwing
The code is correct and displays the frame.

Related

Why can't I run my .jar file normally? (MacOS)

I just programmed my first JFrame and exported it, but I'm not able to run the .jar file normally. When I try to run it using Terminal, it works without any problems. However, when I try to open the file normally by double clicking on it, I get an error message similar to this: The Java jar file "Programm.jar" could not be launched.
Here's the code (maybe there's something wrong with it?):
package GUI;
import javax.swing.JFrame;
public class Frame {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(500, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setTitle("Programm");
jf.setResizable(false);
}
}
Thanks in advance!
I think you need to associate the file type with the java executable, then the Finder will know what to do when you double click

"The type javax.swing.JFrame is not accessible" What does this mean and how do I fix it?

I'm following along with this tutorial, and I got a problem early in the video (at approximately 7:45). I'm trying to create a basic Java program that will launch a window, however, I can't seem to import JFrame.
I've looked for other solutions on Stack Overflow, but I haven't found one that works for me.
Here is the code I've written:
import javax.swing.JFrame;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World");
frame.setVisible(true);
}
}
I'm using Eclipse version 4.12.0 on a Macbook Pro (13-inch, Mid 2012) running macOS Mojave version 10.14.5
Expected result: A window opens when I run the program, and when I close the window, the program ends.
Actual result: No window is created and I get this error message:
Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: /Users/username/eclipse-workspace/Swing1/bin
Caused by: java.lang.module.InvalidModuleDescriptorException: App.class found in top-level directory (unnamed package not allowed in module)
If you have a module-info.java file, put this in the module:
requires java.desktop;
If you have created the java app with eclipse your fault is the package.
With eclipse, I created a java app and this is the result
This code fixed your foult
package demo;
import java.awt.Dimension;
import javax.swing.JFrame;
public class App {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World");
frame.setLocationRelativeTo(null);
frame.setSize(new Dimension(400, 400));
frame.setVisible(true);
}
}
The reference for understand the package
I had the same issue. I did similar code in Eclipse. I got the error The type javax.swing.JFrame is not accessible on the side of import javax.swing.JFrame;
The solution is:
Delete the line of import javax.swing.JFrame;
And then, inside your body of your code inside your main, with your mouse, hover over JFrame keyword, and Eclipse will offer some auto-completion suggestion.
Select import 'JFrame' (javax.swing)
This will bring the required import automatically. It is a kind of shortcut.
In order to avoid these type of errors: Never type manually, get the imports AND methods, for example setVisible by autofilling. For instance, type frame.setV and again Eclipse will suggest the completion ... select from there. I do not know why, but this is what happened in my case.

How does intelliJ process and compile Swing app code?

I tried building simple Swing forms using IntelliJ's GUI designer tool and noticed some things I don't like (at least till I don't understand). For this question lets consider a very simple Swing form with just one button. I opened up GUI designer and added a button( 'load file') to the middle of mainPanel using GridBagLayout. The java code it generated (I wrote the main method myself):
package mainPackage;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by Povilas on 06/01/2017.
*/
public class MainFrame {
private JButton loadFileButton;
private JPanel mainPanel;
public MainFrame() {
loadFileButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"This should be a browser dialog...");
}
});
}
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
JFrame frame = new JFrame("App");
frame.setContentPane(mainFrame.mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
If i run this code it works, which is crazy and unexpected, because nowhere in the code loadFileButton is initialized, nowhere in the code mainPanel is initialized, nowhere in thecode is LayoutManager added or button added to the panel. If i tried compiling this code from cmd I would get NullPointer exceptions in the constructor because button and panel are not initalized.
Question:
How come intelliJ compiles and runs the app successfully? Does it process .form file at compile time to add missing code? I want to design Swing dialogs with IntelliJ's designer but to get proper, full code which could then be compiled and packaged a from cmd using javac and jar, how do I do that?

java System.setProperty not affecting anti-aliasing; command line option works

I'm trying to make a java Swing program default to anti-aliasing when run on Linux. It works when I pass the settings as command line options or using _JAVA_OPTIONS, but not using System.setProperty().
I'm using Java SE 6 on Centos 5.5 via VNC with gnome metacity WM.
public class Test {
public static void main(String args[]) {
System.setProperty("useSystemAAFontSettings", "lcd");
System.setProperty("swing.aatext", "true");
JFrame frame = new JFrame("Test");
JButton button = new JButton("Button");
frame.getContentPane().add(button);
frame.setSize(100,100);
frame.setVisible(true);
}
}
Any clues what I'm doing wrong? Thanks.
PS: The swing.aatext setting seems to have no effect in any case, but I included it as I saw it as part of the solution elsewhere.

JFace error when setting JFrame visible: Cocoa AWT: Running on AppKit thread 0 when not expected

I'm trying to code my first JFrame for a simple app.
The problem is that as soon as I uncomment the
setVisible(true);
I obtain the following error message:
Cocoa AWT: Running on AppKit thread 0 when not expected.
Config:
Running eclipse on a mac OS 10.10 (Yosemite) and Java is up to date.
Here is the code:
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainFrame extends JFrame {
private JLabel appTitle;
public MainFrame(){
super("Tabum by Team Alpha");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,500);
setLayout(new BorderLayout());
setVisible(true);
}
}
I've never used your tools, but I guessing that the problem is that all GUI code should execute on the Event Dispatch Thread (EDT) and your code is not doing this.
You do this by wrapping your code in a SwingUtilities.invokeLater(...):
EventQueue.invokeLater(new Runnable()
{
public void run()
{
// add your code here
}
});
Read the section from the Swing tutorial on Concurrency for more information.
I couldn't get the solution working with Eclipse on my Mac.
However, when I loaded the exact same solution in IntelliJ IDEA, it worked!
I would classify this as a Eclipse bug.
For reference:
I'm using the current JDK 1.8.0_31
I was using Eclipse Kepler Service Release 2 (20140224-0627)
I'm on a Mac OS X Yosemite
Please let me know if it helps you or if you have a better solution.

Categories

Resources