Imaginewagons
Imaginewagons

Reputation: 61

How to pass user input from one frame to another?

I'm currently a beginner in programming and I'm kind of having a hard time right now. Currently I'm making a simple shopping lister using java. Before I go to the problem, I would like to share my code first:

This is the main page of my list:

package com.main;

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

public class MainPage extends JFrame implements ActionListener{
    JTextPane txtpnListName, txtpnTotal, txtpnSubtotal, txtpnShoppingLists;
    JTextField txtfNewItem, txtfPrice, txtItemAmount;
    JTextArea textItemList, textAmountList, textPriceList;
    JButton btnNewButton, btnNewButton_1, btnLogout, btnMakeNewList;
    JScrollPane scrollPane1, scrollPane2,scrollPane3;
    JTabbedPane tabPane;

    public MainPage(){
        super("List n\' Go");
        setResizable(false);
        getContentPane().setBackground(Color.WHITE);
        setBounds(100, 100, 600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        tabPane = new JTabbedPane(JTabbedPane.LEFT);
        add(tabPane);

        txtpnListName = new JTextPane();
        txtpnListName.setBackground(Color.WHITE);
        txtpnListName.setEditable(false);
        txtpnListName.setFont(new Font("Tahoma", Font.BOLD, 30));
        txtpnListName.setText("LIST NAME");
        txtpnListName.setBounds(175, 25, 175, 38);
        getContentPane().add(txtpnListName);

        //Buttons
        btnNewButton = new JButton("Save");
        btnNewButton.setBounds(375, 25, 75, 21);
        getContentPane().add(btnNewButton);

        btnNewButton_1 = new JButton("Add Item");
        btnNewButton_1.setBounds(375, 59, 75, 21);
        getContentPane().add(btnNewButton_1);
        btnNewButton_1.addActionListener(this);

        btnLogout = new JButton("Logout");
        btnLogout.setBounds(463, 25, 75, 21);
        getContentPane().add(btnLogout);
        btnLogout.addActionListener(this);

        txtfNewItem = new JTextField();
        txtfNewItem.setBackground(Color.WHITE);
        txtfNewItem.setEditable(false);
        txtfNewItem.setFont(new Font("Tahoma", Font.PLAIN, 16));
        txtfNewItem.setText("New Item");
        txtfNewItem.setBounds(185, 99, 200, 28);
        getContentPane().add(txtfNewItem);

        txtfPrice = new JTextField();
        txtfPrice.setBackground(Color.WHITE);
        txtfPrice.setEditable(false);
        txtfPrice.setFont(new Font("Tahoma", Font.PLAIN, 16));
        txtfPrice.setText("Price");
        txtfPrice.setBounds(490, 99, 75, 28);
        getContentPane().add(txtfPrice);
        txtItemAmount = new JTextField();
        txtItemAmount.setBackground(Color.WHITE);
        txtItemAmount.setEditable(false);
        txtItemAmount.setFont(new Font("Tahoma", Font.PLAIN, 16));
        txtItemAmount.setText("Amount");
        txtItemAmount.setBounds(395, 99, 75, 28);
        getContentPane().add(txtItemAmount);

        //ListBox
        scrollPane1 = new JScrollPane();
        scrollPane1.setBounds(185, 133, 210, 330);
        getContentPane().add(scrollPane1);

        scrollPane2 = new JScrollPane();
        scrollPane2.setBounds(400, 133, 65, 330);
        getContentPane().add(scrollPane2);

        scrollPane3 = new JScrollPane();
        scrollPane3.setBounds(490, 133, 70, 330);
        getContentPane().add(scrollPane3);

        textItemList = new JTextArea();
        textItemList.setEditable(false);
        scrollPane1.setViewportView(textItemList);
        textItemList.setBackground(Color.GRAY);
        textAmountList = new JTextArea();
        textAmountList.setEditable(false);
        scrollPane2.setViewportView(textAmountList);
        textAmountList.setBackground(Color.GRAY);

        textPriceList = new JTextArea();
        textPriceList.setEditable(false);
        scrollPane3.setViewportView(textPriceList);
        textPriceList.setBackground(Color.GRAY);

        //Summation
        txtpnTotal = new JTextPane();
        txtpnTotal.setEditable(false);
        txtpnTotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
        txtpnTotal.setText("Total no. of Items:");
        getContentPane().add(txtpnTotal);
        txtpnTotal.setBounds(195, 473, 150, 38);

        txtpnSubtotal = new JTextPane();
        txtpnSubtotal.setEditable(false);
        txtpnSubtotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
        txtpnSubtotal.setText("Subtotal:");
        txtpnSubtotal.setBounds(360, 473, 85, 67);
        getContentPane().add(txtpnSubtotal);

        txtpnShoppingLists = new JTextPane();
        txtpnShoppingLists.setEditable(false);
        txtpnShoppingLists.setFont(new Font("Tahoma", Font.BOLD, 15));
        txtpnShoppingLists.setText("Shopping Lists");
        txtpnShoppingLists.setBounds(10, 10, 120, 31);
        getContentPane().add(txtpnShoppingLists);

        //For new List
        btnMakeNewList = new JButton("Make New List");
        btnMakeNewList.setBounds(10, 519, 120, 21);
        getContentPane().add(btnMakeNewList);

        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent exit){
                int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the app?", "Confirm Exit",0);
                if(reply == JOptionPane.YES_OPTION){
                    JOptionPane.showMessageDialog(null, "Thank your for using List n' Go!", "Goodbye!",1);
                    System.exit(0);
                }
                else{
                    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                }
            }
        });
        setSize(600,600);
        setVisible(true);
        setResizable(false);
    }
    //for testiong purposes
    public static void main (String []args){
        MainPage mainpage = new MainPage();
    }
    //Action Listeners
    public void actionPerformed(ActionEvent e) {
    //logout Button
        if(e.getSource() == btnLogout){
            int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to Logout?", "Confirm Logout?",2);
            if( reply == JOptionPane.YES_OPTION){
                JOptionPane.showMessageDialog(null, "You have logged out.", "Logout Success",1);
                LoginScreen login = new LoginScreen();
                dispose();
            }
            else{
                setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
            // Make new List Button
        }
        else if(e.getSource() == btnMakeNewList){
        }
        // Add item to list Button
        else if(e.getSource() == btnNewButton_1){

        }
        else if (e.getSource() == btnNewButton){
        }
    }
}

