0

I have nameS being saved into a string and being written to a file, but in the next window it pops up with a successful message i want to be able to use the name given in the variable nameS, to the successful window. have no idea how to pass it through.

public void setPanels()
{
    mainPanel = new JPanel(new GridLayout(0, 2));
    JPanel containerPanel = new JPanel(new GridLayout(0, 1));
    JPanel lowerPanel = new JPanel(new FlowLayout());
    JPanel errorPanel = new JPanel(new FlowLayout());
    JButton apply = new JButton("PAY");
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
        }
    });
    apply.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent e)
        { 
            errorMessage= "";
            safetyMessage= "";

            String timestamp = new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(new Date());
            String ammountS = ammountField.getText();
            String nameS = nameField.getText();
            String billingAddressS = billingAddressField.getText();
            String cardNameS = cardNameField.getText();
            String cardNumberS = cardNumberField.getText();
            String expiryDateS = expiryDateField.getText();

            if(ammountS.matches("[a-zA-Z]+")){
                safetyMessage += "Top Ammount invalid<br/>";
            }else{errorMessage += "";}
            if(ammountS == null || ammountS.isEmpty()){
                safetyMessage += "Please Enter an Ammount!<br/>";
            }else{errorMessage += "";}
            if(nameS == null || nameS.isEmpty()){
                safetyMessage += "Please Enter an Name!<br/>";
            }else{errorMessage += "";}
            if(billingAddressS == null || billingAddressS.isEmpty()){
                safetyMessage += "Please Enter a Billing Address!<br/>";
            }else{errorMessage += "";}
            if(cardNameS == null || cardNameS.isEmpty()){
                safetyMessage += "Please Enter a Card Name!<br/>";
            }else{errorMessage += "";}
            if(cardNumberS == null || cardNumberS.isEmpty()){
                safetyMessage += "Please Enter a Card Number!<br/>";
            }else{errorMessage += "";}
            if(expiryDateS == null || expiryDateS.isEmpty()){
                safetyMessage += "Please Enter a Expiry Date!<br/>";
            }else{errorMessage += "";}

            if (safetyMessage == ""){
                errorMessage += "Successfully Topped Up With: $"+ ammountS;
                balanceAmmount(nameS);
                errorField.setText(errorMessage);
                errormsg = true;
            }else{
                errorMessage += "<html>";
                errorMessage += safetyMessage;
                errorMessage += "</html>";
                errorField.setText(errorMessage);
            }

            if (errormsg == true){
            WriterFile("######## START OF TRANSACTION ########\n","TopUp.txt");
            WriterFile("Name: "+ nameS+"\n","TopUp.txt");
            WriterFile("DATE: "+ timestamp+"\n","TopUp.txt");
            WriterFile("Ammount: "+ ammountS+"\n","TopUp.txt");
            WriterFile("CardName: "+ cardNameS+"\n","TopUp.txt");
            WriterFile("Address: "+ billingAddressS+"\n","TopUp.txt");
            WriterFile("Card Number: "+ cardNumberS+"\n","TopUp.txt");
            WriterFile("ExpiryDate: "+ expiryDateS+"\n","TopUp.txt");
            WriterFile("######## END OF TRANSACTION ########\n","TopUp.txt");

            topUpAlertWindow TopUpAlertWindow = new topUpAlertWindow();
                //balanceAmmount(nameS);
            //i am trying to pass in nameS into here.
            //dispose();
            }
        }
    });

and i want to send nameS variable into the next java file

import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;

class topUpAlertWindow extends JDialog
{
public topUpAlertWindow(){
    setPanels();

    setModalityType(ModalityType.APPLICATION_MODAL);
    setSize(500, 250);
    setVisible(true);
}

private JPanel mainPanel;

public void setPanels()
{
    mainPanel = new JPanel(new GridLayout(0, 2));
    JPanel containerPanel = new JPanel(new GridLayout(0, 1));
    JPanel lowerPanel = new JPanel(new FlowLayout());
    JPanel errorPanel = new JPanel(new FlowLayout());
    JLabel successfulMessage = new JLabel("Successfully Toped UP");
    JButton apply = new JButton("Okay");
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
        }
    });

    apply.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent e)
        { 
            dispose();


        }
    });

    mainPanel.add(successfulMessage);

    lowerPanel.add(apply);
    //lowerPanel.add(cancel);


    containerPanel.add(mainPanel);
    containerPanel.add(lowerPanel);
    containerPanel.add(errorPanel);

    add(containerPanel);
}
}
Vaughan D
  • 149
  • 1
  • 1
  • 15
  • 1
    1) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson May 13 '15 at 11:43
  • also thank you, bookmarked that page, has a really nice guide on the layouts, sorry I'm extremely new to java and my teachers are very difficult to understand. so there is a broken line in my java experience – Vaughan D May 13 '15 at 11:49

1 Answers1

2

In the first class:

topUpAlertWindow TopUpAlertWindow = new topUpAlertWindow(nameS);

In the second class, change constructor:

public topUpAlertWindow(String nameS){
    setPanels();

    setModalityType(ModalityType.APPLICATION_MODAL);
    setSize(500, 250);
    setVisible(true);
}

From the constructor you can use the name to display a Label or something.

JohannisK
  • 535
  • 2
  • 10
  • WOW quick response, and i did not think of passing it through this way. so simple, i've had way to much coffe HAHA, but thank you – Vaughan D May 13 '15 at 11:47