aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes components displaying on the screen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "components displaying on the screen" Watch "components displaying on the screen" New topic
Author

components displaying on the screen

sun moon
Greenhorn

Joined: Oct 09, 2002
Posts: 28
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

Joined: Oct 14, 2002
Posts: 2
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);
}
};


Cuzz-G
Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
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

Joined: Oct 09, 2002
Posts: 28
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

Joined: Oct 18, 2000
Posts: 4121

"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!


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: components displaying on the screen
 
Similar Threads
Layouts
Layout Problems
AWT components display
Exam tip & a question
basic things with awt