hi! i found problem in using timers.... do anyone help me in this regard as i have no idea about it...... please.... i am sending a code which i tried but in vain. i know where the problem arises.... but cannot find any solution of solving the problem.....

package com.ws.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class WeatherDelegator implements ActionListener {
	static JFrame frame;
    JPanel mainPanel; //a panel that uses CardLayout
    JMenu forecastMenu;
    JMenuItem fItem1,fItem2,fItem3;
    CurrentPanel c=new CurrentPanel();
    HoursPanel h=new HoursPanel();
    DaysPanel d=new DaysPanel();
 
    CardLayout cl;
    Timer timer;
    
    public void addComponentToPane(Container pane) {
				forecastMenu=new JMenu("Forecast");
				forecastMenu.setMnemonic(KeyEvent.VK_F);

				fItem1=new JMenuItem("Current");
				fItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,ActionEvent.CTRL_MASK));
				fItem2=new JMenuItem("Next 3 Hours");
				fItem2.add(new JSeparator());
				fItem3=new JMenuItem("Next 3 Days");
				fItem3.add(new JSeparator());
				forecastMenu.add(fItem1);
				forecastMenu.add(fItem2);
				forecastMenu.add(fItem3);
           
				
		//create the JMenuBar
				JMenuBar menuBar = new JMenuBar();
	//Add menu items in the Menu Bar         
				menuBar.add(forecastMenu);
						
				fItem1.addActionListener(this);
				fItem2.addActionListener(this);
				fItem3.addActionListener(this);
        
        //Create the panel that contains the "cards".
        mainPanel = new JPanel(new CardLayout());
        mainPanel.setBackground(Color.BLACK);
        
        int delay = 60000; // 1000 ms is 1sec
			timer = new Timer(delay, new ActionListener(){
			     public void actionPerformed(ActionEvent e){
			    	 c.currentPanel().invalidate();
			    	 c.currentPanel().revalidate();			    	 
			    	 c.currentPanel().repaint();
			    	 mainPanel.add(c.currentPanel(), "p1");
	 			     //cl.show(mainPanel,"p1");
	 			      
	 			   h.hoursPanel().invalidate();
	 			   h.hoursPanel().revalidate();			    	 
	 			   h.hoursPanel().repaint();
	 			   mainPanel.add(h.hoursPanel(), "p2");
     			              //cl.show(mainPanel,"p2");
     			        
     			       d.daysPanel().invalidate();
     			       d.daysPanel().revalidate();			    	 
     			       d.daysPanel().repaint();
    			    	mainPanel.add(d.daysPanel(), "p3");
    			       //cl.show(mainPanel,"p3");
    			  
			     	}
			     });
				timer.start();  
        pane.add(mainPanel);
    }
    
    public void actionPerformed(ActionEvent e){
        cl = (CardLayout)(mainPanel.getLayout());
        Object obj=e.getSource();
        	if(obj==fItem1){
        		mainPanel.revalidate();
        		mainPanel.add(c.currentPanel(), "p1");
	        	cl.show(mainPanel,"p1");
	        	   				
        	}else if(obj==fItem2){
        		
        		mainPanel.revalidate();
        		mainPanel.add(h.hoursPanel(), "p2");
	        	cl.show(mainPanel,"p2");  		
	        	
        	}else if(obj==fItem3){
        		
        		mainPanel.revalidate();
        		mainPanel.add(d.daysPanel(), "p3");
	        	cl.show(mainPanel,"p3"); 
	       }
	}
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("Weather Report");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Create and set up the content pane.
       WeatherDelegator weathDel = new WeatherDelegator();
       weathDel.addComponentToPane(frame.getContentPane());
        
        //Display the window.
        frame.pack();
        frame.setSize(930,630);
		frame.setResizable(false);
		frame.setVisible(true);
    }
   
    public static void main(String[] args) {   
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

my JPanels do not get updated. please suggest me some useful way of doing it..... i posted similar question in java moose saloon but no one answers me...... i think it does not comes to notice to anyone.. please guide me as i am doing with my own and no one guides me.... any help will highly be appreciated by me.... thanks in advance... please i am in a confused state...

Edited 5 Years Ago by Ezzaral: Fixed code tag.

probably you forget to add these JPanels, there isn't needed call invalidate for already visible Container, but revalidate()+ repaint() could to first parent JPanel

Inside public void addComponentToPane(Container pane) { method, initialize cl variable, same way as in the first line of public void actionPerformed(ActionEvent e){ method.
Look at the runtime exceptions, base info for you:
run:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at WeatherDelegator$1.actionPerformed(WeatherDelegator.java:60)
    at javax.swing.Timer.fireActionPerformed(Timer.java:292)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

thank you for your reply. it is working fine but only with 1 problem. I have not given full details of my code. Actually i am using 3 JPanels (CurrentPanel,HoursPanel,DaysPanel) in cardlayout(mainPanel). Now in those 3 JPanels i have added some JLabels, displayed Graphs, and used JTable and the corresponding value for them i got from database. i returned those JPanels and used in this (above given) main class.

as suggested i initialize cl, but my JPanel get swapped to DaysPanel (which i mentioned as (d.daysPanel()....), but i must remain in my JPanel. let me expalin properly.. if i am viewing first JPanel(c.currentPanel().....) then after 1min, my (c.currentPanel()....) got refreshed....working fine..., but it takes me to the third JPanel (d.dayspanel()....) automatically. But as i am viewing first JPanel, i must remain there and watch my JLabel, Graphs and JTable values got updated from database values.....

All are working fine, the only problem is that it swaps me the the third JPanel, but i must remain in my first JPanel as i am viewing my first JPanel.... please take me out of my problem...

...
//Create the panel that contains the "cards".
        mainPanel = new JPanel(new CardLayout());
        mainPanel.setBackground(Color.BLACK);
        //
        cl = (CardLayout) (mainPanel.getLayout());
        mainPanel.add(c.currentPanel(), "p1");
        mainPanel.add(h.hoursPanel(), "p2");
        mainPanel.add(d.daysPanel(), "p3");
        //
        int delay = 6000; // 1000 ms is 1sec
        timer = new Timer(delay, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // what realy I need here?
            }
        });
        //timer.start();
        pane.add(mainPanel);
        frame.setJMenuBar(menuBar);
        timer.start();
    }

    public void actionPerformed(ActionEvent e) {
        cl = (CardLayout) (mainPanel.getLayout());
        Object obj = e.getSource();
        if (obj == fItem1) {
            cl.show(mainPanel, "p1");
        } else if (obj == fItem2) {
            cl.show(mainPanel, "p2");
        } else if (obj == fItem3) {
            cl.show(mainPanel, "p3");
        }
    }
...

Edited 5 Years Ago by quuba: n/a

Thank you for your reply. But what should i write in the action performed method of timer... please give me some suggestion...... please help me i don't know what to do i tried everything but it is not working.

int delay = 6000; // 1000 ms is 1sec
        timer = new Timer(delay, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
c.currentPanel().revalidate();			    	 
c.currentPanel().repaint();

h.hoursPanel().revalidate();
h.hoursPanel().repaint();

d.daysPanel().revalidate();			    	 
d.daysPanel().repaint();

mainPanel.revalidate();
mainPanel.repaint();


}

}
....

i tried but it is not working. please give me some suggestion......

Edited 5 Years Ago by Ezzaral: Fixed code tag.

Replace your code with the part I already posted.
Now you have a frame with 3 panels, between which you can switch manually from menu bar.
What action you want to implement with an automatic timer?
Cyclic sequential review of each panel with the specified display time?

I need to refresh my JLabels, JTables and Graph values which are in the three respective JPanels with the new database values which comes from database......... i need just to refresh the JPanel..... so the code which i posted is not working.... please tell me what should i do.....

try force
mainPanel.updateUI();
or
javax.swing.SwingUtilities.updateComponentTreeUI(mainPanel);

(do same for JLabels ,JTables etc.)

Edited 5 Years Ago by quuba: n/a

This article has been dead for over six months. Start a new discussion instead.