Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:59:12+00:00 2026-06-10T17:59:12+00:00

I have updated this from the original post, now I can add/remove items but

  • 0

enter image description here

I have updated this from the original post, now I can add/remove items but the last item left gets stuck … it seems to be to do with the button states… the last item’s “del” button will be grayed out… also the “add new item” button seems to sometimes affect this when clicking. Also if you have any other observations about how my probably crude code could be improved….

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);

    }
}

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 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();
    }
    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
    //}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:59:13+00:00Added an answer on June 10, 2026 at 5:59 pm

    When modifying the contents of a visible container, you have to validate and repaint it. So I modified your removeItem() method like below and it works now.

    public void removeItem(Item item) { // -removes Item passed in: from ArrayList + GUI
        itemList.remove(item);
        window.remove(item);
        window.validate();
        window.repaint();
        runStore();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Perhaps this wasn't clear from my original post, but I'm mainly interested in
I have this exact issue ASP.net can’t update page from event handler and it's
I have updated my symfony version to 2.0.12 version. But I have this error
-- This is an updated post, old post removed -- Suppose I have data
Let's assume we have these models, original project differs but this would be the
I have this code to update my SQL database from data in a textbox,
I little while back I posted this question . I have updated that question
UPDATED: I have some table with sync_numbers, like this: --------------------- id | sync_number ---------------------
I have a table of frequently updated information. This is presented using a container
I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.