Java Program Help

Two hours of google searches and I now do admit defeat. I am working on a simple java program but I just can't figure out how to get the text from one jTextField to equal the text of another jTextField in a separate jFrame. The variables in question are accNumber in NewJFrame.java and AccnDynamicLabel in NewFrame.java. I am looking to make the text within AccDynamicLabel equal to the text in accNumber when the user clicks the createBtn in NewJFrame.java. Here is my code for the two java files

NewJFrame.java:

<code>

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package bank;

/**
*
* @author CJ
*/
public class NewJFrame extends javax.swing.JFrame {
private String Helo;

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

panel = new javax.swing.JPanel();
accnLabel = new javax.swing.JLabel();
accBal = new javax.swing.JLabel();
createBtn = new javax.swing.JButton();
accNumber = new javax.swing.JTextField();
accBalText = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

accnLabel.setText("Account Number");

accBal.setText("Initial Balance");

createBtn.setText("Create");
createBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createBtnActionPerformed(evt);
}
});

accNumber.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
accNumberInputMethodTextChanged(evt);
}
});

accBalText.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
accBalTextInputMethodTextChanged(evt);
}
});

javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addComponent(accnLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(accNumber)
.addGap(10, 10, 10))
.addGroup(panelLayout.createSequentialGroup()
.addComponent(accBal)
.addGap(18, 18, 18)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addComponent(createBtn)
.addGap(0, 73, Short.MAX_VALUE))
.addComponent(accBalText))
.addContainerGap())
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(accnLabel)
.addComponent(accNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(5, 5, 5)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(accBal)
.addComponent(accBalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(createBtn)
.addContainerGap(20, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void createBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
NewFrame f = new NewFrame();
f.pack();
f.setVisible(true);

}

private void accNumberInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
String x = accNumber.getText();
}

private void accBalTextInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
// TODO add your handling code here:
String y = accBal.getText();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel accBal;
public javax.swing.JTextField accBalText;
public javax.swing.JTextField accNumber;
private javax.swing.JLabel accnLabel;
private javax.swing.JButton createBtn;
private javax.swing.JPanel panel;
// End of variables declaration
}

</code>

NewFrame.java:

<code>

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package bank;

/**
*
* @author CJ
*/
public class NewFrame extends javax.swing.JFrame {

/**
* Creates new form NewFrame
*/
public NewFrame() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

depLab = new javax.swing.JLabel();
withLab = new javax.swing.JLabel();
deposit = new javax.swing.JTextField();
withdraw = new javax.swing.JTextField();
submit = new javax.swing.JButton();
accnLabel = new javax.swing.JLabel();
AccnDynamicLabel = new javax.swing.JLabel();
BalanceStaticLabel = new javax.swing.JLabel();
BalanceDynamicLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(400, 200));

depLab.setText("Deposit");

withLab.setText("Withdraw");

submit.setText("Submit");

accnLabel.setText("Account Number:");

AccnDynamicLabel.setText("N/A");

BalanceStaticLabel.setText("Balance:");

BalanceDynamicLabel.setText("N/A");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(depLab)
.addGap(18, 18, 18)
.addComponent(deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(withLab)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(submit)
.addComponent(withdraw, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(accnLabel))
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(AccnDynamicLabel))
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(BalanceDynamicLabel))
.addComponent(BalanceStaticLabel))))
.addContainerGap(45, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(depLab)
.addComponent(deposit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(accnLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(withLab)
.addComponent(withdraw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(AccnDynamicLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(BalanceStaticLabel)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(BalanceDynamicLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(submit)
.addGap(23, 23, 23))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel AccnDynamicLabel;
private javax.swing.JLabel BalanceDynamicLabel;
private javax.swing.JLabel BalanceStaticLabel;
private javax.swing.JLabel accnLabel;
private javax.swing.JLabel depLab;
private javax.swing.JTextField deposit;
private javax.swing.JButton submit;
private javax.swing.JLabel withLab;
private javax.swing.JTextField withdraw;
// End of variables declaration
}

</code>

Set a global string variable for what you want to transfer, parse the text field and set it to that string that was parsed to the global and then use settext to set the text in the other text field. It should look something like this

(This needs to go right before any methods)

private static string whatever ="";

(Parse the text field and set your variable)

whatever = textfield.getText();

(Finally set the other text field)

othertextfield.setText(whatever);

Next time, please use pastebin to post your code.  It makes it much easier to read.  Luckily, NetBeans can reformat code with a couple of mouse clicks.

The main reason why you cannot set the AccnDynamicLabel in NewFrame equal to the value of accNumber in NewJFrame is that these are two, discrete classes that have no link to each other, except that you are creating an instance of NewFrame within the createBtn’s ActionListener of NewJFrame.

There is no way to pass information between these two classes with your current design.  You have a few options you can use to refactor your design.  You can move the NewFrame class to be an inner class of NewJFrame, you can move all of the functionality of NewFrame to be part of NewJFrame, or you can create a link between the two classes. 

If your goal is to keep these classes as independent components that can be reused later, then option three is your best choice—it also changes your current design the least.

The easiest way to link these classes is to create a new constructor in NewFrame that takes an instance of JTextField as an argument.  In the NewFrame’s new constructor, call the no args constructor and set AccnDynamicLabel’s text equal to the passed JTextField’s text.

Next, in NewJFrame’s createBtnActionPerformed method, call the new constructor instead of the no arg constructor, passing this.accNumber to it.

It looks like you are also planning to do the same thing with account balance.  And you can certainly repeat the above steps and add account balance to the new constructor.  But if you get to the point where every time you add new functionality to your application, you have to add a new parameter to the constructor, you will be better off just storing an instance of NewJFrame within NewFrame and change NewFrame’s constructor to just take an instance of NewJFrame.

Here are the updated changes on pastebin

NewJFrame:

http://pastebin.com/gzNMdgFe

NewFrame:

http://pastebin.com/cqbH62p9

Thanks for the help, I got an A on the project and an overall A in the class!

Epic necroposting btw