javaswingcardlayout

Java Applet, GridLayout stretching TextFields Horizontally


I'm writing a Java Applet and on one of my CardLayout Panels, the GridLayout within the BorderLayout.CENTER is streching the TextFields horizontally and vertically (this is ok because I can set vertical spacing between the parts of the grid). Can I stop the TextFields from being so wide and can I center the text within them?


Solution

  • Consider nesting the JTextField-containing JPanels inside of another FlowLayout using JPanel. This way the FlowLayout using JPanel is the one that gets stretched while the JTextFields are sized based on what you set the int col property to be. Note that this will fail miserably if the nesting FlowLayout using JPanel is smaller than the preferredSize of its child JPanel.