Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

swing - Passing input from one frame to another(JAVA)

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("
"+e.getActionCommand()+"	"+ 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...