splashscreen is not visible in netbeans - java

[enter link description here][1]I tried an application to display a GIF file. But my application is not showing any error instead GIf ( splash screen ) is not visible.
I have given a code in manifest file :
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build SplashScreen-Image:C:\Users\Admin\Documents\NetBeansProjects\splash\src\splash\try5.gif
-splash:src\splash\try5.gif
the above code in VM options.
In my main class i used this code
public static void main(String[] args) {
sleepThread();
java.awt.EventQueue.invokeLater(new Runnable(){
#Override
public void run()
{
new welcome().setVisible(true);
}
});
}
private static void sleepThread() {
try
{
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
// Do something, if there is a exception
System.out.println(ex.toString());
}
}
// TODO code application logic here
}
But when i tried running my application it doesnot display my Splashscreen. Is there any specification for the size of the Splashscreen GIf because my file is 2.63 MB and its dimensions are 640 * 360. Kindly help me.
EDIT : I USED THE SAME CODING BUT TRIED A JPG IMAGE AS A SPLASH SCREEN IT WORKED WELL. THEN AGAIN I CHANGED IT TO .GIF FILE THE SPLASH SCREEN DID NOT APPEAR AND ALSO AGAIN I CHANGED MY FILE WITH JPG FILE THIS TIME THIS JPG FILE ALSO DID NOT WORK.
EDIT : [1]: http://giphy.com/gifs/thank-you-cute-a3IWyhkEC0p32
Here is the link i have given for a sample gif file. But please note that my gif file size is 2.53 MB.
EDIT : Now this Gif file works perfectly. But after dis splash screen stops my Jframe should open. how do i map it so dat it if i run my program it First displays my Splashscreen den my Frame.

Oops!
I didn't notice. It is better to use SwingUtilities for forms and swing objects to work with. // Change the sleep to 5000, it is 2000 now.
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class test {
public static void main(String[] args) {
final JDialog frame = new JDialog(new JFrame());
frame.setSize(320, 240);
frame.setContentPane(new JLabel(new ImageIcon("H:\\walk.gif")));
frame.setUndecorated(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
new Thread(new Runnable() {
#Override
public void run() {
sleepThread();
CloseDialog(frame);
System.exit(0);
}
}).start();
ShowDialog(frame);
}
private static void sleepThread() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
// Do something, if there is a exception
System.out.println(ex.toString());
}
}
private static void ShowDialog(final JDialog dialog) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog.setVisible(true);
}
});
}
private static void CloseDialog(final JDialog dialog) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog.setVisible(false);
}
});
}
}

swap commands, and you will be fine. Show the window, then sleep. That is how splash screen work, I think :)
public static void main(String[] args) {
new welcome().setVisible(true);
java.awt.EventQueue.invokeLater(new Runnable(){
#Override
public void run()
{
sleepThread();
}
});
}
private static void sleepThread() {
try
{
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
// Do something, if there is a exception
System.out.println(ex.toString());
}
}
// TODO code application logic here
}

Related

How can I insert a login JFrame into a test in Selenium?

When I call the JFrame from another class to test, the JFrame works, but when I insert it into my Selenium test, the JFrame and the browser open so quickly that there's no time to input anything and the test appears like passed.
Sample of the elements that I mention:
#Test
public void Run() throws InterruptedException{
Keys.loginFrame(); //This method is static in another class named 'Keys'
...
}
Is there any way to say to Selenium the following?:
Execute first ONLY Keys.loginFrame();, then wait till the JFrame is closed. Finally execute the rest of the test code.
Thank you for your answers!
remake your loginFrame() method to return JFrame object
add this method:
public static void startFrameThread(JFrame frame) {
Thread thread = new Thread() {
public void run() {
while (frame.isVisible()) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
}
}
}
};
thread.start();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent arg0) {
frame.setVisible(false);
}
});
try {
thread.join();
} catch (InterruptedException e) {
}
}
apply new usage:
JFrame loginFrame = Keys.loginFrame();
loginFrame.setVisible(true); // if not setting visible in loginFrame() method
startFrameThread(loginFrame);

propertyChangeListener doesn't detect JTree's change in preferred size when opening a folder

When opening a file in a JTree, the tree's preferred size changes but his propertyChangeListener doesn't detect it (but if you change it calling setPreferredSize is able to detect it). Is it my code that is wrong or is javax.swing bugged? If this isn't the way of doing this how should I do it. I tested the code with maximumSize as well.
Here is my code, you shouldn't need any external resources:
import javax.swing.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class MyJFrame extends JFrame
{
public MyJFrame()
{
add(new JTree()
{
{
addPropertyChangeListener("preferredSize",
new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
System.out.println("preferred size changed");
}
});
new Thread()
{
public void run()
{
while(true)
{
System.out.println("preferred size = "+getPreferredSize());
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
}
}.start();
}
});
pack();
setVisible(true);
}
public static void main(String[] args)
{
new MyJFrame();
}
}

