0

I'm creating a Loading Page for our group project, but every time I call the loading page class, it is invisible.

Here is my Main class:

public static void main(String[] args) {
    
    new caller();
    
}

Here is the button that when you click you will go into the loading page class.

JFrame frame = new JFrame();
JButton btn = new JButton("Press me!");

caller(){
    
    btn.setBounds(20, 30, 120, 50);
    btn.setFocusable(false);
    btn.addActionListener(this);
    
    
    
    frame.setSize(420, 420);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(null);
    frame.add(btn);
    
}
@Override
public void actionPerformed(ActionEvent e) {
    
    if(e.getSource()==btn) {
        
        frame.dispose();
        new loading_page();
        
        
    }
}

And here is the loading page class

 JLabel lablab = new JLabel();
 JProgressBar bar = new JProgressBar();
 JFrame frame = new JFrame();

loading_page(){
    
    lablab.setBackground(Color.WHITE);
    lablab.setHorizontalAlignment(SwingConstants.CENTER);
    lablab.setIcon(new ImageIcon("C:\\Users\\KHAdmin\\Documents\\Pictures\\giphy.gif"));
    lablab.setBounds(65, 40, 367, 215);
    
    bar.setForeground(Color.cyan);
    bar.setBounds(10, 300, 480, 17);
    bar.setValue(0);
    bar.setStringPainted(true);
    bar.setFont(new Font("MV Boli", Font.BOLD, 15));
    bar.setForeground(Color.CYAN);
    bar.setBackground(Color.BLACK);
    
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(400, 200, 500, 335);
    frame.setVisible(true);
    frame.setLayout(null);
    
    frame.getContentPane().setBackground(Color.BLACK);
    frame.getContentPane().add(lablab);
    frame.getContentPane().add(bar);
    
    
    int counter =0;
    
    while(counter<=100) {
        
        bar.setValue(counter);
        try {
            Thread.sleep(50);
            if(counter==100) {
                frame.dispose();
            }
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        }
        counter +=1;
    
}
    {
        new caller();
    }
}

I think it's in the while(counter<==100) statement. I used the frame dispose to dispose the frame from the previous class, but when the system call the 3rd class which is the loading page class, it's invisible.

  • 1
    Unrelated to your issue: [Java, setting layout to null](https://stackoverflow.com/questions/33249871/java-setting-layout-to-null) and [Java naming convention](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html) – jhamon Mar 30 '22 at 09:36
  • Does this answer your question? [What's the point of setDefaultCloseOperation(WindowConstants.EXIT\_ON\_CLOSE);?](https://stackoverflow.com/questions/32077449/whats-the-point-of-setdefaultcloseoperationwindowconstants-exit-on-close) – jhamon Mar 30 '22 at 09:38
  • 1
    Swing is lazy when it comes to laying its components. If you make the window visible and then add new components, then you must ensure that you're telling the parent container that it needs to `revalidate` and `repaint` it's contents - but most of your issues are most likely associated with the `null` layout. Consider providing a [mcve] – MadProgrammer Mar 30 '22 at 10:11

0 Answers0