0

My application gives a grey screen after the timer is run. As advised, I have now a MainPage which extends JFrame and a MenuPage that extends JPanel. I wish to load MenuPage after MainPage is run. repaint() and revalidate() does not work out for me. Please point me in the right direction.

import java.awt.*;
import java.awt.event.*;
import java.io.File;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class MainPage extends JFrame {

    private static JPanel contentPane;

    //timer
    private final static int interval = 40;
    private int i;
    private Timer t;
    private JProgressBar pbar;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainPage frame = new MainPage();                    
                    frame.setVisible(true);             
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });     
    }

    public MainPage() {             
        dim = Toolkit.getDefaultToolkit().getScreenSize();      
        System.out.println(dim);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        contentPane.setBounds(0,0,dim.width,dim.height);
        setContentPane(contentPane);

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);   

        pbar = new JProgressBar (0,20);
        pbar.setBounds(600, 500, 200, 45);
        pbar.setValue(0);
        pbar.setStringPainted(true);
        pbar.setForeground(Color.RED);
        Border border = BorderFactory.createTitledBorder("Loading...");
        pbar.setBorder(border);         

        t = new Timer (interval, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if (i == 20){
                    t.stop();
                    //start.setEnabled(true);



                    //refresh + load next page              
                    contentPane.removeAll();
                    MenuPage menuPage = new MenuPage();
                    //setContentPane(menuPage);

                    contentPane.add(menuPage);
                    contentPane.revalidate();                   
                    contentPane.repaint();
                    contentPane.setVisible(true);                       

                }
                else{
                    i++;
                    pbar.setValue(i);
                }               
            }           
        });
        t.start();

        contentPane.add(pbar, BorderLayout.NORTH);          
        contentPane.add(lblTitle);
        contentPane.add(imgLogo);
        contentPane.add(imgBackground);     
    }
}



import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MenuPage extends JPanel {

    private JPanel contentPane;



public MenuPage() {
            //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setSize(500, 500);
            contentPane.setLayout(null);                

            add (contentPane);

        JButton btnSadfsafsa = new JButton("sadfsafsa");
        btnSadfsafsa.setBounds(10, 52, 89, 23);
        btnSadfsafsa.setEnabled(true);
        btnSadfsafsa.setVisible(true);
        contentPane.add(btnSadfsafsa);
    }
}
miiike test
  • 103
  • 6
  • Don't use setBounds() and setSize(). Define proper LayoutManager instead. – StanislavL Jan 09 '15 at 08:37
  • @stanslavl: thank you. Im concerned about the display of the pages for now, will get to the layout after that. I still do not see the problem why it cannot load the new page. – miiike test Jan 09 '15 at 08:50

2 Answers2

0

Your MenuPage constructor is the problem.

You create a new JPanel - contentPane but never add it and never set the size. So in fact you just create an empty panel.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Please see edit, it still gives a grey screen. Baffled. – miiike test Jan 09 '15 at 09:10
  • Simplify the exmple. You don't need an additional panel (the contentPane) in the MenuPage. Define layouts to avoid 0 sized components or subcomponents. – StanislavL Jan 09 '15 at 09:18
  • Please correct me but supposedly I place all components such as buttons into a JPanel, then load this panel by calling it from MainPage, shouldnt this be the way i 'refresh' pages? in that sense i will have many jpanels that define different pages later, where MainPage controls them. I do not wish to use a card layout. my previous post - http://stackoverflow.com/questions/27831067/switch-between-jpanels-of-different-classes – miiike test Jan 09 '15 at 09:25
  • all my layouts are currently null by the way, & they r not of primary concern for now – miiike test Jan 09 '15 at 09:26
  • They are part of the problem. You may forget to set bounds or size of any subpanel and the panel is 0 size and invisible so you see gray rect. That's my last comment here. Sorry. – StanislavL Jan 09 '15 at 09:31
  • thank you, i found the problem. its in the order of the statements & also 'contentPane.add(new MenuPage())' does not work. – miiike test Jan 09 '15 at 10:05
0

I hope this helps others in future. Yes null layout is not to be recommended. Applied setContentPane() instead of contentPane.add() in my case.

//refresh + load next page              
contentPane.removeAll();
contentPane.revalidate();                   
contentPane.repaint();
setContentPane(new MenuPage()); 
miiike test
  • 103
  • 6