i have a problem. i don't know where i am making the mistake.... i need to refreshed my JPanels automatically with time..... i have used timers for this. i used in the constructor of the class...

public class HoursPanel{
//....
//.....
   public HoursPanel(){
    int delay = 60000;
    Timer timer = new Timer(delay, new ActionListener(){
         public void actionPerformed(ActionEvent e){
             hrPanel.invalidate();
             hrPanel.revalidate();                   
             hrPanel.repaint();
         }
    });
    timer.start();
     }

//....
//.....
//.....

i am posting only my timer portion. I need to refresh my JPanel which contain values from database. i need to refresh at a regular interval of 1 min automatically, but i got my JPanel refreshed only when i minimize the JFrame or if i switch to different JPanel(which are in cardlayout) and coming back to that JPanel...

in above hrPanel is an object of JPanel, where i put all my JLabel and graphs.... please give me suggestion that where i am doing wrong. Thank you in advance.....

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Where's the code that updates the data from the database?
Your structure should be:
timer actionPerformed updates data objects and calls repaint() for the GUI
paintComponent(...) does the minimum needed to keep the GUI in step with the data objects.

Thank you for your reply. Actually i have 3 classes...

(1) a main class....

(2) a database class where i did all the queries and connections and returned the queries....

(3) another class where i used the returned queries and did all the calculation and added to respective JLabels in a JPanel and returned the JPanel (with those database values in JLabels) to the main class and added to the main JPanel, which is in cardlayout....


Now what should i do...? what should be used in actionPerformed method of timer? i am getting the desired answer, but only when i minimize the JFrame or when i switch to different JPanel and again coming back to that JPanel..... do i need to follow different approach?

Can't say without the whole picture. But like I said before, update the queries and calculations in a method (whatever class it may be in) from the Timer, and update the contents of the JPanel (by querying the previously-retrieved data & calcs) in the paintComponent. You should only need to set up the JPanel once.

Yaaa!!! I have done it. I just used the above code which i have posted but in a method not in the constructor... Thank you...

This question has already been answered. Start a new discussion instead.