JFrame filling / refreshing problem

From: Lancelot dM via JavaKB.com (forum_at_JavaKB.com)
Date: 03/28/05


Date: Mon, 28 Mar 2005 16:54:02 GMT

Hi i'm currently working on an application's GUI.
At the beginning i'm asking the user to enter a code.
If it's right i'm loading some sort of encrypted database.
This operation takes a few seconds and I would have liked to show a splash
screen while waiting. It would disappear at the end of the operation and
then the application interface shows up.
The problem is that my "splash" frame doesn't have any component displayed,
not even a label. I have noticed that in fact this screen fills in after
the main application has been displayed (at that time the splash screen is
hidden, to see that I had commented the "loadingFrame.setVisible(false)
line")

Here is the code (it's in english but the String displayed are in french) :

public class PsychologGUI extends JFrame{
    
    private final String filepaths="data/filepaths.xml";
    private final int applicationWidth=800;
    private final int applicationHeight=600;
    private final int buttonWidth=200;
    private final int buttonHeight=40;
    private int screenHeight;
    private int screenWidth;
    private Dimension displayPanelDimension;
    private String iconspath;
    
    
    //storage objects
    private int key;
    private Hashtable files;
    private UsersStorage usersStorage;
    private Storage storage;
    
    //GUI objects
    private javax.swing.JPanel buttonMenuPanel;
    private javax.swing.JButton createButton;
    private javax.swing.JPanel currentPanel;
    private javax.swing.JPanel displayPanel;
    private javax.swing.JButton helpButton;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPanel menuPanel;
    private javax.swing.JButton quitButton;
    private javax.swing.JButton loadButton;
    private javax.swing.JButton viewButton;
    
    /** Creates a new instance of PsychologGI */
    public PsychologGUI() {
        //Sets the Windows' LookAndFeel
        String lnf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        try {
            UIManager.setLookAndFeel(lnf);
            SwingUtilities.updateComponentTreeUI(this);
        } catch (UnsupportedLookAndFeelException ex1) {
            System.err.println("LookAndFeel non supporte : " + lnf);
        } catch (ClassNotFoundException ex2) {
            System.err.println("Classe de LookAndFeel non trouvee : " + lnf)
;
        } catch (InstantiationException ex3) {
            System.err.println("LookAndFeel ne peut etre charge : " + lnf);
        } catch (IllegalAccessException ex4) {
            System.err.println("LookAndFeel ne peut etre utilise : " + lnf);
        }
        
        //retrieves the dimension of the screen
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        this.screenHeight=d.height;
        this.screenWidth=d.width;
        this.setLocation((this.screenWidth-this.applicationWidth)/2,
(this.screenHeight-this.applicationHeight)/2);
        this.setResizable(false);
        
        
        //loads the filepaths Hashtable
        try {
            files=new Hashtable();
            new XMLReader(files,this.filepaths);
        } catch (SAXException e){
            System.err.println("Error : invalid filepaths file");
            e.printStackTrace();
        } catch (javax.xml.parsers.ParserConfigurationException e){
            System.err.println("Error : error parsing filepaths file");
            e.printStackTrace();
        } catch (IOException e){
             JOptionPane.showMessageDialog(this,
                        "Fichier manquant, veuillez r?installer le
logiciel",
                        "Erreur",
                        JOptionPane.ERROR_MESSAGE);
            System.err.println("Error : bad structure in filepaths file");
            e.printStackTrace();
        }
        this.iconspath=(String)this.files.get("iconspath");
        setIconImage(new ImageIcon(this.iconspath+"psycone32e.png")
.getImage());
        
        //asks decoding key
        JPanel keyPanel=new JPanel();
        keyPanel.add(new JLabel("Entrez la cl?"));
        JPasswordField keyField =new JPasswordField();
        keyField.setColumns(10);
        keyPanel.add(keyField);
        Object[] options=new Object[2];
        options[1]="Annuler";
        options[0]="Valider";
        int n;
        boolean isNumber;
        boolean validKey=false;
        do {
            isNumber=false;
            do {
                n=JOptionPane.showOptionDialog(this,keyPanel,
                        "Idenfication",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.PLAIN_MESSAGE,
                        new ImageIcon(this.iconspath+"password.png"),
                        options,
                        options[0]);
                if (n==JOptionPane.OK_OPTION){
                    try{
                        this.key=Integer.parseInt(new String
(keyField.getPassword()));
                        isNumber=true;
                    }catch (NumberFormatException e){
                        JOptionPane.showMessageDialog(this,
                                "Cl? incorrecte",
                                "Erreur",
                                JOptionPane.ERROR_MESSAGE);
                    }
                } else{
                    System.exit(0);
                }
            } while (!isNumber);
            
            
            //loads the usersdatabase
            this.usersStorage=new UsersStorage();
            try{
                new XMLReader(usersStorage,((String)this.files.get
("usersdb")),this.key);
                validKey=true;
            } catch (SAXException e){
                JOptionPane.showMessageDialog(this,
                        "Cl? incorrecte",
                        "Erreur",
                        JOptionPane.ERROR_MESSAGE);
                System.err.println("Error : invalid users database file or
invalid key");
                e.printStackTrace();
            } catch (javax.xml.parsers.ParserConfigurationException e){
                System.err.println("Error : error parsing users database
file");
                e.printStackTrace();
            } catch (IOException e){
                JOptionPane.showMessageDialog(this,
                        "Erreur de lecture du fichier de base de donn?es\
nVeuillez contacter le service apr?s-vente ou r?installer le logiciel",
                        "Erreur",
                        JOptionPane.ERROR_MESSAGE);
                System.err.println("Error : bad structure in users database
file");
                e.printStackTrace();
            }
        } while (!validKey);
        
        //Shows a loading window while loading the database
        // Probleme : le panneau ne se remplit qu apres avoir charg? la
base de donn?es
      /* JFrame loadingFrame=new JFrame();
        
        //loadingFrame.setIconImage(new ImageIcon
(this.iconspath+"psycone.png").getImage());
        
         JPanel loadingPanel=new JPanel();
        loadingPanel.setLayout(new BoxLayout(loadingPanel,BoxLayout.Y_AXIS))
;
        Dimension loadingDimension=new Dimension(400,300);
        loadingPanel.setPreferredSize(loadingDimension);
        loadingPanel.setMaximumSize(loadingDimension);
        loadingPanel.setMinimumSize(loadingDimension);
        JLabel image=new JLabel();
        image.setIcon(new ImageIcon(this.iconspath+"psycolog.png"));
        loadingPanel.add(image);
        JLabel bePatient=new JLabel("Veuillez patientez quelques
instants...");
        loadingPanel.add(bePatient);
        loadingFrame.getContentPane().add(loadingPanel);
        loadingFrame.setResizable(false);
        loadingFrame.setUndecorated(true);
        loadingFrame.setLocation((this.screenWidth-400)/2,
(this.screenHeight-300)/2);
        loadingFrame.setDefaultCloseOperation
(WindowConstants.DO_NOTHING_ON_CLOSE);
         
        //loadingFrame.getContentPane().setLayout(new FlowLayout());
        //loadingFrame.getContentPane().add(new JLabel("toto")
,java.awt.BorderLayout.CENTER);
        loadingFrame.setUndecorated(true);
        loadingFrame.pack();
        loadingFrame.setVisible(true);
        loadingFrame.setVisible(true);
        loadingFrame.setVisible(true);*/
                        
                
        
        //loads the tests database (it can take a while)
        this.storage=new Storage();
        try{
            new XMLReader(this.storage,((String)this.files.get("db")),key);
        } catch (SAXException e){
            System.err.println("Error :invalid tests storage file");
            e.printStackTrace();
        } catch (javax.xml.parsers.ParserConfigurationException e){
            System.err.println("Error : error parsing tests storage file");
            e.printStackTrace();
        } catch (IOException e){
            System.err.println("Error : bad structure in tests storage
file");
            e.printStackTrace();
        }
        
        //loadingFrame.setVisible(false);
     
        initComponents();
    }

-- 
Message posted via http://www.javakb.com

Quantcast