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...