package Assignment2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class AuctionDialog extends JFrame implements ActionListener {
    private JLabel lblOutput;
    private JTextField tfBidder;
    private JTextField tfPainting;
    private JTextField tfAmount;
    private JTextArea taOutput;
    private JButton btnSubmit;
    private Painting paintings;
    private Bid bids;
    private Auction artworkAuction;


    public AuctionDialog( )
    {

       artworkAuction = new Auction("");

       JLabel lblBidder = new JLabel("Bidder ID:");
        tfBidder= new JTextField(10);
       JLabel lblPainting = new JLabel("Painting ID:");
        tfPainting = new JTextField(10);
       JLabel lblAmount = new JLabel("Amount");
        tfAmount = new JTextField();
        taOutput = new JTextArea();
        taOutput.setEditable(false);
        taOutput.setColumns(20);
        taOutput.setRows(3);
        taOutput.setLineWrap(true);
       btnSubmit= new JButton ("Submit");
        btnSubmit.addActionListener(this);

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(5,2));
        p1.add(lblBidder);
        p1.add(tfBidder);
        p1.add(lblPainting);
        p1.add(tfPainting);
        p1.add(lblAmount);
        p1.add(tfAmount);
        p1.add(new Label()); //dummy
        p1.add(btnSubmit);
        //textArea
        JPanel p2 = new JPanel();
        p2.add(taOutput);

        Container c = getContentPane();
        c.add(p1, BorderLayout.NORTH);
        c.add(p2, BorderLayout.SOUTH);

       setTitle("Making a Bid");
       // pack to organize the size
       pack();
       setLocationRelativeTo(null);
       setVisible(true);
       setResizable(false);


    }
    //need help here 
     public void actionPerformed (ActionEvent e)
    {`Inline Code Example Here`
    //how to search existing object in an arrayList? 
       String input = tfBidder.getText();
            ArrayList<Bidder> bidders = artworkAuction.getBidders();
            for (Bidder b: bidders)
            { 
                if(b.getBidderId().equals(input))
                {
                    taOutput.append(input);
                }else
                {
                    taOutput.append("invalid bidder");
                }
            }

    }

}

Recommended Answers

All 9 Replies

can someone help me how to find existing object in an arraylist?
//need help here 
     public void actionPerformed (ActionEvent e)
    {`Inline Code Example Here`
    //how to search existing object in an arrayList? 
       String input = tfBidder.getText();
            ArrayList<Bidder> bidders = artworkAuction.getBidders();
            for (Bidder b: bidders)
            { 
                if(b.getBidderId().equals(input))
                {
                    taOutput.append(input);
                }else
                {
                    taOutput.append("invalid bidder");
                }
            }
    }

That code looks OK. What help do you need?

