JPanel doesn't display in JFrame - jframe

I'm making a JFrame that will load pictures from the Internet. I have that working, but the problem with this JFrame is that there are many pictures, and so they take quite a while to load. This is fine, but I would like to show the user that the pictures are loading. For some reason, I can't get the JPanel to display in the loading JFrame. I know this is a common error, and I've tried many fixes, but none of them work. Here is the code:
final JFrame weatherLoadPop=new JFrame("Loading weather...");
weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
weatherLoadPop.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
weatherPop.dispose();
};
});
weatherLoadPop.setResizable(false);
weatherLoadPop.setBounds(100,50,225,100);
JPanel weatherLoadPanel=new JPanel();
weatherLoadPanel.setBackground(Color.RED);
weatherLoadPanel.setPreferredSize(new Dimension(225,100));
JLabel weatherLoadLabel=new JLabel("Loading...0%");
weatherLoadPanel.add(weatherLoadLabel);
weatherLoadPop.add(weatherLoadPanel);
weatherLoadPop.pack();
weatherLoadPop.validate();
weatherLoadPop.setVisible(true);
I'm not sure I'm using pack() and validate() correctly. I don't use them often. In any case, removing them does not help. The strangest part of this problem, to me, is that the JFrame that loads the pictures works beautifully, while the much simpler loading JFrame doesn't.
Thanks for any help.

It's working fine here. Maybe you should provide an sscce that we can test?
I had to change your event listener to dispose weatherLoadPop instead of weatherPop and added your code into a test class:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
final JFrame weatherLoadPop = new JFrame("Loading weather...");
weatherLoadPop.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
weatherLoadPop.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
weatherLoadPop.dispose();
}
;
});
weatherLoadPop.setResizable(false);
weatherLoadPop.setBounds(100, 50, 225, 100);
JPanel weatherLoadPanel = new JPanel();
weatherLoadPanel.setBackground(Color.RED);
weatherLoadPanel.setPreferredSize(new Dimension(225, 100));
JLabel weatherLoadLabel = new JLabel("Loading...0%");
weatherLoadPanel.add(weatherLoadLabel);
weatherLoadPop.add(weatherLoadPanel);
weatherLoadPop.pack();
weatherLoadPop.validate();
weatherLoadPop.setVisible(true);
}
}
and I'm getting:
using:
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)

Related

JavaFX WebView usage, transparent background, jdk8 vs jdk9

I have some code snipped out of a much bigger app, which renders some white text on a black background in a JavaFX WebView. The background colour of the page is set to transparent, using some code from Transparent background in the WebView in JavaFX
import java.lang.reflect.Field;
import org.w3c.dom.Document;
import com.sun.webkit.WebPage;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class TestWebView extends Application {
public void start(Stage stage) throws Exception {
StackPane stackpane = new StackPane();
Scene scene = new Scene(stackpane, stage.getWidth(), stage.getHeight(), Color.BLACK);
stage.setScene(scene);
scene.setFill(Color.BLACK);
stackpane.setStyle("-fx-background-color: BLACK");
WebView webview = new WebView();
stackpane.getChildren().add(webview);
WebEngine webengine = webview.getEngine();
webengine.documentProperty().addListener(new WebDocumentListener(webengine));
webengine.loadContent("<p style='color:white'>Hello World</p>");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
protected class WebDocumentListener implements ChangeListener<Document> {
private final WebEngine wdl_webEngine;
public WebDocumentListener(WebEngine webEngine) {
wdl_webEngine = webEngine;
}
#Override
public void changed(ObservableValue<? extends Document> arg0, Document arg1, Document arg2) {
try {
Field f = wdl_webEngine.getClass().getDeclaredField("page");
f.setAccessible(true);
com.sun.webkit.WebPage page = (WebPage) f.get(wdl_webEngine);
page.setBackgroundColor((new java.awt.Color(0, 0, 0, 0)).getRGB());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Testing on MacOS 10.11.6, with Oracle's JDK:
With JDK 1.8.0_152, this code works nicely - I get white text on black. (And the transparency works too when I layer things underneath it in the stackpane)
With JDK 9 (9+181), com.sun.webkit.WebPage is no longer accessible, so I have to compile and run it with --add-exports javafx.web/com.sun.webkit=ALL-UNNAMED - but having done that, I get black text on a black screen. I can tell the text is there by selecting the text and dragging it, which makes the text appear white while being dragged.
Ideally, I'd like to keep a single codebase that works for both JDK 8 and 9. (Java's usually been good to me with backward compatibility). Or as a second best, how do I get the white text I'm expecting in JDK 9?
Can anyone point me in the right direction? Many thanks in advance.
I had the same issue, I solved it by going further in the reflective process :
Field f = webEngine.getClass().getDeclaredField("page");
f.setAccessible(true);
Object page = f.get(webEngine);
Method m = page.getClass().getMethod("setBackgroundColor", int.class);
m.setAccessible(true);
m.invoke(page, (new java.awt.Color(0, 0, 0, 0)).getRGB());

JFXPanel throws UnsatisfiedLinkError in java-9 - how to fix?

Just tried to run my old tests in java-9 and see them not running at all due to an exception thrown by the code that guarantees running on the FX-threaad (the ol' trick to instantiate a JFXPanel)
The stand-alone example below (it's the plain tutorial code) throws it as well:
Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\java\jdk\190_ea\bin\awt.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1935)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1841)
at java.lang.Runtime.loadLibrary0(Runtime.java:874)
at java.lang.System.loadLibrary(System.java:1770)
at java.awt.Toolkit$3.run(Toolkit.java:1355)
at java.awt.Toolkit$3.run(Toolkit.java:1353)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.loadLibraries(Toolkit.java:1352)
at java.awt.Toolkit.<clinit>(Toolkit.java:1387)
at java.awt.EventQueue.invokeLater(EventQueue.java:1268)
at javax.swing.SwingUtilities.invokeLater(SwingUtilities.java:1381)
at de.swingempire.fx.swing.JFXPanelExample.main(JFXPanelExample.java:59)
Environment is win7, jdk9-ea-107 (without jigsaw), eclipse-neon-ea - questions are simple: a) what's wrong exactly, b) how to fix?
The exact output of java -version is:
java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+107-2016-02-24-175644.javare.4520.nc)
Java HotSpot(TM) Client VM (build 9-ea+107-2016-02-24-175644.javare.4520.nc, mixed mode)
The code:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class JFXPanelExample {
private static void initAndShowGUI() {
// This method is invoked on the EDT thread
JFrame frame = new JFrame("Swing and JavaFX");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX(fxPanel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.ALICEBLUE);
Text text = new Text();
text.setX(40);
text.setY(100);
text.setFont(new Font(25));
text.setText("Welcome JavaFX!");
root.getChildren().add(text);
return (scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initAndShowGUI();
}
});
}
}
Update
tried both suggestions in the comments (updating to most recent 9-ea-107, running from the command line) - no success, same exception.
Another observation: the example above fails with the same stacktrace even when all fx related code is commented - plain swing won't run. Looks like something severely wrong in my environment.

