Swing apps doesn't run - java

I have a problem in using swing. I don't know what is the cause of this but i'm just trying to create a simple frame app using swing at it give me a lot of error.
import javax.swing.JFrame;
public class StacksGui {
public static void main(String args[])
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(100,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JRootPane.createGlassPane(Unknown Source)
at javax.swing.JRootPane.<init>(Unknown Source)
at javax.swing.JFrame.createRootPane(Unknown Source)
at javax.swing.JFrame.frameInit(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at StacksGui.main(StacksGui.java:9)
Caused by: java.lang.IllegalArgumentException: 0 incompatible with Text-specific LCD contrast key
at java.awt.RenderingHints.put(Unknown Source)
at sun.awt.windows.WDesktopProperties.getDesktopAAHints(Unknown Source)
at sun.awt.windows.WToolkit.getDesktopAAHints(Unknown Source)
at sun.awt.SunToolkit.getDesktopFontHints(Unknown Source)
at sun.awt.windows.WDesktopProperties.getProperties(Unknown Source)
at sun.awt.windows.WToolkit.updateProperties(Unknown Source)
at sun.awt.windows.WToolkit.lazilyInitWProps(Unknown Source)
at sun.awt.windows.WToolkit.lazilyLoadDesktopProperty(Unknown Source)
at java.awt.Toolkit.getDesktopProperty(Unknown Source)
at javax.swing.UIManager.<clinit>(Unknown Source)
... 10 more

I had problem when all java Swing apps crashed silently.
After trying to launch from command line got the following exception:
Caused by: java.lang.IllegalArgumentException: 181193932 incompatible with Text-specific LCD contrast key
As it's said in JDK-6503988, the problem is connected with ClearType registry setting FontSmoothingGamma.
This can be fixed by running "Adjust ClearType text" from Control panel (cttune.exe) or by changing the registry directly:
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /t REG_DWORD /v FontSmoothingGamma /d 1024 /f
A reboot may be needed for changes to take the effect.
See also a 10-year-old investigation of the same problem at https://www.rarst.net/software/cleartype-install4j-java-bug/.

This code works for me:
package test;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(100,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I only see a difference at
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
where I added the frame. However your stacktrace looks like there's another error, so first try this and if it still don't work, give us a bit more code to look at.

Related

How can I fix "javax.swing.ImageIcon.<init>(Unknown Source)"?

I'm making a simple program of a PONG game, and my problem comes from the Main method which i wanted to set an image icon and it shoots me a NullPointerException.
I'm working with Eclipse IDE 2019,06 and the Java Compiler 12.0.1. The image that I am using is .jpg.
I've tried to change the directory of the image and nothing :(.
The directory of the image is in a folder which is in the same folder of the main class.
This is my frame code:
public class Menu {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu window = new Menu();
window.frame.setVisible(true);
window.frame.setLocationRelativeTo(null);
window.frame.setTitle("PONG!");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Menu() {
frame.setIconImage(new ImageIcon(Menu.class.getResource("/icon.jpg")).getImage());
initialize();
}
So this is the error message:
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui.Menu.<init>(Menu.java:57)
at gui.Menu$1.run(Menu.java:42)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I hope you can help me with this problem. Thanks.
I guess line 57 of Menu.java is this line:
frame.setIconImage(new ImageIcon(Menu.class.getResource("/icon.jpg")).getImage());
It looks like frame has not been initialized and therefore is null. That's why you get a NullPointerException.
When you work with the Maven build system (e.g. netbeans 11.3, maven 3.3.x), you have to take care where you place your resources. The "/icon.jpg" path is relative to your resources directory. Try to create a directory named "resources" (sic!) as a subdirectory of the java main source directory. For example, the path to "resources" should be
<Project root>/src/main/resources
You place your image files in this directory or a maybe a subdirectory /src/main/resources/icons/icon.jpg

JavaFX NullPointerException Location is required NetBeans [duplicate]

This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 2 years ago.
i've tried anything else i found on stack stackoverflow and i really dont get it why this doesn't work.
I won't show you the code of my application that is not working, because it isn't working even with the example project.
So here is the problem:
When i create new JavaFX Application with the sample code that gives button which prints hello world after clicked, this works when i run this as a desktop application and when i build this and start in browser. This works perfectly as desktop and as browser application
But when i create new JavaFX FXML Application which is almost the same as above but stage is defined by fxml and css not byte he code. This one works perfectly as windows application but doesnt work as a browser application
java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
... 11 more
here is the code of working application:
public class JavaFXApplication4 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
and this application doesnt work in browser and throws exception:
public class JavaFXApplication3 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Can someone help me?
The problem is in this line:
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
It is pretty clear that the load method is actually being called with a null argument. And that happens because getResource("/sample/sample.fxml") cannot file that resource.
The resource is missing (or has the wrong path) on the runtime classpath.
Source
The problem has to do with signing but I do not exactly understand what that means. Then I searched for how to run a JavaFX application in a browser with netbeans and found this: http://docs.oracle.com/javafx/2/fxml_get_started/fxml_deployment.htm
In netbeans 8.2 in the project properties under Build > Deployment there is a check-box:
Request unrestricted access (Enable signing)
With this it works. Good luck.
In some case, if you using maven you should move all your fxml files to the resources directory and use a relative address from there.
If you want to use stage.setScene() function inside a controller class, it's better to use:
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
if u are using different packages then cut that file from other package and paste it in main package. And then try to run . I have tried that approach and its working fine now .

JXMapKit/-Viewer extremely slow as webstartable - where to start digging?

Just tried to add some swingx-ws components to the overall swinglabs demos - and noticed that a simple JXMapKit/-Viewer is orders of magnitude slower to load the tiles in the webstartable compared to loading locally.
Rather lost on where I should start looking (ui updates seem to be on the EDT, though might need a closer look):
anybody else experiencing the different loading times?
any guess on what might be the reason?
how to debug a webstartable?
The code is rather straightforward (to run locally, you'll need swingx and swingx-ws:
public class WSDemo {
private JComponent createContent() {
JComponent content = new JPanel();
content.setLayout(new BorderLayout());
content.add(createMapKit());
return content;
}
protected JComponent createMapKit() {
final int max = 17;
TileFactoryInfo info = new TileFactoryInfo(1, max - 2, max, 256, true,
true, // tile size is 256 and x/y orientation is normal
"http://tile.openstreetmap.org",// 5/15/10.png",
"x", "y", "z") {
public String getTileUrl(int x, int y, int zoom) {
zoom = max - zoom;
String url = this.baseURL + "/" + zoom + "/" + x + "/" + y
+ ".png";
return url;
}
};
DefaultTileFactory tf = new DefaultTileFactory(info);
tf.setThreadPoolSize(1);
final JXMapKit kit = new JXMapKit();
kit.setTileFactory(tf);
kit.setZoom(10);
kit.setAddressLocation(new GeoPosition(51.5, 0));
kit.getMainMap().setDrawTileBorders(true);
return kit;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new WSDemo().createContent());
frame.setLocationByPlatform(true);
frame.setSize(400, 400);
frame.setVisible(true);
}
});
}
}
Edit:
seems like it's somehow related to permission checking in the web context: profiling shows that the whole connection call stack is different (not overly surprising) and takes ages. Giving up for now ..
Edit 2:
There seem to be 2 separate issues
the somewhat longer time it takes to open the connections for loading the tiles in the security restricted context, that is the hotspot in JavaWebStartSecurity.checkConnect(String, int), as #Howard already noted.
a rather weird blocking of the EDT that seems to happen only if the mapKit is used in a SingleFrameApplication (of BSAF)
To reproduce the blocking, run the SimpleWSDemoApp, wait until the map is visible (takes some time, that's the first issue) then use the mouse to move the zoom thumb quickly up and down: the ui is completely blocked. Doing the same on the plain frame (the reference on top) has the initial loading wait, but couldn't ever reproduce the blocking.
The weird thingy (to me) is what blocks the EDT, from the thread dump of VisualVM:
"AWT-EventQueue-0" prio=6 tid=0x063d3000 nid=0x1468 waiting for monitor entry [0x05efe000]
java.lang.Thread.State: BLOCKED (on object monitor)
at java.security.Permissions.implies(Unknown Source)
- waiting to lock <0x29f7b118> (a java.security.Permissions)
at sun.security.provider.PolicyFile.implies(Unknown Source)
at java.security.ProtectionDomain.implies(Unknown Source)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkSystemClipboardAccess(Unknown Source)
at java.awt.event.InputEvent.canAccessSystemClipboard(Unknown Source)
at java.awt.event.InputEvent.<init>(Unknown Source)
at java.awt.event.MouseEvent.<init>(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
that is dragging the mouse kicks in checking permission for clipboard access ...
A Web Start app is also a JVM process so you can try to profile it with VisualVM (this entry describes how to do that). It's also worth to profile both applications with VisualVM and compare JVM settings like heap size, JIT compiler differences. I think that a Web Start application runs with client compiler, which is slower by means of produced native code than a server compiler.

How to call a sighned Applet which returns a value from GWT on button click

I am trying to call an sighned applet on by GWT button click. I tried going through the gwtai method of creating an interface on client side then writing an Applet which implements the interface.Howevere I get a TargetInvokationException when I call the method on my onClick() event.
Here is the code I wrote :
screenCap.addListener(new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
String filePath = counterApplet.captureScreenShot(); (Throws exception here)
TextField filePathT = new TextField();
My Applet Interface :
import com.google.gwt.gwtai.applet.client.*;
#ImplementingClass(com.jpmorgan.rd.querymanager.applet.imageCaptureAppletImpl.class)
#Height("10")
#Width("10")
#Archive("GwtAI-Client.jar,GwtAI-Demo.jar")
public interface imageCaptureApplet extends Applet {
String captureScreenShot();
}
My applet implementation class :
import java.awt.*;
import javax.swing.JApplet;
public class imageCaptureAppletImpl extends JApplet implements imageCaptureApplet{
private static final long serialVersionUID = -511774647966368672L;
public String captureScreenShot() {
String file;
// do something
return file;
}
And the full stack trace:
load: class com.amol.test.imageCaptureAppletImpl.class not found.
java.lang.ClassNotFoundException: com.jpmorgan.rd.querymanager.server.imageCaptureAppletImpl.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed:http://127.0.0.1:8888/com/amol/test/imageCaptureAppletImpl/class.class
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 9 more
Exception: java.lang.ClassNotFoundException: com.amol.test.imageCaptureAppletImpl.class
Any help around this is Appreciated.
I have also created a signed jar of my applet can you please suggest if there is a better way of calling this captureScreenShot() method from my GWT page on button click and getting a result back.
Also is JSNI a better way of doing this. If so can you show a example where a applet which passes a value back is called.
Many thanks in advanced.
PS: I used this page for the basis of my code on gwtai :
http://code.google.com/p/gwtai/wiki/GettingStarted
Thanks,
Amol
GwtAI is using JSNI to communicate with the applet. So whether you are writing the JSNI code yourself or using this library will be the same. Can you post the full stacktrace of your error?

JApplet loading problem

I want to convert an java application to applet, but I an having problems to load it in the browser I presume this is because of the package.
package com.applet;
import java.applet.Applet;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
//import javax.swing.JOptionPane;
#SuppressWarnings("serial")
public class AppletDriver extends Applet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
CleanerPanel cFrame = new CleanerPanel();
add(cFrame);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
this is the code I am using to call the applet when I run it in Eclipse it is working.
this is the html code:
<applet archive="app.jar" code="bin/com/applet/AppletDriver.class" width=350 height=200>
</applet>
the app.jar is in the main dir of the eclipse project any suggestions ?
error from browser java console:
java.lang.NoClassDefFoundError: bin/com/applet/AppletDriver (wrong name: com/applet/AppletDriver)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: bin/com/applet/AppletDriver (wrong name: com/applet/AppletDriver)
Remove the bin\ from the start of the code attribute value, and use / instead of \ (we're not in Washington anymore). If the jar is in a directory named bin then you'll need to use archive="bin/app.jar".
Actually looking at the stack trace, the jar has been constructed incorrectly. The class file should be in a directory com/applet, not bin/com/applet.

Categories