Search code examples
javaswingjtextfield

Increase height of the JTextField component


I am creating a calculator using Java Swing. But I am not able to increase the height of the JTextField component. Please find the code below:

JTextField textField = new JTextField(100);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(textField, BorderLayout.PAGE_START);

The text field appears very small. How do I increase the height of the textbox to make it more prominent ?

Thanks


Solution

  • I think better is to let the text field determine the height based on the font used.

    String str = "21564";
    JTextField textField = new JTextField(str, 100);
    Font bigFont = textField.getFont().deriveFont(Font.PLAIN, 150f);
    textField.setFont(bigFont);
    mainPanel.add(textField);
    

    See the Swing Tutorial