3

I'm trying to write a store inventory program, where the program reads in a file of the current inventory which is an ArrayList where the product class just defines each product with the name, price etc. I'm struggling to find a way for the user to enter information for a new object in Product in a JTextField and save all the info after it is all entered and create the object and put it into the ArrayList. Currently my ActionListener class works but when i enter info in a text box and press enter it just pops up a message telling me what i entered. Thanks!

2 Answers 2

4
  • Make a list
  • Make a TextField
  • Make Button
  • Add Listener to button
  • Get the text from the text field or text area
  • Add to array list object
  • Done :)

Here is all the work you need to do:

ArrayList<String> arrayObject= new ArrayList<String>();
JButton button = new JButton();
JtextField textBox = new JtextField ();

button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //inside your action listener:
        String add_item_to_array = textBox.getText().trim();
        arrayObject.add(add_item_to_array);             
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

please add some explanation to your answer
I have added the info requested :) please bump, as this answer is more direct than the one above. Thanks @Philip
1

Something in your ActionListener like this should work

String description = descriptionTextBox.getText();
String price = priceTextBox.getText();

Product p = new Product(description, price);


ArrayList<Product> products = new ArrayList<Product>();

products.add(p);

1 Comment

Glad I could be of help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.