Invoke JPanel
So, I have a JFrame to contain two JPanel. The RecipePanel has only a "ENTER" button. When the user click the button, I want to set the DisplayPanel to paint String recipes1, recipes2... and I don't know how to communicate that. What I did wrong with the actionPerformed()? Please help. Thanks in advance.
class CompareFrameextends JFrame{
public CompareFrame(){
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth, screenHeight/3);
setLocation(screenWidth/4, screenHeight/4);
setTitle("Segcom");
RecipePanel recipePanel =new RecipePanel();
add(recipePanel, BorderLayout.NORTH);
DisplayPanel displayPanel =new DisplayPanel();
add(displayPanel, BorderLayout.CENTER);
pack();
}
}
class RecipePanelextends JPanel{
String recipes1="Food";
String recipes2="Water";
String recipes3="Pie";
public RecipePanel(){
JButton enter =new JButton("ENTER");
enter.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent event){
DisplayPanel panel =new DisplayPanel();
panel.add(new JLabel(recipes1));
panel.revalidate();
}
});
add(enter);
}
}
class DisplayPanelextends JPanel{
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("No Recipes",20,20);
}
}
[2798 byte] By [
Ken@Javaa] at [2007-11-27 4:02:24]

# 5
Would something like this work? It uses an interface to help communicate between two objects:
class CompareFrame extends JFrame implements myIFace
{
DisplayPanel displayPanel;
public CompareFrame()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth, screenHeight / 3);
setLocation(screenWidth / 4, screenHeight / 4);
setTitle("Segcom");
RecipePanel recipePanel = new RecipePanel();
recipePanel.registerMyFace(this);
add(recipePanel, BorderLayout.NORTH);
displayPanel = new DisplayPanel();
add(displayPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void upDatePanel(String fooString)
{
displayPanel.setMainLabel(fooString);
displayPanel.revalidate();
}
}
interface myIFace
{
void upDatePanel(String fooStr);
}
class RecipePanel extends JPanel
{
String[] recipes = new String[]
{
"Food", "Water", "Pie", "Meatballs", "Tuna Sandwiches"
};
int recipeNumber = 0;
myIFace myIF;
public void registerMyFace(myIFace myIF)
{
this.myIF = myIF;
}
public RecipePanel()
{
JButton enter = new JButton("ENTER");
enter.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
myIF.upDatePanel(recipes[recipeNumber]);
recipeNumber = ++recipeNumber % recipes.length;
}
});
add(enter);
}
}
class DisplayPanel extends JPanel
{
private JLabel mainLabel = new JLabel("No Recipes");
public DisplayPanel()
{
add(mainLabel);
}
public void setMainLabel(String labelStr)
{
mainLabel.setText(labelStr);
}
}
# 6
My take out on this is the panel or the owner of panel are best able to change the contents of the panel. The class in charge of it all is CompareFrame, so what I did was:
1) make CompareFrame's displayPanel an instance variable so it can be manipulated after constructor call. I am going to let CompareFrame make the changes to the displayPanel when necessary.
2) Create an interface called myIFace that allows communication between objects. All this interface does is guarantee that the implementing object (CompareFrame here) has a method called "upDatePanel()". This will allow another object (in this case the RecipePanel) call this method when it wants to.
3) Give RecipePanel an instance variable called myIF that is a myIFace variable. Thus RecipePanel can hold a pointer to the CompareFrame object and call it's upDatePanel method.
4) Register CompareFrame w/ the RecipePanel: basically tell RecipePanel what its myIF is pointing to.
5) Put a jlabel inside DisplayPanel that has text that can be changed.
It may seem like overkill, and perhaps it is, but I've been playing w/ interfaces and patterns, and find that they are too cool not try to use.
Good luck!
/Pete