0

My goal is to write something that is visible to the user in the JTextField and to display this text in the console. As of now, the JTextField is accepting text but nothing shows. No cursor, no text.

I've tried using textfield.setEditable(true), textfield.setEnable(true) and different fore and background colors but nothing happens.

Curiously I can use textField.setText("Random text") and it shows, but I cant delete it or edit this when the program is running and it's not included in the outputs from getText().

This is the program :

import acm.program.*;

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

@SuppressWarnings("serial")
public class TextFieldTest extends ConsoleProgram implements SomeConstants {

public void init() {
    setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);

    textField = new JTextField(20);
    add(textField, SOUTH);
    textField.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == textField)
        println("Hi, " + textField.getText());
}

private JTextField textField;
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Tor
  • 47
  • 11
  • can you comment entire logic of actionPerformed and than try again. – rbhawsar Oct 08 '12 at 07:01
  • Commented it out - nothing shows up when I try to write in the Jtextfield. I tried commenting out the addlistener, the logic and method in different combination. – Tor Oct 08 '12 at 08:19
  • If I create a JFrame and add JTextField it's working fine, so I guess it has something to do with the fact that I'm using acm (consoleprogram) ? – Tor Oct 08 '12 at 10:03

1 Answers1

1

If you're working with acm.program.ConsoleProgram, you may need to use Java SE 5 (version 1.5), as suggested here.

Addendum: As @Tor comments, java.awt.TextField may be acceptable under later versions of the JRE.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Ok, thank you. I'm using jre1.6. Finally using a normal TextField instead and it works ok for this purpose. – Tor Oct 09 '12 at 03:22