And here is the GUI where I want to ask the user for input:

package com.main;

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

public class AddToList extends JFrame implements ActionListener{
    JLabel itemName, itemAmount, itemPrice, itemTotalPrice;
    JTextField jtfItemName, jtfItemAmount, jtfItemPrice, jtfTotalPrice;
    JButton addItem, goBack;
    public AddToList(){
        super("What do you want to add?");
        setLayout(null);
        itemName = new JLabel("Item Name");
        itemAmount = new JLabel("Item Amount");
        itemPrice = new JLabel("Item Price");
        itemTotalPrice = new JLabel();
        jtfItemName = new JTextField();
        jtfItemAmount = new JTextField();
        jtfItemPrice = new JTextField();
        jtfTotalPrice = new JTextField();
        addItem = new JButton("Add Item");
        goBack = new JButton("Back");
        itemName.setBounds(10,10, 70,15);
        jtfItemName.setBounds(75,10, 270,20);
        itemAmount.setBounds(350,10, 70,15);
        jtfItemAmount.setBounds(425,10, 120,20);
        itemPrice.setBounds(550,10, 70,15);
        jtfItemPrice.setBounds(610,10, 120,20);
        addItem.setBounds(250,35, 100,25);
        goBack.setBounds(400,35, 100,25);
        addItem.addActionListener(this);
        goBack.addActionListener(this);
        add(itemName);add(jtfItemName);add(itemAmount);
        add(jtfItemAmount);add(itemPrice);add(jtfItemPrice);
        add(addItem);add(goBack);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent exit){
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            }
        });
        setSize(750, 100);
        setResizable(false);
        setVisible(true);
    }
    // test Program
    public static void main(String []args){
        AddToList test = new AddToList();
    }
    // Action Trigger
    public void actionPerformed(ActionEvent e) {
        //Add item Confirmation
        if(e.getSource() == addItem){
        }
        // Cancel Add Item
        else if(e.getSource() == goBack){

        }
    }
}

My goal here is after the user clicked the "Add Item" button from the Main page, it will prompt the user to input the product details such as the name, amount, and price on the add Item GUI. it will then take the user input and list it on the main page. After that it will calculate the total amount of items on the list, it's amount, and total price. The problem is that I am currently stuck on how can I pass the user input from the Add Item GUI to the main page. I've been researching but I still couldn't find my way around it. I hope you can help me and any help is much appreciated.

P.S: I'm sorry if you find my code a bit too long because this is what currently I could do with the best of my knowledge and abilities. But don't worry, I know that I shouldn't use setLayout(null) since swing has layout managers that I can use. I am still currently learning more about it so bear with me please.

Upvotes: 0

Views: 223

Answers (1)

queeg
queeg

Reputation: 9432

Usually I have a JFrame holding most of the data. If additional information is required by the application, it will show an input dialog. Most of the time this input is not just a string but some more complex stuff. So here is the pattern I follow:

Create a form derived from JPanel. Add the UI elements you need, and use the bean pattern so the necessary data can be injected/retrieved. Very likely you want to show such input when a button in a pane is pressed, so the ActionListener code can look like this:

FormBean fb = new FormBean();
fb.setData(...);    (use setters to display data if required/feasible)
if (JOptionPane.showOptionDialog(this, fb, ...)==JOptionPane.OK_OPTION) {
    // user pressed ok, so process the data he entered
   fb.getData();
}

The advantage of using JOptionPane.showOptionDialog is that you get a standard modal dialog window without threading issues or the question when to activate your code again.

Upvotes: 1

Related Questions