Sorry.... i have posted this question before in JavaRanch Forum but i did not get any satisfactory answer to my question. probably no one notices there. I am posting here with the hope that someone will come forward and give me some suggestion.

i am using java swing and i have JPanels in cardlayout. And when i click a JMenuItem then it displays the respective JPanel for that JMenuItem. But my problem is that i could not able to refresh that particular JPanel.....

In each JPanels(in cardlayout) there are JLabels and Graphs containing database values. Now my problem is that when i select different menuItems it displays the required JPanels, but it leaves the previous JPanel in that state in which i left.....

Let me explain properly.... For example in one JPanel there is a combobox, having list of Temperature, Rainfall, Wind. Now at first it displays Temperature Graph and if i select Rainfall then it displays Rainfall Graph. Working fine.... Now if i select another JMenuItem then it takes me to the respective JPanel, but it leaves the JPanel in that state where i left i.e when i again select JMenuItem (for that JPanel) then i found the JPanel with the Rainfall Graph but i need the JPanel with the Temperature Graph..... i.e the JPanels must be refreshed at each switching (of JPanels), by selecting JMenuItem

Probably i could explain you properly..... please give me some suggestion....

Recommended Answers

All 13 Replies

can you please post a link to your question

for informations that you posted was Rob's answwer more that you could be excepted

no there withou any...

there isn't some finall and correct suggestion,

create JFrame http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html

put here JPanel http://download.oracle.com/javase/tutorial/uiswing/components/panel.html

build JMenu http://download.oracle.com/javase/tutorial/uiswing/components/menu.html with JMenuItem

add http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html for JMenuItem (s) with decision action for JPanel

for any next help (on any Java Forums) is better always post question with your runnable code

you can use validate() and repaint() method to refresh JPanel

frame.validate();
panel.validate();

Thank you. I could able to use all these. I have done everything. What i needed is that i need to refresh the JPanel....

my code is very large. i could not able to post the whole code. i just post some part of code.... This is the code from main class. Here i just make an object for HoursPanel.....

HoursPanel h=new HoursPanel();
//......
//.....

currItem.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
                                    h.panelClick.repaint();
                                    cardLayout.show(mainPanel,"p2");

    }
});

This is the code for action performed method for JMenuItem. when i click (currItem) then it must be refreshed. Here h is the object for HoursPanel. In the main class i have added some JMenu with JMenuItem and also used cardlayout.(here(above) mainPanel is the JPanel in cardlayout)

My HoursPanel is a separate class where in a JPanel(allpanel) displayed some graphs, some JLabels and a combobox for selecting graphs..... The Graphs values are taken from database and also the JLabels contain database values.... and after that i returned the JPanel(allPanel)... and that panel is then added to the cardlayout of main class......

By default the graph shows the temperature graph with temperature as the selected item in combobox..... when i select rainfall then it displays me rainfall graph.... Till now everything is working fine for me.....

Now if i switch to different JPanel by clicking another JMenuItem then it left JPanel with the rainfall graph. When i again switch the JMenuItem for the JPanel, i found the rainfall graph, but i need to get temperature graph....(by default). SO it does not get refreshed.... so again i have to select temperature from the combobox to see the temperature graph..... but by default i must have temperature graph..... Hope i could able to explain you...... please guide me... please... i have been trying but still now not a single sign of success......

In actionPerformed method you can write code to set your jpanel to switch in default condition that means you can set condition that select temperature graph.

Thank you for your suggestion. It give me a little hope of success... But what would i write. i have two JPanel(one for the combobox and another for the Graphs) in one JPanel. what would i write in action performed method......????? please guide me.... i could able to understand a little bit. How could i implement this.... please suggest me.

can you show me the code for the actionperformed Method of jmenuitem and combobox event code

if you want i can post the whole code.... but it is very long.... I have used many things in the HoursPanel class... so i have only posted a part of code... same for the main class also....

now the JMenuItem is in the main class and the JPanel to get refreshed is in the HoursPanel class. All the methods in HoursPanel are declared as public because i have to use in the main class. Now in the action performed method for the JMenuItem i have to refresh which part???? i get a bit of confused....

actually i am not getting but what i understand i
1) your using Jmenu Item to go from one panel to other suppose Jm1 for panel1 and jm2 for panel2
2) when you change contain in panel1 and go to panel2 by clicking jm2 and again come back it doesn't go back to default state
3) combo box is used to travel in the jpanel(that mean change the contain of the jpanel)


hence you can write the code in jMenuitem to change japnel contains to default status
that is which code you used in jcombobox to show that temperature panel

i hope u will get it .
without code i can suggest that much only

commented: ask fro code +8

Thank you very much. i have posted the code. Actually, i have posted two answer at the same time. so first time i posted the code and second time i wrote something. i think my first post was not posted successfully.... Sorry for that.

I think you can able to understand my problem. yaa! you are right. But i am using two calsses for that....
(1) The main class where i used the JMenuItem to swap between JPanels which are in cardlayout....

//....
//....(shown only the code for the JMenuItem)
currItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){ 
           cardLayout.show(mainPanel,"p1");
    }
});

here mainPanel is the JPanel in cardlayout.

(2) I have a separate class where i have done everything and i returned the jpanel to the main class. This class(HoursPanel) contain graphs...., JLabels for displaying temperature, rainfall values...., combobox for selecting the graphs...., JTables for displaying the values for the graphs......etc.

my small part of the HoursPanel class is....

String[] names ={"Temperature","Rainfall","Wind"};
JComboBox list=new JComboBox(names);
list.setSize(50,5);

list.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {     
    JComboBox cb = (JComboBox)e.getSource();
    String str = (String)cb.getSelectedItem();
    if(str.equals("Temperature")){
        cardLayout.show(cpanel,"p1");
    }else if(str.equals("Rainfall")){           
        cardLayout.show(cpanel,"p2");
    }else if(str.equals("Wind")){
        cardLayout.show(cpanel,"p3");
    }   
});

here cpanel is the JPanel in cardlayout.... and after doing all the calculation i returned the JPanel to the main class....

i have used separate separate methods for all i.e for graphs different methods, for JTables different methods and declared all the methods as public. Now how i can use??? i tried something like creating an object of HoursPanel in the mainclass and trying to refresh the Jpanel, but no any success.... please be with me and guide me till end...

I have done it. thank you very much for all your replies. my problem was that i add all the JPanels in cardlayout at first time, so it gives me error. So, i added the JPanels for the respective JMenuItem at the actionPerformed method. So when i click the JMenuItem it get refreshed automatically.

thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.