Jframe content not visible after dynamic sizing

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.
 
B

Betty

Thomas Weidenfeller said:
Very bad idea.

I had a project where I used a null layout and it seems
easy at first, but every week there was a new problem.
Try GridLayout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top