简体   繁体   中英

How to pass an object argument to a method called in actionPerformed?

I am writing a stock control system program for a school project. This is the last thing I need to do, but seeing as I am a relative Java noob, I kindly request your assistance.

I have a DisplayRecord class, which is created by taking String input from a "search" JTextField in the Search class, finding the Object (Product p) it's linked to, and passing it to the displayRecord method. This part works perfectly.

I want to take that Product p and pass it to the EditProduct class or the DeleteRecord class (depending on the JButton pressed) so the user can then edit the Name, Quantity or Cost of that same Product. Here are my DisplayRecord, EditProduct and DeleteRecord classes. I have no idea what to do.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

public class DisplayRecord extends JFrame implements ActionListener {

    final private StockList stocks;
    final private ArrayList<Product> list;
    JFrame showWindow;

    private JPanel top, bot;
    private JPanel barcodePanel1 = new JPanel();
    private JPanel barcodePanel2 = new JPanel();
    private JPanel namePanel1 = new JPanel();
    private JPanel namePanel2 = new JPanel();
    private JPanel descPanel1 = new JPanel();
    private JPanel descPanel2 = new JPanel();
    private JPanel compPanel1 = new JPanel();
    private JPanel compPanel2 = new JPanel();
    private JPanel ratingPanel1 = new JPanel();
    private JPanel ratingPanel2 = new JPanel();
    private JPanel costPanel1 = new JPanel();
    private JPanel costPanel2 = new JPanel();
    private JPanel quantityPanel1 = new JPanel();
    private JPanel quantityPanel2 = new JPanel();
    private JLabel barcodeLabel = new JLabel();
    private JLabel nameLabel = new JLabel();
    private JLabel descLabel = new JLabel();
    private JLabel compLabel = new JLabel();
    private JLabel ratingLabel = new JLabel();
    private JLabel costLabel = new JLabel();
    private JLabel quantityLabel = new JLabel();
    private GridLayout displayLayout;
    JButton edit = new JButton("Edit");
    JButton backToMenu = new JButton("Back to Menu");
    JButton delete = new JButton("Delete");

    public DisplayRecord() {
        stocks = new StockList();
        list = stocks.getList();
        try {
            stocks.load();
        } catch (IOException ex) {
            System.out.println("Cannot load file");
        }
    }

    public void displayRecord(Product p) {
        this.setTitle("Displaying one record");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(500, 350));

        top = new JPanel();
        displayLayout = new GridLayout(7, 2, 2, 2);
        top.setLayout(displayLayout);
        top.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 5));

        bot = new JPanel();
        bot.setLayout(new BoxLayout(bot, BoxLayout.LINE_AXIS));
        bot.add(Box.createHorizontalGlue());
        bot.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));

        barcodeLabel.setText("Barcode:  ");
        nameLabel.setText("Name:  ");
        descLabel.setText("Description:  ");
        compLabel.setText("Developer:  ");
        ratingLabel.setText("EU Rating:  ");
        costLabel.setText("Cost:  ");
        quantityLabel.setText("Quantity in Stock:  ");

        JLabel barcodeField = new JLabel(Long.toString(p.getBarcode()));
        JLabel nameField = new JLabel(p.getName());
        JLabel descField = new JLabel(p.getDesc());
        JLabel compField = new JLabel(p.getCompany());
        JLabel ratingField = new JLabel(p.getRating());
        JLabel costField = new JLabel(Double.toString(p.getCost()));
        JLabel quantityField = new JLabel(Integer.toString(p.getQuantity()));

        barcodePanel1.add(barcodeLabel);
        barcodePanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        barcodePanel2.add(barcodeField);                      barcodePanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        namePanel1.add(nameLabel);
        namePanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        namePanel2.add(nameField);
        namePanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        descPanel1.add(descLabel);
        descPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        descPanel2.add(descField);
        descPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        compPanel1.add(compLabel);
        compPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        compPanel2.add(compField);
        compPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        ratingPanel1.add(ratingLabel);
        ratingPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        ratingPanel2.add(ratingField);
        ratingPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        costPanel1.add(costLabel);
        costPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        costPanel2.add(costField);
        costPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
        quantityPanel1.add(quantityLabel);
        quantityPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        quantityPanel2.add(quantityField);
        quantityPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        top.add(barcodePanel1);
        top.add(barcodePanel2);
        top.add(namePanel1);
        top.add(namePanel2);
        top.add(descPanel1);
        top.add(descPanel2);
        top.add(compPanel1);
        top.add(compPanel2);
        top.add(ratingPanel1);
        top.add(ratingPanel2);
        top.add(costPanel1);
        top.add(costPanel2);
        top.add(quantityPanel1);
        top.add(quantityPanel2);

        edit.addActionListener(this);
        delete.addActionListener(this);
        backToMenu.addActionListener(this);

        bot.add(edit);
        bot.add(Box.createRigidArea(new Dimension(10, 0)));
        bot.add(delete);
        bot.add(Box.createRigidArea(new Dimension(10, 0)));
        bot.add(backToMenu);

        this.add(top);
        this.add(bot, BorderLayout.SOUTH);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) { // here is where I'd LIKE to pass Product p as parameter but obviously that's not a thing
        if (e.getSource() == edit) {
//            EditProduct ed = new EditProduct(); <- hypothetical!
//            ed.editProduct(p);
        } else if (e.getSource() == delete) {
//            DeleteRecord del = new DeleteRecord(); <- hypothetical!
//            del.deleteRecord(p);
        } else if (e.getSource() == backToMenu) {
            new CreateDisplay();
            this.dispose();
        }
    }
}

