Search code examples
javaswingactionlistenerjcomboboxitemlistener

Text displays two times instead of once


Would you please checks what is wrong with my code below. It is a simple ordering system that requires the user to input a customer name, cashier name, whether it is dine in or take out and password. Every time I click a meal once in the combo box it doubles the meal I selected

for example I clicked PM3 it displays:

Your is order is/are: PM3 (Pork Barbeque 4 pcs.)
PM3 (Pork Barbeque 4 pcs.)

but it should be: Your is order is/are: PM3 (Pork Barbeque 4 pcs.)

By the way I have 2 classes I will show their codes below.

Code of class Show:

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

 public class Show{

  private static String z  = "";
  private static String w  = "";
  private static String x  = "";
  JComboBox combo;
  private static String a  = "";

 public Show(){
 String mgaPagkainTo[] = {"PM1 (Paa/ Spicy Paa with Thigh part)","PM2 (Pecho)","PM3 (Pork Barbeque 4 pcs.)","PM4 (Bangus Sisig)","PM5 (Pork Sisig)","PM6 (Bangus Inihaw)","SM1 (Paa)","SM2 (Pork Barbeque 2 pcs.)","Pancit Bihon","Dinuguan at Puto","Puto","Ensaladang Talong","Softdrinks","Iced Tea","Halo-Halo","Leche Flan","Truon Split"};
 JFrame frame = new JFrame("Mang Inasal Ordering System");
 JPanel panel = new JPanel();
 combo = new JComboBox(mgaPagkainTo);
 combo.setBackground(Color.gray);
 combo.setForeground(Color.red);
 panel.add(combo);
 frame.add(panel);
    combo.addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent ie){
        String str = (String)combo.getSelectedItem();
        a = str;
        ShowOrder messageOrder1 = new ShowOrder();
        messageOrder1.ShowOrderPo();
       }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,250);
    frame.setVisible(true);
  }

   public static void main(String[] args) {
   Scanner inp = new Scanner(System.in);
   boolean ulitinMoPows = true;
   boolean tryAgain = true;

    System.out.print("\nInput Customer Name: ");
    String customerName = inp.nextLine();
    w = customerName;
    System.out.print("\nInput Cashier Name: ");
    String user = inp.nextLine();
    z = user;
  do{
    System.out.print("\nInput either Dine In or Take Out: ");
    String dInDOut = inp.nextLine();
    x = dInDOut;
        if (x.equals("Dine In") || x.equals("Take Out")){
         System.out.print("");
         ulitinMoPows = false;
         }
        else{
         JOptionPane.showMessageDialog(null, "Try again! Please input Dine In or Take Out only!","Error", JOptionPane.ERROR_MESSAGE);
             ulitinMoPows = true;
             System.out.print ("\f");
            }
 }while(ulitinMoPows);
 do{
    System.out.print("\nInput password: ");
    String pass = inp.nextLine();
    if(pass.equals("admin")){
        ShowOrder messageShowMenu = new ShowOrder();
        messageShowMenu.ShowMenu();
        tryAgain = false;
    }
    if(!pass.equals("admin")){
        JOptionPane.showMessageDialog(null, "Try again! Invalid password!","Error Logging-In", JOptionPane.ERROR_MESSAGE);
    tryAgain = true;
     System.out.print ("\f");
    }
}while(tryAgain);
Show j = new Show();
    }

  public static String kuhaOrder()
{
  return a;
}

public static String kuhaUserName()
{
return z;
}

public static String kuhaCustomerName()
{
return w;
}

public static String kuhaSanKainPagkain()
{
return x;
}  
}

Code of class ShowOrder:

public class ShowOrder {

public void ShowMenu(){
    String user = Show.kuhaUserName();
    System.out.print("\n\n\t\tCashier: " +user);
    String dInDOut = Show.kuhaSanKainPagkain();
    System.out.print("                          "+dInDOut);
    String customerName = Show.kuhaCustomerName();
    System.out.print("\n\t\tCustomer Name: " +customerName);
    System.out.print("\t\t\t\n\nYour order is/ are: ");
}


public void ShowOrderPo() {
    String order = Show.kuhaOrder();
    System.out.print("\t\t\t\t\n " +order);

}
}

Solution

  • The user changes the selection in the combo box. So a first ItemEent is fired to tell that the previously selected item has been de-selected, and a second one is fired to say that the newly-selected item has been selected.

    I would use an ActionListener instead of an ItemListener.