How to make an action only when my Frame disappear on the screen in Java?

I need to take a screenshot of an area of my screen, to make that i use "Robot" in Java. But if the user place the windows in the area, the windows will be on the screenshot instead of the background.
I tried to solve my problem by placing a :
myframe.setVisible(false);
But when i look the screenshot the windows appear on it. I thought it was because the windows didn't have enough time to disappear or because the render of the screen wasn't updated yet, so i tried different things like using:
repaint();
Or by placing a
try{}finally{}
block to be sure that the actions in the try block have been finished.
But no one of these solution works. These is other ways in my mind but they looks globally bad because they use functions to wait.
So is there a good solution to my problem?
You could use window listener to fire the screen shot when window is closing:
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainFrame extends JFrame implements WindowListener {
public MainFrame() {
super("Test Frame");
JLabel displayMsg = new JLabel(" Close window");
getContentPane().add(displayMsg);
addWindowListener(this);
setSize(400, 300);
setVisible(true);
}
#Override
public void windowClosing(WindowEvent e) {
System.out.println("WindowListener method called: windowClosing.");
//add you screen capture code here
}
//--Not used
#Override
public void windowClosed(WindowEvent e) {
//do nothing
}
#Override
public void windowOpened(WindowEvent e) {
//do nothing
}
#Override
public void windowIconified(WindowEvent e) {
//do nothing
}
#Override
public void windowDeiconified(WindowEvent e) {
//do nothing
}
#Override
public void windowActivated(WindowEvent e) {
//do nothing
}
#Override
public void windowDeactivated(WindowEvent e) {
//do nothing
}
public void windowGainedFocus(WindowEvent e) {
//do nothing
}
public void windowLostFocus(WindowEvent e) {
//do nothing
}
public static void main(String[] args) {
new MainFrame();
}
}
Use SwingUtilities.invokeLater() to take the actual screenshot. Not 100% sure, but I think that myframe.setVisible(false); doesn't become effective till the program flow goes back to the event dispatching loop.
EDIT:
instead of
useRobotToMakeScreenshot();
write
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
useRobotToMakeScreenshot();
}
}
(of course, you have to replace useRobotToMakeScreenshot() with the actual call to the method that does the screenshooting thing)

How to show a splash-screen, load datas in the background, and hide the splash-screen after that?

