0

I am trying to create a Java Gui with a circular queue array that has ten elements, and I can't seem to properly dequeue an item in the text field. Every time I click on the Dequeue Button all the other text field are the one that are affected except the text field that I want to dequeue.

Enqueuing Items

Dequeuing the first text field

This is the code:

public int deQueue() {
   int temp;

   // Condition for empty queue.
   if(front == -1) {
      // Return -1 in case of empty queue
      return -1;
   }
         
   temp = queue.get(front);
         
   // Condition for only one element
   if(front == rear) {
      front = -1;
      rear = -1;
   } else if(front == size - 1) {
      front = 0;
   } else {
      front = front + 1;
   }
   
   // Returns the dequeued element
   return temp;
}

void switchClear(int i){           
   switch (i) {
      case 0 -> txt0.setText(String.valueOf(" "));
      case 1 -> txt1.setText(String.valueOf(" "));
      case 2 -> txt2.setText(String.valueOf(" "));
      case 3 -> txt3.setText(String.valueOf(" "));
      case 4 -> txt4.setText(String.valueOf(" "));
      case 5 -> txt5.setText(String.valueOf(" "));
      case 6 -> txt6.setText(String.valueOf(" "));
      case 7 -> txt7.setText(String.valueOf(" "));
      case 8 -> txt8.setText(String.valueOf(" "));
      case 9 -> txt9.setText(String.valueOf(" "));
      default -> {}
   }
}
    
void clearDisplay(){
   for(int i = 0; i <= cq.rear;i++){
      switchDislpay(i);
   }
}
    
private void btnDequeueActionPerformed(java.awt.event.ActionEvent evt) {                                           
   int x = cq.deQueue();
   if(x != -1) {
      txtDequeue.setText(String.valueOf(x));
      txtFront.setText(String.valueOf(cq.front));
      txtRear.setText(String.valueOf(cq.rear));
      btnEnqueue.setEnabled(true);
      clearDisplay();
   } else {
      JOptionPane.showMessageDialog(panel, "Error! Queue is Empty!", "Warning",JOptionPane.WARNING_MESSAGE);
      btnDequeue.setEnabled(false);
      clearDisplay();
      clearForm();
   }
}
Popovkov57
  • 179
  • 16
Beta
  • 1
  • 1
  • When you use a "switch" statement you must add a "break" statement for each "case" statement or the code just falls through, executing all the remaining statements. – camickr Oct 12 '22 at 02:39

0 Answers0