Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel is not getting refreshed

 
Sherin Dsouza
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All,
When a Combo box is clicked I want it to be replaced as a label but in the same frame!!
My code currently refreshes it bu creates a totally new frame.Kindly help!!

package components;

import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author user1
*/
public class TestCombo implements ActionListener {
private JPanel testPanel;
private JComboBox testCombo;
private JLabel testLabel;
private JFrame testFrame = new JFrame("Test Combo");
private String msg="default";
/** Creates a new instance of TestCombo */
public TestCombo() {
}

public JPanel createContentPane()
{
testPanel=new JPanel();
testPanel.setLayout(null);
String[] frequencyVal={"Select","Daily","Weekly","Monthly","Bi-Monthly"};
testCombo=new JComboBox(frequencyVal);
testCombo.addActionListener(this);
testCombo.setBounds(150,25,150,25);
testPanel.add(testCombo);
return testPanel;
}

private void createAndShowGUI() {
//Create and set up the window.

testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
TestCombo test=new TestCombo();
testFrame.setContentPane(test.createContentPane());

//Display the window.
testFrame.setSize(450, 260);
testFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JComboBox comboBox=(JComboBox)e.getSource();
String val=comboBox.getSelectedItem().toString();
if(val.equals("Daily")){
//TestDailyPanel Contains nothing but a label
TestDailyPanel dailyPanel= new TestDailyPanel();
testFrame.setContentPane(dailyPanel.createContentPane());
testFrame.setSize(450, 260);
testFrame.setVisible(true);
}

}
public static void main(String args[]){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestCombo testCombo=new TestCombo();
testCombo.createAndShowGUI();

}
});
}
}
Thanks,
Sherin
 
Fabian Gutierrez
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i think this is not the right place for that post, however, you could try using testFrame.setContentPane(dailyPanel); instead of testFrame.setContentPane(dailyPanel.createContentPane());

regards,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic