Java - Pause initComponents from running? - java

I am building a JApplet in NetBeans. When the application is initially ran it needs to download some files onto local PC first for it to properly work. Once it is done downloading these files than the GUI should be drawn. How do I pause the JApplet from drawing the GUI until the files are downloaded? Please also note I will need to show the user another GUI that indicates that files are being downloaded, what is the best way of solving this problem? Thank you.
#Override
public void init() {
//THIS IS WHERE THE CODE FOR DOWNLOADING FILES SHOULD BE AND ITS GUI
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
initComponents();//Draws the main GUI after files have been downloaded
}
});
} catch (Exception ex) {}
}

Sounds like you need a Splash Screen.

Related

Substance Look and Feel Applet alert during startup

recently I have discovered substance. When i try to load it in my java program (not applet!) I get errors during startup.
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
try
{
UIManager.setLookAndFeel(new SubstanceGraphiteAquaLookAndFeel());
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
And those are the errors (Note: Those are pop ups, where --message-- is the title, and {name} are buttons):
--APPLET ALERT-- The applet is attempting to connect to jar: [...]/substance-6.0/org/pushingpixel/substance/api/skin/graphite.colorschemes. {Allow} {Disallow} {Stop Applet}
--APPLET ALERT-- The applet is attempting to invoke the java/lang/System.getenv() operatoin on KDE_FULL_SESSION {Allow} {Disallow} {Stop Applet}
The last message appears 5 times whilst writing the following into stderr
-->> returning Frame NULL
BaseDialog: owner frame is a java.awt.Frame
Also my first window will be loaded with the default swing ui.
After closing this and opening a new one (programmatically) the ui will be initialized.
Is there any way I can bypass the error?
Edit: I use the following libraries: laf-plugin-7.2; laf-widget-7.0; substance-6.0
I downloaded a fork from: https://github.com/Insubstantial/insubstantial/downloads
The other one was not signed because i compiled it myself.

Java applet doesn't draw or accept mouse inputs in browser, but does in Eclipse Appletviewer

I'm doing a personal project called "Electromagnetic Field Hockey" (named after Electric Field Hockey on The University of Colorado's PhET website). I've decided to port the application to a java applet on my website.
The problem is that the applet will not accept any mouse input, and I don't even know if it started because the graphics disappear when I switch tabs and back. I know the applet is finding the code and codebase because the applet initializes and renders the first frame of the graphics correctly, then does nothing. What's peculiar is the applet works fine in Eclipse's Appletviewer. I suspect that the culprit is the start() method, which I think somehow isn't being executed in a browser.
In case it helps, you can find the applet at http://leo.scruffohio.net/programs/EMFieldHockey.html. I tested it with Firefox on Linux and Safari on a Mac so it's not a system-dependent issue. Note that the application works when you run the jar file (located at http://leo.scruffohio.net/programs/java/thebombzen-emhockey-dev3.jar).
My applet code is very simple:
public class EMFieldHockeyApplet extends JApplet {
private static final long serialVersionUID = 8145754973708683690L;
#Override
public void init() {
this.setBackground(Color.WHITE);
this.setLayout(new GridLayout());
this.add(ElectromagneticFieldHockey.getInstance());
// that's the main application JPanel
}
#Override
public void start() {
ElectromagneticFieldHockey.start();
}
#Override
public void stop(){
ElectromagneticFieldHockey.stop();
}
}
It seems that problem in your jar signing, try with it
http://docs.oracle.com/javase/tutorial/deployment/jar/signing.html

Adding an applet to a website

First time posting here. I made a simple calculator program using java and I am trying to put it onto my website. From what I've gathered from previous help posts is that I need to create a JApplet with all my program contents and compress it into a .jar file. Then I need to create a .JNLP file, which describes how to applet should be launched.
So here is where I am having trouble.
package calculator;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
public class CalculatorApplet extends JApplet {
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run() {
Calculator calc = new Calculator();
add(calc);
}
});
}
catch(Exception e)
{
System.err.println("GUI creation failed");
}
}
}
It seems my applet was not constructed properly. Whenever I run it a "java.lang.reflect.InvocationTargetException" is thrown. Whenever I run my Calculator class independently from the applet it works as expected. Any ideas where the source of my error is?
I think JNLP files are used for Java Web Start. That's something you won't need with an average Java applet. Please do correct me if I'm wrong.
If you have the working .jar file, an HTML file that calls the applet will be sufficient to run the applet. Insert the code <applet width="300" height="300" archive="jar.jar" code="class.class"></applet> into an HTML file, where class.class is the class extending Applet or JApplet and jar.jar the location of the jar file. Loading the HTML file in a browser will display the applet.
Alternatively, you can use Java's Applet Viewer to open the HTML page and open the applet locally.
You can certainly use JNLP with applets.
See https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/runAppletFunction.html and https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/embeddingJNLPFileInWebPage.html

Java - Applet calling/invoking another Applet

I wrote earlier about the following problem and received an answer to use either Splash Screen or JDialog. As I was researching about the above 2 solutions, now I think that I might be able to solve my problem by using another applet.
The problem: Before my main applet GUI runs I need to download certain files to local PC for the GUI to work. Therefore, I am now thinking of having 2 applets where Applet1 downloads the files, Applet2 is the main GUI.
I would use the Splash Screen or JDialog but at the moment they don't seem to be what I need. How can I invoke Applet2 from Applet1 automatically in the same window, and fully close Applet1 once Applet1 is done downloading files? Is the Applet idea better solution for my problem than Splash Screen or JDialog?
Here is the code of my main applet (in this case it would be Applet2):
#Override
public void init() {
/* Create and display the Applet2 once Applet1 is done */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
initComponents();//Draw the GUI
}
});
} catch (Exception ex) {}
}
My earilier post:
Java - Pause initComponents from running?

Making java tooltips disappear with application switch

I use JComponent.setToolTipText to get tooltips in my java application, but unlike, e.g., eclipse, if I switch to another application when a tip is showing, it remains hovering over what I'm doing until I switch back to the java app and move the mouse around to get it off the element before returning to the alternate application. Is there a way to make the tooltips in my app better citizens? to make them disappear when the app loses focus?
I'm on a mac if relevant.
I've found a solution! (with some input from a colleague)
<myJFrame>.addWindowFocusListener(new WindowFocusListener() {
#Override #SuppressWarnings("unused")
public void windowLostFocus(WindowEvent e) {
ToolTipManager.sharedInstance().setEnabled(false);
}
#Override #SuppressWarnings("unused")
public void windowGainedFocus(WindowEvent e) {
ToolTipManager.sharedInstance().setEnabled(true);
}
});
A WindowAdapter listening to windowActivated/Deactivated also works.
A plain FocusListener does not.

Categories