0

Here's the problem: I want to check all of my customer list. Hence i will use a hash map and output their ic num and their name. The output runs fine if I tried to use the terminal.

But now I want to output the result of those hash map, into a GUI

Here's my coding so far. You may skip right to action event for viewNumCust. That's where i tried to output the haspmap into a jtextarea.

import javax.swing.*;
import java.io.*;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;

public class manageClientAccGui extends JFrame
{
ImageIcon image;
JLabel imgLabel,clientLabel,helloLabel,greetingLabel;
JButton createCustProfileButt,editClientAccButt,deleteClientButt,backClientButt,viewNumCust;
JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7;
JTextArea viewListCust;

public manageClientAccGui()
{

    super("Team Garoking Manage Customer");

    Container con = getContentPane();
    con.setLayout(new BorderLayout());

    image = new ImageIcon("images/garokingLogo.jpg");
    imgLabel = new JLabel(image);

    clientLabel = new JLabel("Manage Client Account");
    clientLabel.setForeground(Color.WHITE);

    helloLabel = new JLabel("Hello! ");
    helloLabel.setForeground(Color.WHITE);

    greetingLabel = new JLabel("What would you like to do? ");
    greetingLabel.setForeground(Color.WHITE);

    JScrollPane scrollPane = new JScrollPane(viewListCust,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setPreferredSize(new Dimension(100, 100));

    JLabel empty = new JLabel("");
    JLabel empty1 = new JLabel("");
    JLabel empty2 = new JLabel("");
    JLabel empty3 = new JLabel("");
    JLabel empty4 = new JLabel("");

    createCustProfileButt = new JButton("Create Customer");
    editClientAccButt = new JButton("Edit Customer Details");
    deleteClientButt = new JButton("Remove Customer");
    viewNumCust = new JButton("View Number of Customers");
    backClientButt = new JButton("Back");
    viewListCust = new JTextArea();
    viewListCust.setEditable(false);

    panel1 = new JPanel();
    panel2 = new JPanel(new GridLayout(12,3));
    panel3 = new JPanel(new GridLayout(12,3,0,3));
    panel4 = new JPanel();
    panel5 = new JPanel();

    panel6 = new JPanel(new GridLayout(1,2));
    panel7 = new JPanel(new GridLayout(1,2));

    panel1.add(imgLabel);
    panel1.setBackground(new Color(90,18,18));

    panel2.add(clientLabel);
    panel2.add(empty);
    panel2.add(createCustProfileButt);
    panel2.add(empty1);
    panel2.add(editClientAccButt);
    panel2.add(empty2);
    panel2.add(deleteClientButt);
    panel2.add(empty3);
    panel2.add(viewNumCust);    
    panel2.add(empty4);
    panel2.add(backClientButt);
    panel2.setBackground(new Color(90,18,18));

    panel6.add(helloLabel);
    panel6.setBackground(new Color(90,18,18));

    panel7.add(greetingLabel);
    panel7.setBackground(new Color(90,18,18));

    panel3.add(panel6);
    panel3.add(panel7);
    panel3.setBackground(new Color(90,18,18));
    panel3.add(scrollPane);

    panel5.add(panel3);
    panel5.setBackground(new Color(90,18,18));

    panel4.add(panel2);
    panel4.setBackground(new Color(90,18,18));

    con.add(panel4, BorderLayout.WEST);
    con.add(panel1, BorderLayout.NORTH);
    con.add(panel5, BorderLayout.CENTER);

    Action act = new Action();

    createCustProfileButt.addActionListener(act);
    editClientAccButt.addActionListener(act);
    deleteClientButt.addActionListener(act);
    backClientButt.addActionListener(act);
    viewNumCust.addActionListener(act);

    setVisible(true);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(((dim.width-1024)/2), ((dim.height-650)/2));
    setSize(1024,650);

}

//  public static void main(String args[])
//  {
//      new manageClientAccGui();
//  }

class Action implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {   
            if(e.getSource() == createCustProfileButt)
            {
                dispose();
                new Log("Accessing create customer");
                new addCustGui();
            }

            if(e.getSource() == editClientAccButt)
            {
                dispose();
                new Log("Accessing edit customer");
                new editCustGui();
            }

            if(e.getSource() == deleteClientButt)
            {
                dispose();
                new Log("Accessing remove customer");
                new removeCustGui();
            }

            if(e.getSource() == backClientButt)
            {
                dispose();
                new Log("Accessing main menu");
                new menuGarokingGui();
            }

            if(e.getSource() == viewNumCust)
            {
                int enumer = 0;
                HashMap<String, String> map = new HashMap<String, String>();

                try
                {
                    Scanner scanner = new Scanner(new FileReader("IndividualDetails.txt"));

                    while (scanner.hasNextLine()) {
                        String[] columns = scanner.nextLine().split("\t");
                        map.put(columns[1,], columns[2])
                        enumer++;
                    }
                }catch (FileNotFoundException ex)
                    {
                    }
                    new Log("View latest");
                    viewListCust.append("The clients are : " + map.values());
                    viewListCust.append("The total number of clients are : " + enumer);         
            }
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 3
    1) Change `}catch (FileNotFoundException ex) { }` to `}catch (FileNotFoundException ex) { ex.printStackTrace(); }` and report the output. 2) Potentially long running tasks such as file I/O should not be done in the EDT. Instead use a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Nov 29 '12 at 03:27
  • 2
    Also consider `JTable`, shown [here](http://stackoverflow.com/a/9134371/230513). – trashgod Nov 29 '12 at 03:30
  • thank you andrew and trashgod. I am currently learning about printstacktrace (im fairly new to GUI). And for the record, my list of customers wont grow that much but i know what u mean. Currently looking at swing worker – Salehin Suhaimi Nov 29 '12 at 04:04
  • @SalehinSuhaimi Make sure you mark an answer as accepted if one solved your issue. :) – ardavis Nov 29 '12 at 05:14

2 Answers2

5

Map#values returns a Collection of objects.

Calling viewListCust.append("The clients are : " + map.values()) is simply going to call the Collection#toString method of the returned collection, which isn't likely to provide a pretty view of the contents.

Instead, you're going to have to iterate the collection yourself.

viewListCust.append("The clients are :");
for (String value : map.values()) {
    viewListCust.append("\n" + value);
}

Now remember, while you're reading the file and append the content, you are blocking the Event Dispatching Thread, this will make your program look as if it's hung.

You might like to take advantage of a SwingWorker to load the file in the background before updating the UI. Check out Concurrency in Swing

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

You need to instantiate viewListCust before passing it to the JScrollPane like so:

viewListCust = new JTextArea();
viewListCust.setEditable(false);

JScrollPane scrollPane = new JScrollPane(viewListCust,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(100, 100));

viewListCust is currently null when you pass it into the JScrollPane constructor.

Ryan
  • 672
  • 8
  • 15
  • i did instantiate it, the coding is just messed up :) or did i miss something? – Salehin Suhaimi Nov 29 '12 at 04:06
  • You instantiated it AFTER you created the JScrollPane. You need to do it before. In other words, just copy your viewListCust code and paste it before where you create the scrollPane (in the order I have shown). – Ryan Nov 29 '12 at 04:09
  • Ah i forgot it must be in order. Thank you very much – Salehin Suhaimi Nov 29 '12 at 04:44