Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:28:16+00:00 2026-05-30T20:28:16+00:00

Here’s the thing… I have 2 GUI programs. A Menu Program,which is basically a

  • 0

Here’s the thing…

I have 2 GUI programs.
A Menu Program,which is basically a frame with buttons of food items,the buttons when clicked
opens this other program,an Input Quantity Program,which is a frame with a text field,buttons for numbers,buttons for Cancel and Confirm. The quantity that is confirmed by the user will be accessed by the menu program from the Input Quantity Program to be stored in a vector so that every time a user wants to order other food items he will just click another button and repeat the process.

Now I’ve coded the most part and got everything working except one thing,the value returned by the Input Quantity Program has this delay thing.

This is what I do step by step:

1)Click a food item in Menu,it opens the Input Quantity window.
2)I input the number I want,it displayed in the text box correctly.
3)I pressed confirm which will do 3 things,first it stores the value of the text field to a variable,second it will call the dispose() method and third a print statement showing the value of the variable(for testing purposes).
4)The menu program then checks if the user has already pressed the Confirm button in the Input program,if true it shall call a method in the Input program called getQuantity() which returns the value of the variable ‘quantity’ and store it in the vector.
5)After which executes another print statement to check if the passed value is correct and then calls the method print() to show the ordered item name and it’s recorded quantity.

Here are the screenshots of the GUI and the code will be below it.

MENU GUI
1st order
last order

ActionPerformed method of the CONFIRM BUTTON in the Input Quantity Program:

private void ConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    confirmed = true;
    q= textField.getText().toString();
    quantity =Integer.parseInt(q) ;
    System.out.println("getQTY method inside Input Quantity Interface:" +getQuantity());
    System.out.println("Quantity from confirmButton in Input Quantity Interface actionPerformed: "+quantity);

    //getQuantity();
}                            

ACTION LISTENER CLASS of the MENU ITEM BUTTONS in MENU PROGRAM which does step 2 above:

class f implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) 
    {
         inputGUI.setVisible(true);
         int  q =0;

          q=inputGUI.getQuantity(); //call method to get value from Input Program

          System.out.println("Quantity inside Menu actionperformed from AskQuantity interface: "+q);

         orderedQuantity.add(q); //int vector
         textArea.append("\n"+e.getActionCommand()+"\t"+ q);
         orderedItems.add(e.getActionCommand());  //String vector
         print();
         /*
         System.out.println("Enter QTY: ");
         int qty = in.nextInt();
         orderedQuantity.add(qty);
         print();*/
   }

Here are screenshots of the print statements in the console:
Here I first ordered Pumpkin Soup,I entered a quantity of 1
1st Order

Here I ordered seafood marinara and entered a quantity of 2
2nd order

Here I ordered the last item,pan fried salmon and entered a quantity of 3

enter image description here

As you can see the first recorded quantity is 0 for the first item I ordered then when I added another item,the quantity of the first item gets recorded but the 2nd item’s quantity is not recorded..same goes after the third item… and the quantity of the 3rd item is not recorded even if the program terminates 🙁

How can I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:28:17+00:00Added an answer on May 30, 2026 at 8:28 pm

    I think I see your problem, and in fact it stems directly from you’re not using a modal dialog to get your input. You are querying the inputGUI before the user has had a chance to interact with it. Hang on while I show you a small example of what I mean…

    Edit
    Here’s my example code that has a modal JDialog and a JFrame, both acting as a dialog to a main JFrame, and both using the very same JPanel for input. The difference being the modal JDialog will freeze the code of the main JFrame at the point that it has been made visible and won’t resume until it has been made invisible — thus the code will wait for the user to deal with the dialog before it progresses, and therein holds all the difference.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DialogExample {
       private static void createAndShowGui() {
          JFrame frame = new JFrame("Dialog Example");
          MainPanel mainPanel = new MainPanel(frame);
    
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class MainPanel extends JPanel {
       private InputPanel inputPanel = new InputPanel();
       private JTextField responseField = new JTextField(10);
       private JDialog inputDialog;
       private JFrame inputFrame;
    
       public MainPanel(final JFrame mainJFrame) {
          responseField.setEditable(false);
          responseField.setFocusable(false);
    
          add(responseField);
          add(new JButton(new AbstractAction("Open Input Modal Dialog") {
    
             @Override
             public void actionPerformed(ActionEvent e) {
                if (inputDialog == null) {
                   inputDialog = new JDialog(mainJFrame, "Input Dialog", true);
                }
                inputDialog.getContentPane().add(inputPanel);
                inputDialog.pack();
                inputDialog.setLocationRelativeTo(mainJFrame);
                inputDialog.setVisible(true);  
    
                // all code is now suspended at this point until the dialog has been 
                // made invisible
    
                if (inputPanel.isConfirmed()) {
                   responseField.setText(inputPanel.getInputFieldText());
                   inputPanel.setConfirmed(false);
                }
             }
          }));
          add(new JButton(new AbstractAction("Open Input JFrame") {
    
             @Override
             public void actionPerformed(ActionEvent e) {
                if (inputFrame == null) {
                   inputFrame = new JFrame("Input Frame");
                }
    
                inputFrame.getContentPane().add(inputPanel);
                inputFrame.pack();
                inputFrame.setLocationRelativeTo(mainJFrame);
                inputFrame.setVisible(true);  
    
                // all code continues whether or not the inputFrame has been
                // dealt with or not.
    
                if (inputPanel.isConfirmed()) {
                   responseField.setText(inputPanel.getInputFieldText());
                   inputPanel.setConfirmed(false);
                }
    
             }
          }));
       }
    }
    
    class InputPanel extends JPanel {
       private JTextField inputField = new JTextField(10);
       private JButton confirmBtn = new JButton("Confirm");
       private JButton cancelBtn = new JButton("Cancel");
       private boolean confirmed = false;
    
       public InputPanel() {
          add(inputField);
          add(confirmBtn);
          add(cancelBtn);
    
          confirmBtn.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                confirmed = true;
                Window win = SwingUtilities.getWindowAncestor(InputPanel.this);
                win.setVisible(false);
             }
          });
          cancelBtn.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                confirmed = false;
                Window win = SwingUtilities.getWindowAncestor(InputPanel.this);
                win.setVisible(false);
             }
          });
       }
    
       public boolean isConfirmed() {
          return confirmed;
       }
    
       public void setConfirmed(boolean confirmed) {
          this.confirmed = confirmed;
       }
    
       public String getInputFieldText() {
          return inputField.getText();
       }
    }
    

    So solution: use a modal JDialog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an example: I have a file 1.js, which has some functions. I
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here we go again, the old argument still arises... Would we better have a
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here's what I'm trying to accomplish with this program: a recursive method that checks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.