user3773028
user3773028

Reputation:

Issue with SplashScreen java

I need help with my code.

I'm having problem to start my SplashScreen of my Java application. The principal class (bolaoJFrame) that extends JFrame and calls a screen logon that extends JDialog. After the validation of user, the SplashScreen is created but don't appears. What am I doing wrong?

The SplashScreen should appear after the validation to show a JProgressBar while the loading of class principal does not finish.

This is the metodo main of pricipal class:

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(bolaoJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {

            bolaoJFrame principal= new bolaoJFrame();
            principal.setLocationRelativeTo(null);
            login = new loginJDialog(principal, true);
            if(newUser==true){
                CardLayout cards = (CardLayout)(principal.jpPrincipal.getLayout());
                cards.show(principal.jpPrincipal, "cadastro");
            }else{
                if(Usuario.userCriado==0){
                    System.exit(0);
                }else{

                    Splash telaSplash =new Splash(null);
                    telaSplash.setVisible(true);

                    CardLayout cards = (CardLayout)(principal.jpPrincipal.getLayout());
                    cards.show(principal.jpPrincipal, "apostas");
                    principal.jLabel1.setText("Bem vindo " + user.getUsername());
                    principal.jLabel4.setIcon(new Util().dimencionaImagem(user.getFoto(),80));

                    HoraDoSistema.start(principal.retomaHora(),principal.lbRelagio);
                    principal.montaCards();
                    PanelRanking pr = new PanelRanking(10, 1, user.getUserCod());

                    principal.jpRanking.add(pr,"1");
                    if(pr.getPosUser()>0){
                       principal.lbRankingInfo.setText("Sua posição atual é " + pr.getPosUser() + "º" ); 
                    }
                    if(pr.getPontuacaoUser()>0){
                       principal.lbPontuacaoInfo.setText("Você fez " + pr.getPontuacaoUser()+ " pontos" ); 
                    }                        
                    principal.lbNext.requestFocus();


                    principal.verificaDisponibilidad();
                }
            }


          principal.setVisible(true); 




        }
    });
}

SplashScreen Class:

public Splash(JFrame owner) {
    //super(owner);
    setLayout(null);
    Util.centralizarJanela(this, 521, 335);
    inicialize();
}



public void inicialize(){
    JLabel fundo = new JLabel();
    //fundo.setLocation(new Point(0,0));
    fundo.setIcon(new ImageIcon(getClass().getResource("/Principal/splash.png")));
    fundo.setSize(521, 315);
    add(fundo);

    barraDeProgresso= new JProgressBar();
    barraDeProgresso.setBackground(Color.red);
    barraDeProgresso.setBounds(0, 315, 521, 20);
    barraDeProgresso.setStringPainted(true);
    add(barraDeProgresso);
}

public void setValorBarraDeProgresso(int progresso){
    barraDeProgresso.setValue(progresso);
}

public int getValorBarraDeProgresso(){
    return barraDeProgresso.getValue();
}

Upvotes: 1

Views: 348

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347244

My first guess is setLayout(null);, my second guess is you're not actually applying a size or location to the Splash window

Normally, something like pack() and setLocationRelativeTo(bolaoJFrame) would be what you want, but because you decided to discard the layout manager, pack will no longer help you...

Updated...

So the basic workflow is something like...

bolaoJFrame principal= new bolaoJFrame();
principal.setLocationRelativeTo(null);

//...
Splash telaSplash =new Splash(null);
telaSplash.setVisible(true);
//..

principal.setVisible(true); 

The problem with this, is the windows are likely to appear (almost) simultaneously as there is no delay between the setVisible commands.

You could use a javax.swing.Timer to insert a short delay between making the splash screen visible and the principal

While not possible to determine from your code snippet, it's also possible you are blocking the Event Dispatching Thread, prevent any new events from processing and preventing the windows from been shown

Upvotes: 1

Related Questions