Search code examples
javauser-interfacejtextfielduser-input

Improper use of JTextField (maybe) Help creating a GUI


I'm very new to Java and I'm trying to create a small program that reverses text (That part I've figured out).

Where I'm getting stuck on is my GUI, my envisioned plan for the gui is a window with a centered text field for user input then under it in the directly middle of the window a button that reverses the text from the above text box and outputs it in a text box below the button.

Right now I'm using JTextField boxes and after trying to make them look the way I want I'm getting the feeling that there's an easier way to do it, but I don't know it.

Here's my GUI class:

public class ReverseTextGUI extends ReverseRun implements ActionListener {

    public static JFrame frame;

    private JPanel northFlowLayoutPanel;
    private JPanel centerFlowLayoutPanel;
    private JPanel southFlowLayoutPanel;

    private final JButton reverse = new JButton("Reverse");
    private final JTextField userInput = new JTextField(50);
    private final JTextField reverseOutput = new JTextField(50);

    public void actionPerformed(ActionEvent e) {

        reverse.addActionListener((ActionListener) reverse);
        reverse.setActionCommand("Reverse");

        if ("algorithm".equals(e.getActionCommand())) {
            System.out.println("test");
        }

    }

    public void initUI() {

        northFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        northFlowLayoutPanel.add(userInput);
        userInput.setPreferredSize(new Dimension(150,100));

        centerFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        centerFlowLayoutPanel.add(reverse);

        southFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        southFlowLayoutPanel.setBorder(BorderFactory.createTitledBorder("Output text"));
        southFlowLayoutPanel.add(reverseOutput);
        reverseOutput.setPreferredSize(new Dimension(150,100));

        JFrame frame = new JFrame("Backwardizer");
        frame.setLayout(new BorderLayout());      // This is the default layout
        frame.add(northFlowLayoutPanel, BorderLayout.PAGE_START);
        frame.add(centerFlowLayoutPanel, BorderLayout.CENTER);
        frame.add(southFlowLayoutPanel, BorderLayout.PAGE_END);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setSize(750, 500);

    }

Any ideas how to either move the cursor to the start of the box (it shows up in the middle as of now) or a better way to accomplish what I'm trying to do?


Solution

  • For the reversing aspect, you can add the text from the first box to a string builder

    StringBuilder rev = new StringBuilder(firstBox.getText());
    String reversedText = rev.reverse().toString();
    secondBox.setText(reversedText);
    

    Something along those line should get the desired result if you nest it in the button action.