Search code examples
javaswinghashmapjbuttonjtextfield

java swing addactionlistener JButton


I have a JPanel where a JTextFiled exists. I want to show the information belongs to a hashmap collection in this JTextFiled.

My hashmap collection is: HashMap<String,Job>jobs = new HashMap < String,Job>();

My method in the other class (Branch) has the method for getting all the Jobs:

    public String getAllJobs()
 {       
        String result_jobs;
        result_jobs = " ";

        Collection<Job> jobValues = jobs.values();
        Iterator<Job> Jobiter =  jobValues.iterator();

       while(Jobiter .hasNext())
       {
       Job jo = Jobiter.next();
       result_jobs += jo.toString()+ '\n' ; 
       }

       return result_jobs; 

    }

In this JTextFiled, it should be entered key value of hashmap indicating Customer Name which is declared as String in the HashMap collection shown above. When it is pressed Add Job JButton, the information belongs to the hashmap collection is listed in the JTextFiled.

The figures are below;

enter image description here

enter image description here

I have tried to write down the method of actionPerformed(ActionEvent e).

Since I am very new in Java, I have difficulty to write down this method.

 private class AddJobButtonHandler implements ActionListener{

          public void actionPerformed(ActionEvent e) { }
      } 

EDITED: if there was a menu like the one below and by selecting the "Add Job" menu item; How would the codes change?

enter image description here

I would appreciate if you suggest/recommend any examples, methodoligies or anything. Thanks in advance, Serb


Solution

    • You need a JTextArea to display the the information from Job
    • Also what I would recommend is to override the toString() method in the Job class. Something like

      public String toString() {
          return "Job No: " + jobNum +
                 "\nCustomer: " + customer +
                 "\nCredit Limit: " + creditLimit
          ....
      }
      
    • Then in the actionPerformed all you have to do, is check the value in the text field, then get the value from the map and display it in the text area.

      public void actionPerformed(ActionEvent e) {
          String customer = textField.getText();
          if (map.containsKey(customer)) {
               jta.append(String.valueOf(map.get(customer)));
               jta.append("\n***********************\n\n");
          }
      }
      

    Run this example. to see what I mean. Just type in one of the names from the map and press the button

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.HashMap;
    import java.util.Map;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class TestMap {
    
        private JTextArea jta = new JTextArea(15, 30);
        private JTextField jtf = new JTextField(30);
        private JButton button = new JButton("Show Job");
        private Map<String, Job> map;
    
        public TestMap() {
            map = getMap();
    
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(jta, BorderLayout.CENTER);
            panel.add(jtf, BorderLayout.NORTH);
            panel.add(button, BorderLayout.SOUTH);
    
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (!"".equals(jtf.getText())) {
                        String customer = jtf.getText();
                        if (map.containsKey(customer)) {
                            jta.append(String.valueOf(map.get(customer)));
                            jta.append("\n***********************\n\n");
                        }
                        jtf.setText("");
                    }
                }
            });
    
            JFrame frame = new JFrame();
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.pack();
            frame.setVisible(true);
        }
    
        private Map<String, Job> getMap() {
            Map<String, Job> map = new HashMap<>();
            map.put("Paul", new Job(100, "Paul", 10000.00));
            map.put("Jim", new Job(101, "Jim", 20000.00));
            map.put("John", new Job(102, "John", 30000.00));
            map.put("Sean", new Job(103, "Sean", 40000.00));
            map.put("Shane", new Job(104, "Shane", 50000.00));
            map.put("Mike", new Job(105, "Mike", 60000.00));
    
            return map;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new TestMap();
                }
            });
        }
    }
    
    class Job {
    
        int jobNo;
        String customer;
        double creditLimit;
    
        public Job(int jobNo, String customer, double creditLimit) {
            this.jobNo = jobNo;
            this.customer = customer;
            this.creditLimit = creditLimit;
        }
    
        public String toString() {
            return "Job No: " + jobNo
                    + "\nCustomer: " + customer
                    + "\nCredit Limit: " + creditLimit;
        }
    }