0

everytime I write something in the text box and press enter, I'm able to see the message in the "Uneditabletest", however, if I write something different and press enter, It will replace my last message, what do I do to have them saved one after another? Like if it was a chat or something similar.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tutorial extends JFrame {

JTextField jtfText1, jtfUneditableText;
String disp = "";
TextHandler handler = null;

public Tutorial() {
    super("TextField Test Demo");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
    jtfText1 = new JTextField(10);
    jtfUneditableText = new JTextField("Uneditable text field", 20);
    jtfUneditableText.setEditable(false);
    container.add(jtfText1);
    container.add(jtfUneditableText);
    handler = new TextHandler();
    jtfText1.addActionListener(handler);
    jtfUneditableText.addActionListener(handler);
    setSize(325, 100);
    setVisible(true);
}

private class TextHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jtfText1) {
            disp = "text1 : " + e.getActionCommand();
        } else if (e.getSource() == jtfUneditableText) {
            disp = "text3 : " + e.getActionCommand();
        }

        jtfUneditableText.setText(disp);

        }
    }

    public static void main(String args[]) {
        Tutorial test = new Tutorial();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
ylun.ca
  • 2,504
  • 7
  • 26
  • 47
Bob Lozano
  • 838
  • 2
  • 13
  • 38
  • have you tried to use a list to store your inputs (http://stackoverflow.com/questions/858572/how-to-make-a-new-list-in-java)? If you would rather have your program store up to a certain amount of text (ie. up to 10 previous strings) you can use an array to store your strings instead of a list. – ylun.ca Feb 16 '14 at 02:38

2 Answers2

0

Simply append it to the end. In this case, it would be jtfUneditableText.setText(jtfUneditableText.getText() + "\n" + disp)

GreySwordz
  • 368
  • 1
  • 9
0

There should be something like getText() in jtfUneditableText, so you can do jtfUneditableText.setText(jtfUneditableText.getText() + "\n" + disp);

So the text in jtfUneditableText will be what was there before (jtfUneditableText.getText()) plus a line break (\n), and the new text.

xp500
  • 1,426
  • 7
  • 11
  • Could you Edit my code? I'm kinda of just learning so I'm not sure how to add getText ): – Bob Lozano Feb 16 '14 at 02:47
  • you don't add getText(). It is a method of the JTextField Class. So just replacing the last line of the actionPerformed method with the line I wrote should work :). Hope that helps! I also added some more info to my answer, so maybe you understand better. – xp500 Feb 16 '14 at 02:49
  • However, the "/n" is not making a new line as expected Is there anything I can do? to have them listed – Bob Lozano Feb 16 '14 at 02:55
  • According to http://stackoverflow.com/questions/6662029/problem-with-multi-line-jtextfield you should use JTextArea instead of JTextField, since it doesn't support multiple lines. And please check that I used \n not /n – xp500 Feb 16 '14 at 02:58