1

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?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Graham
  • 13
  • 3
  • To set cursor to start use setCaretPosition(0) on the JTextField. I am guessing this is not the whole class. It is unclear what your trying to do. – WillShackleford Aug 09 '15 at 21:58
  • Other than imports and packaging that's the full class. http://i61.tinypic.com/2qxxruo.png That picture shows what I'm trying to do, along with resetting the cursor I'm trying to use more than one line in the box – Graham Aug 09 '15 at 22:05
  • For multi-line input/output you should use a JTextArea rather than a JTextField. – WillShackleford Aug 09 '15 at 22:08
  • Cool thanks, that fixes my problem! – Graham Aug 09 '15 at 22:11
  • Don't mess with the `preferredSize` properties of components. Instead, try making the textfield readonly (`setEditable(false)`) – MadProgrammer Aug 09 '15 at 22:26

2 Answers2

1

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.

Rob Rushton
  • 156
  • 8
1

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?

  • JTextField#setCaretPosition, call this AFTER you've updated the text of the field
  • Make the field readonly, JTextField#setEditable and pass it false

Additionally, you could use a JList or JTextArea if you want to store multiple rows/lines of text

You should also avoid using setPreferredSize, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366