the taOutput of my AuctionDialog class does not show anything when I clicked the submit button
here is my addBidder() method:
 public void addBidder() {

         try
        {
        Scanner reader = new Scanner(new File("bidder.txt"));
        String s;
        while(reader.hasNextLine())
        {
            s = reader.nextLine();

            String[] w= s.split(",");

            try{
                bidders.add(new Bidder(w[0], w[1]));
                System.out.println("Bidder loaded");
            }catch(InvalidFile ex){
             System.out.println(ex.getMessage());   
            }catch(ArrayIndexOutOfBoundsException ex){
                System.out.println("Undable to process this line -"+s);
            }

        }
        }catch(FileNotFoundException ex)
        {
            System.out.println("File not found!");
        }

    }


    //my GUI class looks something like this:
    public void actionPerformed(ActionEvent s)
    {
        if(s.getSource() == mnuExit ){
           System.exit(0);
        }
        else if(s.getSource() == mnuLoad)
        {
            artworkAuction.addBidder();
            artworkAuction.addPainting();
            artworkAuction.addBid();
        JOptionPane.showMessageDialog(this, "Loading completed");

        }

Your method should add one line to the text area for each bidder in the array list, so you need to debug the actionPerformed by adding a number of simple print statements to display the values of the main variables as the method executes. You need to answer questions like "what is the value of input?", "what is in the bidders aray?"

Does OP needs to revalidate/repaint the textArea object because the OP builds the UI in AuctionDialog constructor but appends contents in actionPerformed()?

actionPerformed is called on the Swing thread, so the appends should be OK as they are.

he might have the same problem as one I tried to help with earlier: declared the textArea as an instance variable, but only added a local variable of type JTextArea with the same name to the actual JFrame.

it would be easier to see more of the code, to decide whether or not that might be the problem

The problem was already solved thanks a lot to those who helped...
here is the code:
package Assignment2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class AuctionDialog extends JFrame implements ActionListener {

    private JLabel lblOutput;
    private JTextField tfBidder;
    private JTextField tfPainting;
    private JTextField tfAmount;
    private JTextArea taOutput;
    private JButton btnSubmit;
    private Painting paintings;
    private Bid bids;
    private Auction artworkAuction;

    public AuctionDialog(Auction artworkAuction) {

        this.artworkAuction = artworkAuction;

        JLabel lblBidder = new JLabel("Bidder ID:");
        tfBidder = new JTextField(10);
        JLabel lblPainting = new JLabel("Painting ID:");
        tfPainting = new JTextField(10);
        JLabel lblAmount = new JLabel("Amount");
        tfAmount = new JTextField();
        taOutput = new JTextArea();
        taOutput.setEditable(false);
        taOutput.setColumns(20);
        taOutput.setRows(10);
        taOutput.setLineWrap(true);
        btnSubmit = new JButton("Submit");
        btnSubmit.addActionListener(this);

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(5, 2));
        p1.add(lblBidder);
        p1.add(tfBidder);
        p1.add(lblPainting);
        p1.add(tfPainting);
        p1.add(lblAmount);
        p1.add(tfAmount);
        p1.add(new Label()); //dummy
        p1.add(btnSubmit);
        //textArea
        JPanel p2 = new JPanel();
        JScrollPane scrollPane = new JScrollPane(taOutput);
        p2.add(scrollPane);


        Container c = getContentPane();
        c.add(p1, BorderLayout.NORTH);
        c.add(p2, BorderLayout.SOUTH);

        setTitle("Making a Bid");
        // pack to organize the size
        pack();
        setLocationRelativeTo(null);
        setVisible(true);


    }

    public void actionPerformed(ActionEvent e) {
        String bidderInput = tfBidder.getText();
        String paintingInput = tfPainting.getText();
        String bidInput = tfAmount.getText();
        Bidder b = artworkAuction.searchBidder(bidderInput);
        Painting p = artworkAuction.searchPainting(paintingInput);
        try{
            if (b == null || bidderInput == null) {
                taOutput.setText("");
                taOutput.append("Invalid bidder" + "\n");
            }else if (p == null || paintingInput == null) {
                taOutput.setText("");
                taOutput.append("Invalid painting" + "\n");
            }else if (p.getMinBidPrice() > Double.parseDouble(bidInput) || bidInput == null) {
                taOutput.setText("");
                taOutput.append("amount must be higher than the minimum bid price" + "\n");
            } 
            else {
                taOutput.setText("");
                artworkAuction.addBid(b, p, Double.parseDouble(bidInput));
                taOutput.setText("Bid added! bid ID is : " +artworkAuction.getBidId()+"\n" + "bidder name is: " + b.getBidderName() + "\n" + "painting name is: " + p.getPaintingName() + "\n");
            }

        } catch (InvalidFile ex) {
            taOutput.append(ex.getMessage());
        } catch (NullPointerException ex) {
            taOutput.append("the field cannot be empty" + "\n");
        } catch (NumberFormatException ex) {
            taOutput.append("the amount field cannot be empty" + "\n");
        }

    }

}
GUI class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame implements ActionListener{
    private JMenuItem mnuExit;
    private JMenuItem mnuLoad;
    private JMenuItem mnuSave;
    private JMenuItem mnuViewPaintings;
    private JMenuItem mnuViewBidders;
    private JMenuItem mnuAddBid;
    private JMenuItem mnuPreviewBids;
    private JMenuItem mnuHighestBid;
    private JMenuItem mnuAverageBid;
    private JMenuItem mnuBidderPaintings;
    private JMenuItem mnuAbout;
    private JTextArea taOutput;


    private Auction artworkAuction;
    private ArrayList<Bidder> bidders;

    public Test()
    { 
      artworkAuction = new Auction();



        JMenuBar mainMenu = new JMenuBar();

        //create file
        JMenu mnuFile = new JMenu("File");
        //create menu Item
        mnuLoad = new JMenuItem("Load");
        mnuSave = new JMenuItem("Save");
        mnuExit = new JMenuItem("Exit");
        mnuFile.add(mnuLoad);
        mnuFile.add(mnuSave);

        mnuExit.addActionListener(this);
        mnuLoad.addActionListener(this);
        mnuFile.add(mnuExit);

        //***********
        JMenu mnuView = new JMenu("View");

        mnuViewPaintings = new JMenuItem("View Paintings");
        mnuViewPaintings.addActionListener(this);
        mnuView.add(mnuViewPaintings);

        mnuViewBidders = new JMenuItem("View Bidders");
        mnuViewBidders.addActionListener(this);
        mnuView.add(mnuViewBidders);
        //*************
        JMenu mnuBids = new JMenu("Bids");

        mnuAddBid = new JMenuItem("Add Bid");
        mnuAddBid.addActionListener(this);
        mnuBids.add(mnuAddBid);

        mnuPreviewBids = new JMenuItem ("Preview Bids");
        mnuPreviewBids.addActionListener(this);
        mnuBids.add(mnuPreviewBids);

        mnuHighestBid = new JMenuItem ("Highest Bid");
        mnuHighestBid.addActionListener(this);
        mnuBids.add(mnuHighestBid);

        mnuAverageBid = new JMenuItem ("Average Bid");
        mnuAverageBid.addActionListener(this);
        mnuBids.add(mnuAverageBid);

        mnuBidderPaintings = new JMenuItem ("Bidder Paintings");
        mnuBidderPaintings.addActionListener(this);
        mnuBids.add(mnuBidderPaintings);
        //*****************
        JMenu mnuHelp = new JMenu("Help");

        mnuAbout = new JMenuItem ("About");
        mnuAbout.addActionListener(this);
        mnuHelp.add(mnuAbout);


        //*********************
        taOutput = new JTextArea(10,10);
        taOutput.setFont(new Font("monospaced", Font.PLAIN, 12));
        taOutput.setEditable(false); //means cannot edit
        JScrollPane scrollPane = new JScrollPane(taOutput);
        Container c = getContentPane();
        c.setLayout(new BorderLayout());
        c.add(scrollPane, BorderLayout.SOUTH);

        //****************************

        //add menu file to the main menu
        mainMenu.add(mnuFile);
        mainMenu.add(mnuView);
        mainMenu.add(mnuBids);
        mainMenu.add(mnuHelp);
        //set the menu bar
        this.setJMenuBar(mainMenu);

        setTitle("Bidding Application");
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // so that dialog will appear on the center
        setVisible(true);



    }

    public void actionPerformed(ActionEvent s)
    {
        if(s.getSource() == mnuExit ){
           System.exit(0);
        }
        else if(s.getSource() == mnuLoad)
        {
            artworkAuction.loadBidder();
            artworkAuction.loadPainting();
            artworkAuction.loadBid();
        JOptionPane.showMessageDialog(this, "Loading completed");

        }
        else if(s.getSource() == mnuViewBidders)
        { 
           taOutput.setText("");
           taOutput.setColumns(2);
           taOutput.append("ID" + "\t" + "Name" + "\n");
           ArrayList<Bidder> bidders = artworkAuction.getBidders();
           for(Bidder b : bidders)
           taOutput.append(b.toString());


        }

        else if(s.getSource() == mnuViewPaintings)
        {
            taOutput.setText("");
            taOutput.append("id" + "\t" + "\t" + "Name" + "\t" +"\t" + "\t" + "\t" + "Artist" + "\t" + "\t" + "\t" + "min Bid Price" + "\n");


            ArrayList<Painting> paintings = artworkAuction.getPaintings();
            for(Painting p: paintings)
            taOutput.append(p.toString());

        }
        else if(s.getSource() == mnuAddBid)
        {
            new AuctionDialog(artworkAuction);
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.