Results 1 to 11 of 11
- 09-03-2012, 05:58 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
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.
Thanks for the response. I have added the code tags.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); } }
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
-
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.
- 09-03-2012, 06:05 PM #3
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.
- 09-03-2012, 06:24 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
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
-
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.
- 09-03-2012, 06:48 PM #6
Re: remove *this item/object from arrayList and window(jframe) via item button
I do it the other way: compile and run.I can run and compile it,
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.
- 09-03-2012, 06:54 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
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.InventoryLast edited by droidtech1; 09-03-2012 at 06:57 PM.
- 09-03-2012, 07:31 PM #8
Re: remove *this item/object from arrayList and window(jframe) via item button
Is this line in the code you are compiling:
revalidate() is not a method in JFrame.Java Code:window.revalidate();
If you don't understand my response, don't ignore it, ask a question.
-
Re: remove *this item/object from arrayList and window(jframe) via item button
Actually it is now as it was added as a method of Component with the current 1.7 version of Java.
- 09-03-2012, 07:44 PM #10
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.
- 09-04-2012, 08:12 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 6
- Rep Power
- 0
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
-
Remove matching LinkedList item
By wdh321 in forum New To JavaReplies: 15Last Post: 04-20-2012, 10:56 PM -
Remove Item from PST File & Access to Default MS Outlook Folders
By sherazam in forum Java SoftwareReplies: 0Last Post: 01-31-2012, 12:51 PM -
select item from combobox and continue to new window
By brunjoo in forum AWT / SwingReplies: 4Last Post: 12-16-2010, 03:47 PM -
Button to remove item from list
By dacool25 in forum Java AppletsReplies: 6Last Post: 10-14-2009, 04:30 AM -
Remove an item from listbox
By Dieter in forum Advanced JavaReplies: 9Last Post: 09-21-2009, 10:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks