-1

My program is supposed to get information about a person (first and last names, address, phone) so that it can add the person into an address book.

I made a JLabel which gives instructions on what to enter right below. Below the JLabel is a JTextField which has an ActionListener listening to what is being entered. My method has about 8 ActionListener's with 8 actionPerformed methods. I am running into trouble it is not working. I can't figure out any other way.

River
  • 8,585
  • 14
  • 54
  • 67
Beatle
  • 123
  • 3
  • 1
    Your question uses both Swing and SWT tags, and this puts your question into an impossible state -- either this is a Swing question or it's an SWT question, but it can't possibly be both. Please remove the inappropriate tag (likely the SWT tag). – Hovercraft Full Of Eels Feb 13 '16 at 23:08
  • Swing and swt is the same thing. SWT stands for swing widget something. – Beatle Feb 13 '16 at 23:30
  • 2
    `"Swing and swt is the same thing"` -- No, you are very mistaken, in fact I'd say **extremely** mistaken, as they are two completely different GUI libraries. The text of your question shows that yours is a Swing question -- so get rid of the swt tag, please, for your own good as you will confuse folks on this site if you persist with the wrong tag. – Hovercraft Full Of Eels Feb 13 '16 at 23:32
  • 1
    SWT = "Standard Widget Toolkit". Just Google it before making such statements. Seriously. – Hovercraft Full Of Eels Feb 13 '16 at 23:33
  • Well i installed a swing library on eclipse and swt was checked so its the same. Both for GUI. I am typing on a galaxy s4 so i didn't feel like googlinh it.. too much truble – Beatle Feb 13 '16 at 23:36
  • 2
    Then listen to the experts -- I've used them both, and again you are wrong. But you seem to want to ignore this, so I will leave. – Hovercraft Full Of Eels Feb 13 '16 at 23:36
  • I've deleted it for you. – Hovercraft Full Of Eels Feb 14 '16 at 00:01
  • thanks. I didnt know how to delete it so i didn't do it. still the same in my opinion. swing. such a stupid name. – Beatle Feb 14 '16 at 09:22

2 Answers2

3

You can create an instance of the listener and reuse it across the class, like:

...

OnChangeListener listener = new OnChangeListener() {
    //All the code here
};

...

textField1.addOnChangeListener(listener);
textField2.addOnChangeListener(listener);
textField3.addOnChangeListener(listener);

...
thyago stall
  • 1,654
  • 3
  • 16
  • 30
3

Best to create a form, perhaps with GridBagLayout or MigLayout that holds displays JLabel/JTextField pairs, so that the user can enter all the data on a single simple form, much like most software you use.

If you absolutely must use a single JTextField, then you should use a single ActionListener, but change how it responds based on the state of the GUI. That is, perhaps use an int counter variable, that you increment each time data is entered, and base what the listener does with the data based on the value held by the counter.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I would LOVE to make a pop out form which asks the user to enter in information on different lines. Only problem is i don't know how to do that and i have googled everywhere with no luck. So if you have a goog tutorial i would love to know. That is my first option. But seems more complex to me. – Beatle Feb 13 '16 at 23:19
  • @Beatle234: well, take a look at my code and answer [here](http://stackoverflow.com/a/10150308/522444) that shows just such a form at work. – Hovercraft Full Of Eels Feb 13 '16 at 23:23