I'm designing a simple JavaFX form.
First, I load the JavaFX environment (and wait for it to finish), with something like this :
final CountDownLatch latch_l = new CountDownLatch(1);
try {
// init the JavaFX environment
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFXPanel(); // init JavaFX
latch_l.countDown();
}
});
latch_l.await();
}
This works fine. (the reason why I need to first load the JavaFX this way, is because it's mainly a Swing application, with some JavaFX components inside, but they are loaded later)
Now, I'd like to add a splash-screen on launch, and displays it while the JavaFX environment loads (and in fact put in on-screen for like 5 seconds, because there are logo, trademark etc.. of the application I need to show)
So I came up with a SplashScreen class, which just displays a JWindow on-screen, like that :
public class SplashScreen {
protected JWindow splashScreen_m = new JWindow();
protected Integer splashScreenDuration_m = 5000;
public void show() {
// fill the splash-screen with informations
...
// display the splash-screen
splashScreen_m.validate();
splashScreen_m.pack();
splashScreen_m.setLocationRelativeTo(null);
splashScreen_m.setVisible(true);
}
public void unload() {
// unload the splash-screen
splashScreen_m.setVisible(false);
splashScreen_m.dispose();
}
}
Now, I want for the splash-screen to load and display itself 5 seconds.
Meanwhile, I want the JavaFX environment to load, too.
So I updated the CountDownLatch like this :
final CountDownLatch latch_l = new CountDownLatch(2); // now countdown is set to 2
final SplashScreen splash_l = new SplashScreen();
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// show splash-screen
splash_l.show();
latch_l.countDown();
// init the JavaFX environment
new JFXPanel(); // init JavaFX
latch_l.countDown();
}
});
latch_l.await();
splash_l.unload();
}
So, it's working, but the splash only stays for the JavaFX environment to load, so basically it unloads very quickly (which is normal, given the code I wrote).
How to display the splash-screen for 5 seconds minimum (if the JavaFX loads faster) without freezing the EDT ?
Thanks.
The most significant issue is you're blocking the Event Dispatching Thread, meaning that it can't display/update anything while it's blocked. The same problem applies to JavaFX.
You should, also, never update either from anything other then they respective event queues.
Now, there are any number of ways you might be able to go about this, but SwingWorker is probably the simplest for the time been.
I apologise, this is the entire exposure to JavaFX I've had...
public class TestJavaFXLoader extends JApplet {
public static void main(String[] args) {
new TestJavaFXLoader();
}
public TestJavaFXLoader() throws HeadlessException {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Loader loader = new Loader();
loader.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("state") && evt.getNewValue().equals(SwingWorker.StateValue.DONE)) {
System.out.println("Load main app here :D");
}
}
});
loader.load();
}
});
}
public class Loader extends SwingWorker<Object, String> {
private JWindow splash;
private JLabel subMessage;
public Loader() {
}
protected void loadSplashScreen() {
try {
splash = new JWindow();
JLabel content = new JLabel(new ImageIcon(ImageIO.read(...))));
content.setLayout(new GridBagLayout());
splash.setContentPane(content);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
subMessage = createLabel("");
splash.add(createLabel("Loading, please wait"), gbc);
splash.add(subMessage, gbc);
splash.pack();
splash.setLocationRelativeTo(null);
splash.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
protected JLabel createLabel(String msg) {
JLabel message = new JLabel("Loading, please wait");
message.setForeground(Color.CYAN);
Font font = message.getFont();
message.setFont(font.deriveFont(Font.BOLD, 24));
return message;
}
public void load() {
if (!EventQueue.isDispatchThread()) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
#Override
public void run() {
loadSplashScreen();
}
});
} catch (Exception exp) {
exp.printStackTrace();
}
} else {
loadSplashScreen();
}
execute();
}
#Override
protected void done() {
splash.dispose();
}
#Override
protected void process(List<String> chunks) {
subMessage.setText(chunks.get(chunks.size() - 1));
}
#Override
protected Object doInBackground() throws Exception {
publish("Preparing to load application");
try {
Thread.sleep(2500);
} catch (InterruptedException interruptedException) {
}
publish("Loading JavaFX...");
runAndWait(new Runnable() {
#Override
public void run() {
new JFXPanel();
}
});
try {
Thread.sleep(2500);
} catch (InterruptedException interruptedException) {
}
return null;
}
public void runAndWait(final Runnable run)
throws InterruptedException, ExecutionException {
if (Platform.isFxApplicationThread()) {
try {
run.run();
} catch (Exception e) {
throw new ExecutionException(e);
}
} else {
final Lock lock = new ReentrantLock();
final Condition condition = lock.newCondition();
lock.lock();
try {
Platform.runLater(new Runnable() {
#Override
public void run() {
lock.lock();
try {
run.run();
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
condition.signal();
} finally {
lock.unlock();
}
}
}
});
condition.await();
// if (throwableWrapper.t != null) {
// throw new ExecutionException(throwableWrapper.t);
// }
} finally {
lock.unlock();
}
}
}
}
}
I found the runAndWait code here

Why setExtendedState(JFrame.ICONIFIED)does not work while windows screen locked?

all.
i want to minimize my jframe with setExtendedState(JFrame.ICONIFIED), in most case, it works properly, but when it does not work when i lock my os(windows XP) screen with WIN+L.My wimple code are as follows:
import javax.swing.JDialog;
import javax.swing.JFrame;
public class FrameTest extends JFrame {
public static FrameTest ft = new FrameTest();
public static void main(String[] args)
{
FrameTest.ft.setVisible(true);
FrameTest.ft.setLocation(300, 300);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JDialog dlg = new JDialog( ft, "xxx", true );
ft.setExtendedState(JFrame.ICONIFIED);
dlg.setVisible(true);//if not have this line, it works also in screen lock case
}
}
Any help will be appreciated.
It could me that you are manipulating Swing components from the main thread instead of the Event Dispatch Thread. Try wrapping the contents of main in:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Rennable() {
#Override
void run() {
FrameTest.ft.setVisible(true);
FrameTest.ft.setLocation(300, 300);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SwingUtilities.invokeLater(new Rennable() {
#Override
void run() {
JDialog dlg = new JDialog( ft, "xxx", true );
ft.setExtendedState(JFrame.ICONIFIED);
dlg.setVisible(true);case
}
}
If that doesn't help, try splitting the second invokeLater block into:
SwingUtilities.invokeLater(new Rennable() {
#Override
void run() {
ft.setExtendedState(JFrame.ICONIFIED);
}
SwingUtilities.invokeLater(new Rennable() {
#Override
void run() {
JDialog dlg = new JDialog( ft, "xxx", true );
dlg.setVisible(true);case
}
That gives Swing a chance to respond to the iconification before handing off control to the dialog.

Categories