arch
arch

Reputation: 471

Java - Eclipse JTextField too big

I have added a JTextField to a JPanel using the Window Builder in Eclipse and no matter what I tried, the text field is displayed like this:

ScreenShot
(source: gyazo.com)

I have tried to change the preferred size, the maximum and minimum sizes and it still appears like this.

How do I fix this? What have I done wrong?

Thanks in advance.

UPDATE


Code: (it is all automatically generated by Google's Window Builder)

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Dimension;


public class Main extends JFrame {

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    textField = new JTextField();
    textField.setSize(new Dimension(6, 3));
    textField.setMinimumSize(new Dimension(6, 5));
    textField.setMaximumSize(new Dimension(6, 3));
    textField.setPreferredSize(new Dimension(6, 3));
    contentPane.add(textField, BorderLayout.WEST);
    textField.setColumns(10);
}

}

Upvotes: 1

Views: 4146

Answers (1)

Reimeus
Reimeus

Reputation: 159784

You could simply use the default layout manager FlowLayout for the JPanel contentPane. This will respect the components preferred size:

Remove (or comment out):

contentPane.setLayout(new BorderLayout(0, 0));

Always better to use a layout manager and avoid calling the setXXXSize methods for components for sizing. You can override getPreferredSize if necessary.

See: Doing Without a Layout Manager

Upvotes: 2

Related Questions