Search code examples
javaswingjtextfield

Java Text Field Input Issue


So I'm trying to have users type in an answer to the question via a text field, but the way I have it set up, the name field is only being stored inside the ActionListener Class and not the QName class. I've checked by trying to output Name using the Name() function. Does anyone have any ideas on how I can pull out the info and store it into QName's name field? Thanks in advance

Code for my QName function Code showing null output


Solution

  • The reason why your name field is null is because typing in your textfield doesn't generate any events that are caught by the associated listener.

    You seem to have attached an ActionListener directly to your JTextField. The more typical (and for your purposes, the more correct) implementation is to associate the listener to a button in your QName object.

    To fix your code, you're going to have to create a button. Let's assume you call it "OK". Then associate an ActionListener to the button instead of the textfield. Then your user can type a character name and click OK. In turn, your listener will detect the button press and set name to the contents of the textfield.

    I'd post sample code, but I'm not going to key in all the code from your picture (this is another reason why you don't post pictures).