• Post Reply Bookmark Topic Watch Topic
  • New Topic

Change panel

 
Vladimir Kositsky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
To change a panel in AWT application i am trying following:
public void actionPerformed()
{
remove(previousPanel);
add(newPanel,BorderLayot.CENTER);
invalidate();
update();
validate();
}
It does not work and above code is a reason?
Thanks in advance for advice.

 
patankar kishor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Vladimir,
Here is the working code for the change in panel.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet implements ActionListener
{
boolean showFirstPanel = true;
Button theFirstBtn = new Button("First");
Button theSecondBtn = new Button("Second");
Panel firstPanel = new Panel();
Panel secondPanel = new Panel();
public Test()
{
firstPanel.add(theFirstBtn);
secondPanel.add(theSecondBtn);
theFirstBtn.addActionListener(this);
theSecondBtn.addActionListener(this);
add(firstPanel);
add(secondPanel);
}
public void actionPerformed(ActionEvent theEvent)
{
if (showFirstPanel)
{
add(firstPanel);
remove(secondPanel);
showFirstPanel = false;
}
else
{
add(secondPanel);
remove(firstPanel);
showFirstPanel = true;
}
invalidate();
validate();
}
}
Originally posted by Vladimir Kositsky:
Hi!
To change a panel in AWT application i am trying following:
public void actionPerformed()
{
remove(previousPanel);
add(newPanel,BorderLayot.CENTER);
invalidate();
update();
validate();
}
It does not work and above code is a reason?
Thanks in advance for advice.

 
patankar kishor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet implements ActionListener
{
boolean showFirstPanel = true;
Button theFirstBtn = new Button("First");
Button theSecondBtn = new Button("Second");
Panel firstPanel = new Panel();
Panel secondPanel = new Panel();
public Test()
{
firstPanel.add(theFirstBtn);
secondPanel.add(theSecondBtn);
theFirstBtn.addActionListener(this);
theSecondBtn.addActionListener(this);
add(firstPanel);
add(secondPanel);
}
public void actionPerformed(ActionEvent theEvent)
{
if (showFirstPanel)
{
add(firstPanel);
remove(secondPanel);
showFirstPanel = false;
}
else
{
add(secondPanel);
remove(firstPanel);
showFirstPanel = true;
}
invalidate();
validate();
}
}

Originally posted by Vladimir Kositsky:
Hi!
To change a panel in AWT application i am trying following:
public void actionPerformed()
{
remove(previousPanel);
add(newPanel,BorderLayot.CENTER);
invalidate();
update();
validate();
}
It does not work and above code is a reason?
Thanks in advance for advice.

 
Vladimir Kositsky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
Vladimir
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!