Results 1 to 11 of 11
  1. #1
    droidtech1 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default remove *this item/object from arrayList and window(jframe) via item button

    /*
    screenshot of running app:
    View image: itemlist
    Currently I can add items to the array list and window.
    PROBLEM: How to make each item's delete button, remove that item from the array list and also the window it was added to?
    */ update:
    As you can see in the screenshot I have a window displaying an "add" button, and a list of items. When the program first starts. there are no items, just the "add" button in the window(also the volume total textfield). Every time this button is pressed, a new Item, object of Item class is created.. this Item is actually a JPanel containing a text field and a button. The text field is for editing the value of the specific item, since this is an Inventory, this value is called volume, think physical size. There is also a total-volume textfield at the top of the outer window -in the future this should show the total of all the added item's set volume values.... this is a step ahead though I have a more immediate problem... I want each added item's "delete" button to completley remove that item(the jpanel containing the volume-textfield, and the delete button)... here is my problem... I added the items to an ArrayList, simply by when clicking the outer window's main add button -a new Item() is created then sent to the next available ArrayList position -this is then added to the outer window/jframe with
    window.add(itemList.get(itemList.size()-1));

    But now that items have been added to window.... they are cut off from the ArrayList index position... I am also stll trying to figure out what to put in the Item class's actionListener to make it remove the current item from the ArrayList anyway.

    I hope this is more clear now. Any tips are appreciated.

    Java Code:
        package com.jlab.inventory;
        import java.awt.Color;
        import javax.swing.*;
        import java.awt.*;
        import java.util.ArrayList;
        import java.awt.event.*;
         
        public class Inventory implements ActionListener {
                JTextField volumeTotal = new JTextField("Volume Total Value"); // count total item Volume
                JFrame window = new JFrame(); // new items to be added during run
                JButton newItemButton = new JButton("Add new item");
         
            public static ArrayList<Item> itemList = new ArrayList<Item>();
         
                public static void main(String args[]) {
                    Inventory store = new Inventory();
                    store.runStore();
                }
               
                public Inventory() { // constructor initializes program's main interface and data
                        newItemButton.addActionListener(this);
                        window.setPreferredSize(new Dimension(460, 700));
                        window.setLayout(new FlowLayout());
                        window.add(volumeTotal);
                        window.add(newItemButton);
                        window.pack();
                        window.setVisible(true);
                }
         
                public void runStore() {
                        System.out.println("running tool");
                }
         
                public void actionPerformed(ActionEvent e) {
                    System.out.println("adding new item");
                    itemList.add(new Item());
                    System.out.println(itemList.size());
                    window.add(itemList.get(itemList.size()-1));
                    window.revalidate();
                }
        }
         
         
        package com.jlab.inventory;
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
         
        public class Item extends JPanel implements ActionListener {
            private static final long serialVersionUID = 1L;
            JTextField volume = new JTextField("#vol");
            JButton deleteItem = new JButton("-del");
         
                public Item(){
                    deleteItem.addActionListener(this);
                    this.setBackground(Color.gray);
                    this.setPreferredSize(new Dimension(400, 50));
                    this.add(volume);
                    this.add(deleteItem);
         
                }
                public void actionPerformed(ActionEvent e) {
                    System.out.println("item action");
                    Inventory.itemList.remove(this);
         
                }
        }
    Thanks for the response. I have added the code tags.
    I have added more info at the top of the post about what I am trying to do.
    Last edited by droidtech1; 09-03-2012 at 06:26 PM. Reason: added code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    You've given us a lot of code to review but have not taken the time or put in the effort to describe your problem, please do so now, enough detail so that we can more easily understand all this code and help you solve your issue. Also consider editing your post and adding [code] [/code] tags.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    The code does not compile for me without errors.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    droidtech1 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    I can run and compile it, I just copied the code from here to my files and compiled them.. I am sure it compiles and runs... at least on my system

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    I suggest that you do a few things:

    • itemList should not be static as that goes against OOPs principles for no benefit. Make it an instance variable.
    • One way to solve your problem is to give Inventory a removeItem(Item item) method that both removes the Item passed in from the ArrayList and the GUI.
    • Have Item call this method in its JButton's ActionListener.
    • To do this, Item will need a valid reference to the Inventory object, and you give one to Item by giving it an Iventory field, inventory, and then passing a reference to Inventeory in Item's constructor. Inventory will call the constructor while passing this into it.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    I can run and compile it,
    I do it the other way: compile and run.
    Are you using an IDE that ignores compiler errors and executes the code even if there are compiler errors?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    droidtech1 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    Thanks Fubarable I will try those things...I'm sure it will all become clear to me after a while. I am trying to find how to get the index of an item added to the window too. Do you know of any tutorials which go over those techniques or have other similar examples?

    Yes Norm, I compiled and ran it.. I am using emacs and my build script which uses javac and java commands:

    #buildxx
    echo "compiling project"
    javac -d ./ ./src/com/jlab/inventory/*.java
    #test
    echo "running project"
    java com.jlab.inventory.Inventory
    Last edited by droidtech1; 09-03-2012 at 06:57 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    Is this line in the code you are compiling:
    Java Code:
       window.revalidate();
    revalidate() is not a method in JFrame.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    Quote Originally Posted by Norm View Post
    revalidate() is not a method in JFrame.
    Actually it is now as it was added as a method of Component with the current 1.7 version of Java.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    Whoops. Time to upgrade. Just don't like the format of the 1.7 API docs.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    droidtech1 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Re: remove *this item/object from arrayList and window(jframe) via item button

    All the item buttons work perfectly now. The matter is solved then, unless anyone can see any obvious things I could be doing better, always trying to learn and improve more.

    Here is the final code:

    Java Code:
    package com.jlab.inventory;
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.*;
    import java.util.ArrayList;
    import java.awt.event.*;
    
    public class Inventory implements ActionListener {
        JTextField volumeTotal = new JTextField("Volume Total Value"); // total item Volume 
        JFrame window = new JFrame(); // new items to be added during run
        JButton newItemButton = new JButton("Add new item");
        public ArrayList<Item> itemList = new ArrayList<Item>(); // not static
    
        public static void main(String args[]) {
    	Inventory store = new Inventory();
    	store.runStore();
        }
        public Inventory() { // constructor initializes program's main interface and data
    	newItemButton.addActionListener(this);
    	window.setPreferredSize(new Dimension(460, 700));
    	window.setLayout(new FlowLayout());
    	window.add(volumeTotal);
    	window.add(newItemButton);
    	window.pack();
    	window.setVisible(true);
        }
        public void runStore() {
    	System.out.println("revalidating");
    	window.revalidate();
    	window.repaint();
        }
        public void actionPerformed(ActionEvent e) {
    	System.out.println("adding new item");
    	itemList.add(new Item(this));
    	System.out.println(itemList.size());
    	window.add(itemList.get(itemList.size()-1));
    	runStore();
        }
        public void removeItem(Item item) { // -removes Item passed in: from ArrayList + GUI
    	itemList.remove(item);
    	window.remove(item);
    	runStore();
        }
        //addItem(Item i) {
        // add item to arraylist
        // add item to gui }
    }
    
    
    package com.jlab.inventory;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Item extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;
        JTextField volume = new JTextField("#vol");
        JButton deleteItem = new JButton("-del");
        Inventory inventory;
    
        public Item(Inventory inv) {
    	deleteItem.addActionListener(this);
    	this.setBackground(Color.gray);
    	this.setPreferredSize(new Dimension(400, 50));
    	this.add(volume);
    	this.add(deleteItem);
    	inventory = inv;
        }
        public void actionPerformed(ActionEvent e) {
    	System.out.println("item action");
    	inventory.removeItem(this);
        }
    }
    Last edited by droidtech1; 09-04-2012 at 11:11 AM.

Similar Threads

  1. Remove matching LinkedList item
    By wdh321 in forum New To Java
    Replies: 15
    Last Post: 04-20-2012, 10:56 PM
  2. Replies: 0
    Last Post: 01-31-2012, 12:51 PM
  3. Replies: 4
    Last Post: 12-16-2010, 03:47 PM
  4. Button to remove item from list
    By dacool25 in forum Java Applets
    Replies: 6
    Last Post: 10-14-2009, 04:30 AM
  5. Remove an item from listbox
    By Dieter in forum Advanced Java
    Replies: 9
    Last Post: 09-21-2009, 10:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •