4

I created FlashScreen.java as loading screen consist of JProgressBar. I want that after progressbar percentage is completed current window should be closed and new window should be open. I made it but after closing first window in next window there is no component in window. Empty window is opening.

Here is code: FlashScreen.java

    package crimeManagement;

import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;  
public class FlashScreen extends JFrame{  
JProgressBar jb;
JLabel lblStat;
int i=0,num=0;  

FlashScreen(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setBounds(new Rectangle(400, 200, 0, 0));
    jb=new JProgressBar(0,2000);
    jb.setBounds(100,219,579,22);  

    jb.setValue(0);  
    jb.setStringPainted(true);  

    getContentPane().add(jb);  
    setSize(804,405);  

    getContentPane().setLayout(null);  

    lblStat = new JLabel("");
    lblStat.setForeground(Color.CYAN);
    lblStat.setHorizontalAlignment(SwingConstants.CENTER);
    lblStat.setHorizontalTextPosition(SwingConstants.CENTER);
    lblStat.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
    lblStat.setBounds(229, 252, 329, 14);
    getContentPane().add(lblStat);

    JLabel lblBackGround = new JLabel("");
    lblBackGround.setHorizontalTextPosition(SwingConstants.CENTER);
    lblBackGround.setHorizontalAlignment(SwingConstants.CENTER);
    lblBackGround.setIcon(new ImageIcon(FlashScreen.class.getResource("/Images/FlashImage.jpg")));
    lblBackGround.setBounds(0, 0, 798, 376);
    getContentPane().add(lblBackGround);


    }  

public void iterate(){  
    while(i<=2000){  
        jb.setValue(i);  
        i=i+20;  
        try{
            Thread.sleep(50);           
            if(i==20)
            {
                lblStat.setText("Loading...");

            }
            if(i==500)
            {
                lblStat.setText("Please Wait...");
            }
            if(i==1000)
            {
                Thread.sleep(100);
                lblStat.setText("Loading Police Station Management System...");
            }
            if(i==1200)
            {
                lblStat.setText("Please Wait...");
            }
            if(i==1600)
            {
                lblStat.setText("Almost Done...");
            }
            if(i==1980)
            {
                lblStat.setText("Done");
            }
            if(i==2000)
            {
                this.dispose();
                LoginPage lp=new LoginPage();
                lp.setVisible(true);
            }

        }
        catch(Exception e){}

    }
} 

public static void main(String[] args) {  
    FlashScreen fs=new FlashScreen();  
    fs.setVisible(true);  
    fs.iterate();  
}  
}  



**LoginPage.java**

package crimeManagement;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.*;

public class LoginPage extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JTextField txtUserName;
    private JPasswordField txtPass;

    public static void main(String[] args) {

                    LoginPage window = new LoginPage();
                    window.frame.setVisible(true);

    }





    public LoginPage() {
        frame = new JFrame();
        frame.setResizable(false);
        frame.getContentPane().setBackground(SystemColor.inactiveCaption);
        frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 16));
        frame.setBounds(100, 100, 554, 410);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblLoginType = new JLabel("Login Type");
        lblLoginType.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblLoginType.setBounds(97, 53, 99, 20);
        frame.getContentPane().add(lblLoginType);

        JLabel lblUsename = new JLabel("User Name");
        lblUsename.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblUsename.setBounds(97, 177, 99, 26);
        frame.getContentPane().add(lblUsename);

        JLabel lblPaaword = new JLabel("Password");
        lblPaaword.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblPaaword.setBounds(97, 223, 99, 26);
        frame.getContentPane().add(lblPaaword);

        JPanel panel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        panel.setBackground(SystemColor.inactiveCaptionBorder);
        panel.setBounds(210, 47, 143, 93);
        frame.getContentPane().add(panel);
        TitledBorder tb=new TitledBorder( "Login");
        tb.setTitleJustification(TitledBorder.CENTER);
        tb.setTitlePosition(TitledBorder.CENTER);

        panel.setBorder(BorderFactory.createTitledBorder(tb));


        JRadioButton rdbAdmin = new JRadioButton("Admin");
        rdbAdmin.setBackground(SystemColor.inactiveCaption);
        rdbAdmin.setFont(new Font("Tahoma", Font.PLAIN, 13));
        rdbAdmin.setSelected(true);
        panel.add(rdbAdmin);

        JRadioButton rdbOthers = new JRadioButton("Others");
        rdbOthers.setBackground(SystemColor.inactiveCaption);
        rdbOthers.setFont(new Font("Tahoma", Font.PLAIN, 13));
        panel.add(rdbOthers);

        txtUserName = new JTextField();
        txtUserName.setBackground(UIManager.getColor("TextField.background"));
        txtUserName.setBounds(210, 177, 158, 26);
        frame.getContentPane().add(txtUserName);
        txtUserName.setColumns(30);

        JButton btnLogin = new JButton("Login");
        btnLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnLogin.setBounds(210, 286, 71, 23);
        frame.getContentPane().add(btnLogin);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        btnExit.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnExit.setBounds(297, 286, 71, 23);
        frame.getContentPane().add(btnExit);

        txtPass = new JPasswordField();
        txtPass.setBounds(210, 226, 158, 26);
        frame.getContentPane().add(txtPass);
    }

}

