S
Scott Steiner
Hi,
I have a simple JFrame that just has a JButton positioned on it. The
frame size is dynamically set so that it encloses the button. Here is
the simple code:
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
public class A extends JFrame
{
public A()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(null);
JButton jb = new JButton("Button");
jb.setBounds(100,100,75,25);
c.add(jb);
this.setVisible(true);
this.setSize(200,this.getInsets().top + jb.getY() +
jb.getHeight() + this.getInsets().bottom);
this.setVisible(true);
}
public static void main(String[] args)
{
new A();
}
}
Question: as you might have noticed, I require "this.setVisible(true)"
twice in the above code. If I remove the second call then the button is
not displayed, it will only become visible once you resize the frame
slightly and enforce a repaint I presume. I tried putting this.repaint()
instead of the second this.setVisible(true) but this also didn't work.
Is it really necessary to call this.setVisible(true) twice like I have
done above? This seems a litte suspicious to me.
Thanks for any input on the matter.
I have a simple JFrame that just has a JButton positioned on it. The
frame size is dynamically set so that it encloses the button. Here is
the simple code:
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
public class A extends JFrame
{
public A()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(null);
JButton jb = new JButton("Button");
jb.setBounds(100,100,75,25);
c.add(jb);
this.setVisible(true);
this.setSize(200,this.getInsets().top + jb.getY() +
jb.getHeight() + this.getInsets().bottom);
this.setVisible(true);
}
public static void main(String[] args)
{
new A();
}
}
Question: as you might have noticed, I require "this.setVisible(true)"
twice in the above code. If I remove the second call then the button is
not displayed, it will only become visible once you resize the frame
slightly and enforce a repaint I presume. I tried putting this.repaint()
instead of the second this.setVisible(true) but this also didn't work.
Is it really necessary to call this.setVisible(true) twice like I have
done above? This seems a litte suspicious to me.
Thanks for any input on the matter.