JScrollPane in Eclipse by using palette

I am trying to make a JFrame scrollable by using Palette. In Netbeans if I make a panel with dimensions (300, 500) and a ScrollPane with dimensions (200,200) then if I drag and drop the panel into the ScrollPane it creates automatically the bars.
In eclipse I tried it with the same way and I cannot make it. Moreover the final code in eclipse after the attempt is the following:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
public class InsertWaterRawData extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InsertWaterRawData frame = new InsertWaterRawData();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InsertWaterRawData() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 577, 383);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(236, 87, 200, 200);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
}
}
Is there any way to make it with the palette ??
Thanks in advance
panel.setLayout(null);
Don't use a null layout!!!
The scrollbars will appear automatically when the preferred size of the panel is greater than the size of the scrollpane.
When you use a layout manager the preferred size of the panel will be calculated automatically as you add components to the panel.
If you are doing custom painting on your panel, then you need to override the getPreferredSize() method of your panel to return an appropriate size.
Based on the code you posted there is no need for scrollbars because no components have been added to the panel.

How to create a javafx 2.0 application MDI

How to implement something kinda internal frame in JavaFx 2.0 specifically?
My attempt is as so..
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
ConnectDb connection;
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage stage) throws Exception {
final Stage stage1 = new Stage();
StackPane pane = new StackPane();
Button btn = new Button("Click Me");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
connection = new ConnectDb();
try {
connection.start(stage1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Fire some thing..");
}
});
pane.getChildren().add(btn);
stage.setScene(new Scene(pane ,200, 300));
stage.show();
}
}
ConnectDb.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ConnectDb extends Application {
#Override
public void start(Stage stage) throws Exception {
StackPane pane = new StackPane();
Button btn = new Button("Click On Button which is me");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Something here..");
}
});
pane.getChildren().add(btn);
stage.setScene(new Scene(pane ,200, 300));
stage.show();
}
}
First of all, for your approach, you don't really need to (and therefore should not) extend ConnectDb from Application as you just use the start method to create new stages. You just need one Application class (in your case Main). You could just as well create the new stage/scene in your first event handler.
Secondly, there is no real MDI support in JavaFX 2.1. Right now, you can just have multiple Stages (which is the equivalent to having multiple windows/frames). But you cannot have something like an internal frame in a desktop pane.
I guess you could take the following actions:
Just use multiple Stages (windows) with the drawback that they will float quite uninspiredly on your desktop
Use Swing as a container (with JDesktopPane and JInternalFrame) and integrate JavaFX (here's a nice How-To)
Implement your own framework that emulates MDI behavior
Find a framework that provides MDI behavior
Wait for a future release of JavaFX that hopefully provides MDI support (as far as I know, there's a change request pending...)
Create parent AncorPane.
Add several children AnchorPanes to it. They will serve as internal frames. Add different content to these.
Set children AnchorPanes invisible.
Add buttons to hide, resize or close children AnchorPanes. When needed, call function to set all children AnchorPanes invisible, except for one.

Mean of this alert message box in Android

I am creating a simple application in Android. When I compile and run it in the emulator, it's showing an alert box like this; what it does mean?
sorry, The application simplegame (process com.example.simplegame) has stopped unexpectedly. Please try again!
I'm getting this alert box always? Please explain?
UPDATE:
just trying to import a Picture in it,
import android.app.Activity;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
public class SimpleGame extends Activity {
/** Called when the activity is first created. */
private Bitmap mBackGroundImage;
private Canvas canvas;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mBackGroundImage = hBitmapFactory(R.drawable.background1);
canvas.drawBitmap(mBackGroundImage, 0, 0,null);
setContentView(R.layout.main);
}
private Bitmap hBitmapFactory(int background1)
{
// TODO Auto-generated method stub
return null;
}
}
am newable to android.thats why getting some troubles.
your application is crashing, there must be some exception in your app. Please check the log .