My EditProduct class:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EditProduct extends JFrame implements FocusListener, ActionListener {

    final private StockList stocks;
    final private ArrayList<Product> list;
    JPanel top, bot;
    JLabel nameLabel, costLabel, quantityLabel = new JLabel();
    JTextField nameField, costField, quantityField = new JTextField();
    JButton save, quit = new JButton();
    private GridLayout topLayout;

    public EditProduct() {
        stocks = new StockList();
        list = stocks.getList();
    }

    public void editProduct(Product p) {
        this.setTitle("Editing a Product");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(500, 250));

        top = new JPanel();
        topLayout = new GridLayout(3, 2, 5, 5);
        top.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 5));
        top.setLayout(topLayout);

        bot = new JPanel();
        bot.setLayout(new BoxLayout(bot, BoxLayout.LINE_AXIS));
        bot.add(Box.createHorizontalGlue());
        bot.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));

        nameLabel.setText("Name:  ");
        costLabel.setText("Cost:  ");
        quantityLabel.setText("Quantity:  ");
        top.add(nameLabel);
        top.add(costLabel);
        top.add(quantityLabel);

        nameField = new JTextField(p.getName());
        costField = new JTextField(String.valueOf(p.getCost()));
        quantityField = new JTextField(p.getQuantity()); 

        nameField.addFocusListener(this);
        costField.addFocusListener(this);
        quantityField.addFocusListener(this);

        save.setText("Save");
        save.addActionListener(this);
        quit.setText("Quit");
        quit.addActionListener(this);

        bot.add(save);
        bot.add(Box.createRigidArea(new Dimension(10, 0)));
        bot.add(quit);

        this.add(top);
        this.add(bot, BorderLayout.SOUTH);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    @Override
    public void focusGained(FocusEvent e) {
        if (e.getSource() == nameField) {
            nameField.setText("");
        } else if (e.getSource() == costField) {
            costField.setText("");
        } else if (e.getSource() == quantityField) {
            quantityField.setText("");
        }
    }

    @Override
    public void focusLost(FocusEvent fe) {
        //do nothing
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == save) {
            String newName = nameField.getText();
            double newCost = Double.parseDouble(costField.getText());
            int newQty = Integer.parseInt(quantityField.getText());
            stocks.editProduct(newName, newCost, newQty);
            this.dispose();
            JOptionPane.showMessageDialog(null, "Changes have been saved!", "Saved!", JOptionPane.PLAIN_MESSAGE);
        } else if (e.getSource() == quit) {

        }
    }
}

Aaand the DeleteRecord class:

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class DeleteRecord {

    private StockList stocks;
    private ArrayList<Product> list;

    public DeleteRecord() {
        stocks = new StockList();
        list = stocks.getList();
    }

    public DeleteRecord(Product p) {
        String title = "Are you sure you want to delete " + p.getName() + "?";
        if (JOptionPane.showConfirmDialog(null, title, "Deleting...", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            stocks.deleteRecord(p);
        } else {
            new CreateDisplay();
        }
    }
}

I'm sorry for the massive post and text wall but I honestly have no idea where my problem is or how to work around this problem (and I'm also new to StackOverflow). Can anyone help me?

It seems to me that DisplayRecord can only display one Product at a time. If that is indeed the case, you can store that Product in a field and then access it from actionPerfomed() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM