Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

components displaying on the screen

 
sun moon
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Code follows like this. Doubt at @line 1
import java.awt.*
import java.awt.event.*
class TestAwt extends Frame implements ActionListener
{
Panel p1,p2;
Button b1,b2,b3;
TestAwt()
{
p1 = new Panel();
p2 = new Panel();
b1 = new Button("One");
b2 = new Button("Two");
b3 = new Button("Three");
setLayout(new BorderLayout());
add(p1,"North");
add(p2,"South");
p1.setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
b1.addActionListener(this);
setvisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
p2.add(b3);
b3.setvisible(true); // line 1
}
}
public static void main(String []arg)
{
new TestAwt();
}
};
my question is at line 1. Everything functions properly and also displays button "Three" when i click on button "One". But actually the button is seen when i maximmize or minimize the window.
I want to show the button when i clicked on the button "One".
thanks in advance
 
Grant Currey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldnt get your code to compile, so i made a minor alteration to your main and to the line one. You need to use setVisible() not setvisible().
secondly i added in validate(); after u add that component.
here is the adjusted source code
import java.awt.*;
import java.awt.event.*;
class TestAwt extends Frame implements ActionListener
{
Panel p1,p2;
Button b1,b2,b3;
TestAwt()
{
p1 = new Panel();
p2 = new Panel();
b1 = new Button("One");
b2 = new Button("Two");
b3 = new Button("Three");
setLayout(new BorderLayout());
add(p1,"North");
add(p2,"South");
p1.setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
p2.add(b3);
validate();
b3.setVisible(true); // line 1
}
}
public static void main(String []arg)
{
Frame f = new TestAwt();
f.setVisible(true);
f.setSize(400,400);
}
};
 
Dorothy Finkel-Laverty
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another solution is to put in a pack(); statement at the end of your actionPerformed() method. That will re-calculate the size of each component, panel, and the whole frame. Your problem was NOT that it wasn't there, it was just in RAM. You needed to get it on the screen.
Also, it would be a good idea to use a pack() at the end of your constructor, so that you get the preferred size of the components.
 
sun moon
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for your contribution and helping me out in this. I think now it is packed and validated in my brain
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"sun moon" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic