Update a jPanel

Hi In the following section of code Panel1() is called to create a panel. This section of code contains in 'if' statement and depending on the result a combo box may or may not be created. When updatePanelFromData() is called, I would like to somehow call Panel1 to update it. Does anyone have any suggestions?

public Panel1() {

super();

// Code removed to keep simple

if (data.getType().equals("Type1"))

{

Box = new JComboBox(Option);

panels[2].add(Box);

}

}

public void updatePanelFromData()

{

}

[584 byte] By [mmidea] at [2007-11-27 2:08:26]
# 1
Put that code inside a method, not inside your constructor.
CaptainMorgan08a at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 2
Such as?
mmidea at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 3
What is updatePanelFromData()going to update?What data is being used for the update, is the data from the ComboBox?Your example is a little vague, please provide a little more detail...
cafBeana at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 4
When updatePanelFromData() is called, I would like the JPanel to be updated in such a way that a set of JLables and JTextFields are displayed when the 'if' condition returns true and a different set of JLabels and JTextFields are displayed when the 'if' condition returns false.
mmidea at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 5
Pretty much setText method under JLabel class will do.
Icycoola at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 6
When the 'if' statement is true I would like 5 jLables to be displayed. When the 'if' statement is false I would like 1 jLable to be displayed. How can this be achieved?Thanks in advance
mmidea at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 7

if(condition)

{

//create 5 JLabels, then add them

}

else

{

//create 1 JLabel, then add it

}

CaptainMorgan08a at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 8
Thanks, but if the if else conditions are in public panel1 (see original post) then how can I call panel1 using public void pdatePanelFromData() to update the jpanel
mmidea at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 9
You have to declare it outside the else block. Try reading this article on variable scope. http://java.about.com/od/beginningjava/l/aa_vars1.htm
CaptainMorgan08a at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 10

public Panel1()

{

super();

if(condition)

{

create 5 lables

}

else

{

create 1 lable

}

}

public void updatePanelFromData()

{

}

What do I need to put inside public void updatePanelFromData() to update Panel1 everytime updatePanelFromData() is called?

Thanks

mmidea at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 11
http://java.sun.com/docs/books/tutorial/getStarted/index.htmlRead through that link. You should really learn the basics before moving on to Swing.
CaptainMorgan08a at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 12
I want to update the contents of panel1 (i.e. display 1 or 5 jlables) using public void updatePanelFromData() which is called everytime an items is selected in a jComboBox.Is there a way to do this similar to how I have described?
mmidea at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...
# 13
repaint() any good to you?
DokMoka at 2007-7-12 1:57:49 > top of Java-index,Java Essentials,New To Java...