• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

components displaying on the screen

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
};
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic