• Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBagLayout  RSS feed

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why when I resize this frame to be of a lesser height than when packed does the textfield become extremely narrow?

 
Ankit Pradhan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The shrinking of text field happens bcoz the layout resizes the shape of each and every component placed on it, whenever there is any change in the size of the window.
One way to resolve this problem is to use : JToolBar
Try the following code:

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.*;

public final class Temp3{

JToolBar toolBar = new JToolBar();
JTextField textField = new JTextField();
JPanel panel = null;
GridBagConstraints c = null;
public static void main(String[] args)
{
JFrame f = new JFrame("test");
f.setLayout(new GridBagLayout());
panel = new JPanel();
panel.setLayout(new GridBagLayout());
c = new GridBagConstraints();
panel.add(new JTextField(24),c);

toolBar.add(panel);
toolBar.setFloatable(false);

f.getContentPane().add(toolBar);
f.pack();
f.setVisible(true);

}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!