Showing splash screen while loading...
Hi,
I'd like to show a splash screen while the application is loading (setting up hibernate session etc.) - here is my main method which is loading hibernate and starting up the application:
publicstaticvoid main(String[] args){
java.awt.EventQueue.invokeLater(new Runnable(){
publicvoid run(){
MdbCheckerApp app =new MdbCheckerApp();
app.initializeLookAndFeel();
// preload Hibernate session
HibernateUtil.getSession();
// login successful
// if (app.login()) {
new MdbChecker().setVisible(true);
// }
}
});
HibernateUtil.closeSession();
}
How do I show my splash screen while simultaneously loading the hibernate session?
Thanks in advance
- Stephan
[1357 byte] By [
Jacklera] at [2007-11-27 10:40:43]

# 3
However, I'm not quite sure on how to proceed...
public static void main(String args[]) {
final TestForm tf = new TestForm();
tf.showSplash();
tf.runInitializer(new Runnable() {
public void run() {
try {
Thread.currentThread().sleep(3000);
} catch (Exception e) {}
tf.hideSplash();
}
});
}
public void runInitializer(Runnable r) {
Thread t = new Thread(r);
t.start();
}
public SplashScreen showSplash() {
// if a splash window was already created...
if(splash != null) {
// if it's showing, leave it; else null it
if(splash.isShowing()) {
return splash;
} else {
splash = null;
}
}
splash = new SplashScreen(null, true);
centerComponent(splash);
splash.show();
return splash;
}
public void hideSplash() {
if (splash != null) {
splash.dispose();
splash = null;
}
}
I'm wondering why I do only see the splash screen (I was hoping that the splash screen would be disappearing after 3 seconds).
Here is the code of my splash screen:
public class SplashScreen extends javax.swing.JDialog {
/** Creates new form SplashScreen */
public SplashScreen(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setLocationRelativeTo(null);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jProgressBar1 = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle(java.util.ResourceBundle.getBundle("com/gigatronik/mdbchecker/ui/bundle/MdbCheckerMessages").getString("splashscreen.title"));
setModal(true);
setUndecorated(true);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/gigatronik/mdbchecker/ui/resources/gigatronik_4c_300.jpg")));
jProgressBar1.setIndeterminate(true);
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, jProgressBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SplashScreen(new javax.swing.JFrame(), true).setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration
}
Any help would be greatly appreciated.
- Stephan
# 4
The only way I got it to work was calling the splash screen before calling invokeLater(). Otherwise the JWindow gets gray.
public static void main(String[] args)
{
// Splash screen s funciona se for exibida antes de invokeLater()
SplashScreen.getInstance().start();
SGPersistence.setPersistenceUnit("AutorizadorOnline");
SplashScreen.getInstance().finish();
java.awt.EventQueue.invokeLater(new java.lang.Runnable()
{
public void run()
{
SGUtil.setLookAndFeel(SGUtil.getCompactMetalLookAndFeelClassName());
// Exibe o formulrio de login
new LoginForm().setVisible(true);
}
});
}
}
In my case, SGPersistence.setPersistenceUnit() starts Hibernate
/*
* SplashScreen.java
*
* Created on 19 de Julho de 2007, 08:32
*/
package br.com.spdata.javase.splashscreen;
/**
*
* @author fkj
*/
public class SplashScreen extends javax.swing.JWindow
{
private static SplashScreeninstance;
private SplashScreen()
{
}
public synchronized static SplashScreen getInstance()
{
if(instance == null)
{
instance = new SplashScreen();
}
return instance;
}
public void start()
{
setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
initComponents();
setVisible(true);
setLocationRelativeTo(null);
}
public void finish()
{
setVisible(false);
dispose();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents()
{
progressBar = new javax.swing.JProgressBar();
image = new javax.swing.JLabel();
progressBar.setIndeterminate(true);
getContentPane().add(progressBar, java.awt.BorderLayout.PAGE_END);
image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/spdata/images/splash.png"))); // NOI18N
getContentPane().add(image, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel image;
private javax.swing.JProgressBar progressBar;
// End of variables declaration
}
Message was edited by:
fkjaekel
Message was edited by:
fkjaekel