Reputation: 1597
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
Upvotes: 1
Views: 108
Reputation: 91
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
Upvotes: 1
Reputation: 6808
There are many ways to achieve it. First, is to use a proper Layout Manager. It is always recommended not to mess with a bunch of hardcoded values and let APIs to do what they supposed to do. However, without a short self contained example is hard to guess what you know and use and what you don't.
So I guess another way is to create an empty border to the component (or its parent container accordingly). In JTextField
though, you might want to keep the default border, so you will have to use a compound one:
JTextField field = new JTextField(20);
Border outsideBorder = field.getBorder();
Border insideBorder = BorderFactory.createEmptyBorder(10, 0, 10, 0); //insets
field.setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
frame.add(field);
Upvotes: 1