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

  • Home
  • SEARCH
  • 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 8646139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:43:34+00:00 2026-06-12T12:43:34+00:00

i have a JTable that displays a list of customer objects that are kept

  • 0

i have a JTable that displays a list of customer objects that are kept in an ArrayList collection.
From this JTable, i want to be able to highlight a specific customer, then click a “delete customer” button, which extracts the ID column value from the selected table row and uses it to search through the ArrayList with an Iterator, find the matching customer, and delete it from the collection.
My expertise with swing components and eventlisteners leave a lot to be desired, ive tried about 5 different combinations so far and im getting nowhere. How do i get this done? The relevant parts of my code so far are:

class CustomerFrame extends JFrame{
    public CustomerFrame(travelagent agent){

        CustomerPanel cp = new CustomerPanel(agent, this);
        setSize(800,500);
        setLocationRelativeTo(null);
        setTitle("Actions for customer");


        add(cp);

    }
}

class CustomerPanel extends JPanel implements ActionListener, ListSelectionListener{
    private travelagent agent;
    private JButton addCust;
    private JButton deleteCust;
    private JButton editCust;
    private JButton bookFlight;
    private JButton exit;
    private CustomerFrame frame;
    private Object selectedID;
    private JTable ref;

    public CustomerPanel(travelagent agent, CustomerFrame frame){
        ListCustPanel lp = new ListCustPanel(agent);
        setLayout(new GridLayout(2,1,5,5));

        ref = lp.getTable();
        ListSelectionModel sel = ref.getSelectionModel();
        sel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        sel.addListSelectionListener(this);

        this.agent = agent;
        this.frame = frame;
        JLabel options = new JLabel("Options:");
        addCust = new JButton("Add customer");
        deleteCust = new JButton("Delete customer");
        editCust = new JButton("Edit Customer");
        bookFlight = new JButton("Book a flight");
        exit = new JButton("Back to main menu");

        options.setHorizontalAlignment(SwingConstants.CENTER);
        JPanel b = new JPanel(new GridLayout(3,2,5,5));

        b.add(options);
        b.add(addCust);
        b.add(deleteCust);
        b.add(editCust);
        b.add(bookFlight);
        b.add(exit);

        add(lp);
        add(b);

        addCust.addActionListener(this);
        deleteCust.addActionListener(this);
        editCust.addActionListener(this);
        bookFlight.addActionListener(this);
        exit.addActionListener(this);
    }


    public void valueChanged(ListSelectionEvent l){
        if(!l.getValueIsAdjusting()){
            int index = ref.getSelectedRow();
            selectedID = ref.getValueAt(index, 0);
        }
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource() == addCust){
            NewCustFrame cf = new NewCustFrame(agent, frame);
            cf.setVisible(true);
            cf.setDefaultCloseOperation(NewCustFrame.DISPOSE_ON_CLOSE);

        if(e.getSource() == deleteCust){
            DeleteCustFrame df = new DeleteCustFrame(agent, selectedID);
            df.setVisible(true);
            df.setDefaultCloseOperation(DeleteCustFrame.DISPOSE_ON_CLOSE);
        }
        if(e.getSource() == editCust){

        }
        if(e.getSource() == bookFlight){

        }
        if(e.getSource() == exit){
            frame.dispose();
        }

    }
    }



}





/*
 * this displays the list of customers:
 */
class ListCustPanel extends JPanel{

    private static final long serialVersionUID = 1L;
    private JTable table;
    public ListCustPanel(travelagent agent){

        String[] colNames = {"ID", "First Name", "Last Name"};

        int size = agent.getCust().size(); //get the size of the arraylist in order to define an object array to contain information in
        Object[][] data = new Object[size][3]; // create the array

        int i = 0;
        Iterator<customer> iter = agent.getCust().iterator(); //create an iterator to progress thought the arraylist
        while(iter.hasNext()){
            customer temp = iter.next(); //assign the current customer to a variable

            //extract its attributes and feed into object array
            data[i][0] = temp.getID(); 
            data[i][1] = temp.getFname();
            data[i++][2] = temp.getLname();

        }
        setLayout(new GridLayout(1,1,5,5));


        // convert the object array into a JTable
        table = new JTable(data, colNames);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // create a sorter to sort table rows
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
        table.setRowSorter(sorter);
        ArrayList<SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
        sortKeys.add(new RowSorter.SortKey(2, SortOrder.ASCENDING));
        sorter.setSortKeys(sortKeys);


        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        add(scrollPane);


    }

