Transferring input from one frame to another (JAVA)

That's what...

I have 2 GUI programs.
The menu program, which basically consists of a frame with food buttons, pressing this button opens this other program, the quantity input program, which is a frame with a text field, buttons for numbers, buttons for canceling and confirmation. The quantity confirmed by the user will be accessible by the menu program from the quantity input program, which will be stored in the vector, so every time the user wants to order other food products, he simply presses another button and repeats the process.

Now I have encoded most of it and got everything except one, the value returned by the quantity input program has this delay.

This is what I am doing step by step:

1) Press the power item in the menu, the Number of input data window opens.
2) I enter the number I want, it displays in the text box correctly.
3) I pressed the confirmation button, which will do 3 things, first save the text field value for the variable, then call the dispose () method and the third print statement showing the variable value (for testing purposes).
4) Then the menu program checks whether the user pressed the confirmation button in the input program, if it is true, it should call a method in the input program called getQuantity (), which returns the value of the quantity variable and stores it in the vector.
5) After that, another print statement is executed to check the correctness of the passed value, and then calls the print () method to display the name of the ordered element and its quantity.

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

MENU GUI
1st orderlast order

The ActionPerformed CONFIRM BUTTON method in the quantity input 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(); } 

CLASS OF ACTION CLASS BUTTONS OF THE MENU BUTTON in the 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 on the console:
Here I first ordered pumpkin soup, I entered the quantity 1
1st Order

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

Here I ordered the last item, fried fried salmon and entered the amount of 3

enter image description here

As you can see, the first recorded value is 0 for the first element that I ordered, when I added another element, the number of the first element is recorded, but the number of the second element is not recorded .. the same thing happens after the third element ... and the amount of the third element is not written even if the program terminates :(

How can I solve this problem?

-1
source share
2 answers

I think I see your problem, and in fact this is due to the fact that you are not using a modal dialog to get your input. You request an input user interface before the user can interact with it. Wait until I show you a small example of what I mean ...

Edit
Here is my sample code that has modal JDialog and JFrame, both act as a dialog for the main JFrame and both use the same JPanel for input. The difference is that the modal JDialog will freeze the main JFrame code in the place where it was made visible and will not be renewed until it becomes invisible - thus, the code will wait until the user encounters a dialog box, it progresses , and that matters.

 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 the solution: use modal JDialog.

+6
source

Suppose I have two GUI frames f1 and f2. Now, by pressing the button on f1, I want to call the f2 frame, and also send some data from f1 (frame class) to f2 (frame class).
One possible way is to declare a constructor in f2 that accepts the same data as the parameters that I wanted to send to it from f1.Now in the f1 frame, the encoding includes only these statements:
f1.setVisible (false); // f1 becomes invisible f2 newFrame = new f2 (uname, pass); // uname and pass were taken from the text fields f1 f2.setVisible (true);

I think this will clarify your problem.

0
source

Source: https://habr.com/ru/post/1446156/


All Articles