i am created small frame with 500,500 size, in my frame i added panel also, i have added a label and text box dynamically, i added 10 labels in my panel it's showing correctly but i will add another 5 labels and text box continuation of above added labels means after of 5 labels and text box's are hiding, so how to I EXTENDS A HEIGHT OF FRAME AUTOMATICALLY
-
Consider putting the labels in a panel that is in a `JScrollPane` instead, as seen in the labels of [this example](http://stackoverflow.com/a/5630271/418556). – Andrew Thompson Aug 06 '12 at 07:01
3 Answers
Calling the .pack()
method should do the trick:
public void pack()
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method. If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.

- 51,780
- 5
- 72
- 96
-
-
@aioobe: If the components fit length wise, shouldn't that remain unchanged? – npinti Aug 06 '12 at 06:38
-
there are a few important rulles and required knowledge how concrete LayoutManager works with min, max and PreferredSize came from its childs
I'd strongly suggest to add
(re)validate()
andrepaint()
for already visibleContainer
use
JScrollPane
as firstJComponent
added to theJFrame
/JDialog
/JWindow
and with to determine maximum size forJFrame
(best could be from display resolution), don't usepack()
in the case that maximum size is exceeded, otherwise containers bounds could be out of the screens
code example, have to add rules in point 2nd.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class AddComponentsAtRuntime {
private JFrame f;
private Container panel;
private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;
public AddComponentsAtRuntime() {
JButton b = new JButton();
b.setBackground(Color.red);
b.setBorder(new LineBorder(Color.black, 2));
b.setPreferredSize(new Dimension(600, 10));
panel = new Container();
panel.setLayout(new GridLayout(0, 1));
panel.add(b);
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel, "Center");
f.add(getCheckBoxPanel(), "South");
f.setLocation(200, 200);
f.pack();
f.setVisible(true);
}
private JPanel getCheckBoxPanel() {
checkValidate = new JCheckBox("validate");
checkValidate.setSelected(false);
checkReValidate = new JCheckBox("revalidate");
checkReValidate.setSelected(false);
checkRepaint = new JCheckBox("repaint");
checkRepaint.setSelected(false);
checkPack = new JCheckBox("pack");
checkPack.setSelected(false);
JButton addComp = new JButton("Add New One");
addComp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = new JButton();
b.setBackground(Color.red);
b.setBorder(new LineBorder(Color.black, 2));
b.setPreferredSize(new Dimension(600, 10));
panel.add(b);
makeChange();
System.out.println(" Components Count after Adds :" + panel.getComponentCount());
}
});
JButton removeComp = new JButton("Remove One");
removeComp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = panel.getComponentCount();
if (count > 0) {
panel.remove(0);
}
makeChange();
System.out.println(" Components Count after Removes :" + panel.getComponentCount());
}
});
JPanel panel2 = new JPanel();
panel2.add(checkValidate);
panel2.add(checkReValidate);
panel2.add(checkRepaint);
panel2.add(checkPack);
panel2.add(addComp);
panel2.add(removeComp);
return panel2;
}
private void makeChange() {
if (checkValidate.isSelected()) {
panel.validate();
}
if (checkReValidate.isSelected()) {
panel.revalidate();
}
if (checkRepaint.isSelected()) {
panel.repaint();
}
if (checkPack.isSelected()) {
f.pack();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime();
}
});
}
}

- 109,525
- 20
- 134
- 319
There's no boolean which you can set to true in order to make the frame grow automatically.
You probably want to do getPreferredSize()
of the content panel each time you add something, and do frame.setSize(...)
to accommodate for the new components.
Here's a demo
public class FrameTestBase {
public static void main(String args[]) {
final JFrame t = new JFrame();
t.getContentPane().setLayout(new GridLayout(-1, 1));
t.add(new JButton(new AbstractAction("Add label") {
@Override
public void actionPerformed(ActionEvent arg0) {
t.getContentPane().add(new JLabel("hello"));
// Auto-adjust height.
Dimension dim = t.getContentPane().getPreferredSize();
dim.width = 500;
t.setSize(dim);
}
}));
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(500, 500);
t.setVisible(true);
}
}

- 413,195
- 112
- 811
- 826