Size (height) Java Swing JTextfield
I am having problems getting the JTextfield in my program to be sized appropriately, I am trying to change the textbox with the text "Price must be real value". This is what I would like to look like:
And this is what it looks like at the moment:
My code looks like this:
package test1;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.*;
public class Test1{
//Should components be final? Should they be defined outside of the class?
private final JTextArea outputArea = new JTextArea();
private final JTextField errorReportField = new JTextField();
private final JPanel inputPanel = new JPanel();
private final JLabel nameLabel = new JLabel("Item Name");
private final JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private final JLabel priceLabel = new JLabel("Price per unit (or L) in pence");
private final JTextField nameField = new JTextField(10);
private final JTextField numberField = new JTextField(10);
private final JTextField priceField = new JTextField(10);
private final JButton addVolumeButton = new JButton("Add by Volume");
private final JButton addNumberButton = new JButton("Add by number of units");
public Test1() {
JFrame frame = new JFrame("Fuel station shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
outputArea.setEditable(false);
outputArea.setRows(30);
JScrollPane scrollPanel = new JScrollPane(outputArea);
scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
errorReportField.setEditable(false);
//Better way of adding multiple components to panel?
inputPanel.setLayout(new FlowLayout());
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(numberLabel);
inputPanel.add(numberField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(addVolumeButton);
inputPanel.add(addNumberButton);
Container contentPane = frame.getContentPane();
//Why is it adding components from bottom to top?
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(scrollPanel);
contentPane.add(errorReportField);
contentPane.add(inputPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Test1 test = new Test1();
}
}
So, basically I would like to increase the height of the textbox while keeping the text in the center, and also changing the background to white, keeping it unusable.
source to share
Use BorderLayout
instead BoxLayout
.
Place the text area in BorderLayout.CENTER
and wrap the text box and bottom pane in another pane with GridLayout(0, 1)
. Add this panel to BorderLayout.PAGE_END
. The text box will be the same size as the bottom panel
Besides
- Swing application must be run / run on Thread Dispatch Thread. See Starter Topics
- For general information about layout managers see Layout Components with Container
- Also not sure why your field and text are not editable.
Refactoring
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class Test1{
//Should components be final? Should they be defined outside of the class?
private final JTextArea outputArea = new JTextArea();
private final JTextField errorReportField = new JTextField();
private final JPanel inputPanel = new JPanel();
private final JLabel nameLabel = new JLabel("Item Name");
private final JLabel numberLabel = new JLabel("Number of units (or Volume in L)");
private final JLabel priceLabel = new JLabel("Price per unit (or L) in pence");
private final JTextField nameField = new JTextField(10);
private final JTextField numberField = new JTextField(10);
private final JTextField priceField = new JTextField(10);
private final JButton addVolumeButton = new JButton("Add by Volume");
private final JButton addNumberButton = new JButton("Add by number of units");
public Test1() {
JFrame frame = new JFrame("Fuel station shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//outputArea.setEditable(false);
outputArea.setRows(30);
JScrollPane scrollPanel = new JScrollPane(outputArea);
scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//errorReportField.setEditable(false);
//Better way of adding multiple components to panel?
inputPanel.setLayout(new FlowLayout());
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(numberLabel);
inputPanel.add(numberField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
inputPanel.add(addVolumeButton);
inputPanel.add(addNumberButton);
Container contentPane = frame.getContentPane();
//Why is it adding components from bottom to top?
contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPanel);
JPanel wrapper = new JPanel(new GridLayout(0, 1));
wrapper.add(errorReportField);
wrapper.add(inputPanel);
contentPane.add(wrapper, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
Test1 test = new Test1();
}
});
}
}
Note
If you want the box to be slightly larger (in height), you can add an empty border to inputPanel
, causing the box to expand as well. But this will mean that there inputPanel
will be a little more (may or may not be desirable). But in any case, with the help of GridLayout, the field and inputPanel
will be the same size.
inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
Another option is to provide panel wrapper
a BorderLayout
and add a margin in the center, giving it EmptyBorder
to expand its size.
contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPanel);
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(errorReportField);
wrapper.add(inputPanel, BorderLayout.PAGE_END);
Border border = errorReportField.getBorder();
CompoundBorder compound = BorderFactory.createCompoundBorder(
border,BorderFactory.createEmptyBorder(10, 10, 10, 10));
errorReportField.setBorder(compound);
contentPane.add(wrapper, BorderLayout.PAGE_END);
This will give a little headroom to the left of the field (which may or may not be desirable). If you don't want this, just change the values ββfor the empty border.
createEmptyBorder(top, left, bottom, right)
source to share