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.
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.
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.
Re: remove *this item/object from arrayList and window(jframe) via item button
The code does not compile for me without errors.
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.
Re: remove *this item/object from arrayList and window(jframe) via item button
Quote:
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?
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
Re: remove *this item/object from arrayList and window(jframe) via item button
Is this line in the code you are compiling:
Code:
window.revalidate();
revalidate() is not a method in JFrame.
Re: remove *this item/object from arrayList and window(jframe) via item button
Quote:
Originally Posted by
Norm
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.
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.
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:
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);
}
}