Search code examples
javaswingjtextfieldflowlayout

How to make the cursor in a JTextField span multiple rows in FlowLayout?


enter image description here

How can I make the cursor start at the top left of the JTextField? The text just stays centered when I adjust the height with .setPreferredSize(). Here is the code for the fields and buttons.

public class GUIWindow extends JFrame{

    private JTextField inputBox = new JTextField(20);
    private JTextField outputBox = new JTextField(20);
    private JButton encodeButton = new JButton("Encode");
    private JButton decodeButton = new JButton("Decode");

    public GUIWindow(){
        JPanel mainPanel = new JPanel(new FlowLayout());
        outputBox.setPreferredSize(new Dimension(80, 80));
        inputBox.setPreferredSize(new Dimension(80, 80));
        outputBox.setEditable(false);
        mainPanel.add(inputBox);
        mainPanel.add(encodeButton);
        mainPanel.add(decodeButton);
        mainPanel.add(outputBox);
        Container container = getContentPane();
        container.add(mainPanel);
    }
}

Solution

  • If you are looking for a multiline control then you should use a JTextArea instead of JTextField.