0

I have a JPanel and set it as follows:

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

Then I add a JTextField, which takes up the entire panel:

JTextField field = new JTextField();
panel.add(field);

However, when I try to resize it:

panel.setPreferredSize(new Dimension(20,400));

Nothing happens. Why? I am using BoxLayout in order to put my JLabel and JTextField components in vertical order. If it's not possible to resize the panel, can I at least resize the JTextField so it does not take up the entire space?

Example:

import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUI extends JFrame 
{
    public GUI() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setPreferredSize(new Dimension(20,400));
        JTextField field1 = new JTextField();
        JTextField field2 = new JTextField();
        panel.add(field1);
        panel.add(field2);
    }
}
Community
  • 1
  • 1
Playdowin
  • 94
  • 1
  • 11
  • And in which container does your panel exist? Sorry, but you better put up a real **complete** [mcve] here. There is no point in explaining that you have more elements in your code ... show that code; or better: reduce it to something that we can easily run ourselves! – GhostCat Jan 30 '17 at 18:39
  • You mean the JFrame? `public class GUI extends JFrame` – Playdowin Jan 30 '17 at 18:40
  • 1
    I mean a [mcve] ... including all the code required to allow us to repro your problem. And yes, your frame, but also the code that shows how you embed your panel in your frame; and how you add those other elements to your panel! – GhostCat Jan 30 '17 at 18:41
  • The cause of the problem is the setting of BoxLayout. If you want an example, I'd how to recreate a program because this is taken from a large program consisting of several subclasses. I guess I could create an example, but I hoped there would be a simple solution to this... – Playdowin Jan 30 '17 at 18:43
  • `panel.setLayout(new BoxLayout(paneFields, BoxLayout.PAGE_AXIS));` shouldn't you be using `panel` instead of `paneField`? See [Should I avoid the use of setPreferred|Minimum|MaximumSize in Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi), the general consensus says yes. You should instead override the `getPreferredSize()` method. *"I guess I could create an example"* Yes! That's what we need in order to give you an accurate answer – Frakcool Jan 30 '17 at 18:43
  • Added example now – Playdowin Jan 30 '17 at 18:49
  • You should not be controlling the size of the panel. The layout manager will determine the size of the panel as you add components to the panel. – camickr Jan 30 '17 at 18:50
  • @camickr It was a desperate attempt to adjust the size of the JTextFields – Playdowin Jan 30 '17 at 18:51

2 Answers2

7

can I at least resize the JTextField so it does not take up the entire space?

A BoxLayout will resize a component to fill the space in the panel up to the maximum size of the component. A text field doesn't have a maximum size.

You can prevent the text field from growing by controlling the maximum size:

panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
JTextField textField = new JTextField(10);
textField.setMaximumSize( textField.getPreferredSize() );
panel.add( textField );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • It worked, I can now adjust the length! Is there a possibility I can adjust the width as well? Thanks! – Playdowin Jan 30 '17 at 18:51
  • @Playdowin, Length and width mean the same to me. You control the preferred size (ie width) by specifying the number of columns when you create the text field. In my example the text field will be sized to hold 10 "W" characters. – camickr Jan 30 '17 at 18:52
  • 2
    There is no need to control the height. The text field is given a preferred size based on the font of the text field. Don't try to micro manage the size of components. You can change the `Font` if you want larger text, but you must do this BEFORE changing the maximum size. If you want extra space around the text and its border then you will need to use a custom `Border`. Read the Swing tutorial on [How to Use Borders](http://docs.oracle.com/javase/tutorial/uiswing/components/border.html). You would need a `CompoundBorder`, containing a `LineBorder` and an `EmptyBorder`. – camickr Jan 30 '17 at 18:59
  • Thanks! You helped me a lot!! – Playdowin Jan 30 '17 at 19:01
3

Although @camickr already solved your question, I need to say some extra recommendations:

  1. Don't extend JFrame, JFrame is a rigid component, instead build you application around JPanels. See: Using extends JFrame vs creating an instance of it

  2. You're not placing your program on the Event Dispatch Thread (EDT) this can be solved by:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Your constructor here
            }
        });
    }
    
  3. I mean vertical length

    To specify the height of the JTextField you could override its getPreferredSize() method as follows, which gives your field1 a height of the double of its original value.

    JTextField field1 = new JTextField(10) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(super.getPreferredSize().width, super.getPreferredSize().height * 2);
        }
    };
    

    Which will make your GUI to look like this:

    enter image description here

    OR

    set the field2 font like:

    JTextField field2 = new JTextField(10);
    field2.setFont(new Font("Courier", Font.BOLD, 30));
    

    which then would make your field2 to automatically calculate its preferredSize according to the Font used:

    enter image description here

    It all depends on what your needs are.

  4. Don't forget to call pack() instead of setPreferredSize() or setSize() on your JFrame

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89