1 Answers1

4

You're displaying the wrong JFrame. Yes the LoginPage extends JFrame, and yes you display it, but you add no components to it, and instead add all components to a private JFrame field of the class named, frame.

A quick solution is to change your LoginPage class so that it doesn't extend JFrame and then give this class a public getFrame() method:

public JFrame getFrame() {
    return frame;
}

and when wanting to show it, call

this.dispose();
LoginPage lp = new LoginPage();
// lp.setVisible(true);
lp.getFrame().setVisible(true);

but having said this, there are still some serious threading issues with your code that you'll eventually want to fix, including trying to avoid calling Thread.sleep() in code that risks being called on the Swing event thread.

Also please check out The Use of Multiple JFrames: Good or Bad Practice? to see why it is often a bad practice to display a bunch of JFrames in your app, and ways around this.

Other issues include use of null layouts. Yes they may seem like an easy way to create complex GUI's quickly -- until you try to show the GUI on another platform and find that they don't look so nice, or have to enhance, debug or change it, and find it very tricky and easy to mess up. Much better to use layout managers.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class FlashScreenTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame mainFrame = new JFrame("Main App");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.add(new MainAppPanel());
            mainFrame.pack();
            mainFrame.setLocationRelativeTo(null);

            FlashScreenPanel dialogPanel = new FlashScreenPanel();
            JDialog dialog = new JDialog(mainFrame, "Flash Screen", ModalityType.APPLICATION_MODAL);
            dialog.add(dialogPanel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);

            dialogPanel.startProgress();
            dialog.setVisible(true);

            mainFrame.setVisible(true);
        });
    }
}

class FlashScreenPanel extends JPanel {
    public static final String LOADING = "Loading...";
    public static final String PLEASE_WAIT = "Please Wait...";
    public static final String LOADING_POLICE_STATION = "Loading Police Station...";
    public static final String ALMOST_DONE = "Almost Done...";
    public static final String DONE = "Done";
    private static final int TIMER_DELAY = 50;

    private JProgressBar jb = new JProgressBar(0, 2000);
    private JLabel statusLabel = new JLabel("", SwingConstants.CENTER);

    public FlashScreenPanel() {
        setPreferredSize(new Dimension(800, 400));

        statusLabel.setForeground(Color.CYAN);
        statusLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
        jb.setStringPainted(true);
        JPanel bottomPanel = new JPanel(new BorderLayout(20, 20));
        bottomPanel.add(jb, BorderLayout.PAGE_START);
        bottomPanel.add(statusLabel, BorderLayout.CENTER);

        int eb = 40;
        setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
        setLayout(new GridLayout(0, 1));
        add(new JLabel()); // dummy component to move prog bar lower
        add(bottomPanel);
    }

    public void startProgress() {
        statusLabel.setText(LOADING);
        new Timer(TIMER_DELAY, new ActionListener() {
            private int i = 0;
            @Override
            public void actionPerformed(ActionEvent e) {
                i += 20;
                jb.setValue(i);
                if (i == 500) {
                    statusLabel.setText(PLEASE_WAIT);
                } else
                if (i == 1000) {
                    statusLabel.setText(LOADING_POLICE_STATION);
                } else
                if (i == 1200) {
                    statusLabel.setText(PLEASE_WAIT);
                } else
                if (i == 1600) {
                    statusLabel.setText(ALMOST_DONE);
                } else
                if (i == 1980) {
                    statusLabel.setText(DONE);
                } else
                if (i == 2000) {
                    ((Timer) e.getSource()).stop();
                    Window win = SwingUtilities.getWindowAncestor(FlashScreenPanel.this);
                    win.dispose();
                }
            }
        }).start();
    }
}

class MainAppPanel extends JPanel {
    public MainAppPanel() {
        setPreferredSize(new Dimension(600, 400));
    }
}
Community
  • 1
  • 1