    public JTable getTable(){
        return table;
    }
}


/*
 * delete the customer using this
 */
class DeleteCustFrame extends JFrame{
    public DeleteCustFrame(travelagent agent, Object ID){
        DeleteCustPanel dcp = new DeleteCustPanel(agent, this, ID);
        add(dcp);

        setSize(300,200);
        setTitle("Delete Customer");
        setLocationRelativeTo(null);

    }

}

class DeleteCustPanel extends JPanel implements ActionListener{
    private travelagent agent;
    private JButton delete;
    private JButton cancel;
    private JTextField id;
    private DeleteCustFrame frame;
    public int ID;

    public DeleteCustPanel(travelagent agent, DeleteCustFrame frame, Object ID){
        this.ID = (int)ID;
        this.agent = agent;
        setLayout(new GridLayout(3,1,5,5));

        String fname = null;
        String lname = null;
        boolean hasFlight = false;
        Iterator<customer> iter = agent.getCust().iterator();
        while(iter.hasNext()){
            customer temp = iter.next();
            if(temp.getID() == this.ID){
                 fname = temp.getFname();
                 lname = temp.getLname();
            }
            if(temp.getNumFlight() > 0){
                hasFlight = true;
            }
        }

        JLabel desc = new JLabel("Customer: " + ID + " " + fname + " " + lname);;
        desc.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel yesFlight;
        if(hasFlight){
             yesFlight = new JLabel("This customer has flights booked!");
        }else{
            yesFlight = new JLabel("No flights are booked for this customer");
        }

        yesFlight.setHorizontalAlignment(SwingConstants.CENTER);

        JPanel buttonPanel = new JPanel();

        buttonPanel.setLayout(new GridLayout(1,2,5,5));
        buttonPanel.add(delete);
        buttonPanel.add(cancel);

        add(desc);
        add(yesFlight);
        add(buttonPanel);

        delete.addActionListener(this);
        cancel.addActionListener(this);

    }


    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == delete){
            Iterator<customer> iter = agent.getCust().iterator();
            while(iter.hasNext()){
                customer temp = iter.next();
                if(temp.getID() == ID){
                    agent.deleteCust(temp);
                }
            }
            JOptionPane.showMessageDialog(null, "Customer deleted!", null, JOptionPane.PLAIN_MESSAGE);
            frame.dispose();
        }
        if(e.getSource() == cancel){
            frame.dispose();
        }

    }
}

so far with this when i click delete customer with a customer highlighted, no action is taken. long ass question i know, but if it helps, i suspect my problem is occurring around where im trying to use a ListSelectionListener.

  • 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-12T12:43:36+00:00Added an answer on June 12, 2026 at 12:43 pm

    JTable offers a DefaultTableModel that makes this much simpler. Use it instead of the ArrayList.

    The methods include removeRow(int) where the int (row) is from JTable.getSelectedRow().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTable that I want to use to display some data (a
I have a JTable displays the event accordingly, I want to do like when
I have a JTable that loads data from a database. Because sometimes there's too
I have Java Desktop application that displays some information in a JTable that contains
I want to display a JTable that display the data from a DataBase table
I have a standalone Java application that gets data from a database and displays
I have Java Desktop application that displays some information in a JTable that may
I have a ScrollPane and JTable that should only have one row of data.
I have a JTable with few columns that are painted as checkboxes. What I
The problem I have is that when more rows are added to JTable (jtbls)

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.