0

I want to create a little swing application that takes input from the text field and puts it in a ArrayList. The code is not working and I need some help.

EDIT. Fixed the syntax. Now the i cant get the code to work. It wont add the textfield data to the list. Please help.

Here is the code:

public class GUI extends JPanel implements ActionListener{

JButton button = new JButton(" >> CLICK ME <<");
final JTextField txt = new JTextField("Enter player name.");
static List <String> player = new ArrayList <String> ();

public GUI(){

    txt.setBounds(1, 1, 300, 30);

    JButton button = new JButton(" >> ADD PLAYER <<");

    button.setBounds(40, 40, 200, 40);

    setLayout(null);
    add(txt);
    add(button);

    button.addActionListener(this);


}

public void actionPerformed(ActionEvent e) {

    if (e.getSource().equals(button)) {
        player.add(txt.getText());
        System.out.println("Added: " + txt.getText() + " to the list.");
    }
    else
        System.out.println("Adding the textField data to the list did not work.");


}


public static void main(String[] args) {

    JFrame frame1 = new JFrame("Memory Game. Add player function.");
    frame1.getContentPane().add(new GUI());

    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(310, 130);
    frame1.setVisible(true);

}

I am new to swing and have only been coding for a couple of weeks. :/

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kharbora
  • 129
  • 1
  • 8
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Nov 14 '14 at 12:17
  • @AbdiTem Whats your new Question? – Lukas Hieronimus Adler Nov 14 '14 at 12:37
  • @LukasHieronimusAdler the adding function is not working. – Kharbora Nov 14 '14 at 12:51

3 Answers3

2

You can get text from JtextField getText() method and use add() to add it to a list player. Given below is the code.

player.add(txt.getText());

Check the links below for more details:

Johny
  • 2,128
  • 3
  • 20
  • 33
2

You need to add the ActionListener to the JButton with

button.addActionListener(this);

And then add in the actionPerformed the text of the JTextfield to the list :

player.add(txt.getText());
dor47
  • 81
  • 7
  • Thank you. I have updated my question, please feel free to read through it. :) – Kharbora Nov 14 '14 at 12:31
  • Try to know (with system.out.println or debug mode) what is the source in e.getSource(), if it is really your button. – dor47 Nov 14 '14 at 13:09
1

You only need to get the text from the textfield.

player.add(txt.getText())

This will do what you want to have.

There you will get the current String which is in the TextField. So you can add it into your List.

Lukas Hieronimus Adler
  • 1,063
  • 1
  • 17
  • 44