1

I am working with two JFrames, POS & Amounts. when I click the Value button on POS frame it calls the Amount class for me to enter value(#) in there and when I click on ENTER those values should be transferred back to the textarea in POS frame. I can get the text to appear within the text area by creating a new object of POS jframe, what I want to do is put the values back into the original POS frame that is already open.

See code below...

 public class Amounts extends javax.swing.JFrame {
String val; 
public Amounts(){
     initComponents(); 
}
public Amounts(String AmtVal) {

    this.val = AmtVal;
    String getValue = txtField.getText();
    txtField.setText(val + getValue);
 }
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:  

    String Enternum = txtField.getText();

            if (Enternum == "")
            {
                txtField.setText(jButton1.getText());
            }
        else {
             Enternum = txtField.getText() + jButton1.getText();
                txtField.setText(Enternum);
            }
          } 

   private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:  

    String Enternum = txtField.getText();

            if (Enternum == "")
            {
                txtField.setText(jButton2.getText());
            }
        else {
             Enternum = txtField.getText() + jButton2.getText();
                txtField.setText(Enternum);
            }
          } 
       private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:  

    String Enternum = txtField.getText();

            if (Enternum == "")
            {
                txtField.setText(jButton3.getText());
            }
        else {
             Enternum = txtField.getText() + jButton3.getText();
                txtField.setText(Enternum);
            }
          } 
     private void jEnterActionPerformed(java.awt.event.ActionEvent evt) {                                       
     // TODO add your handling code here:
      String data =  txtField.getText();

     new POS_System(data).setVisible(true);
 }  

  // Variables declaration - do not modify                     
private javax.swing.JButton jEnter;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
}

The POS class code:

public class POS_System extends javax.swing.JFrame {
 public POS_System() {
    initComponents();
}

String val ="";
public POS_System(String amt) {
    initComponents();
    this.val = amt;

    jTextArea1.setText(amt);
}

 private void jValueActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:

    Amounts at = new Amounts ();
    at.setVisible(true);
    at.setLocationRelativeTo(null);
    at.txtField.getText();
    jTextArea1.setText(at.txtField.getText());      
}  

  // Variables declaration - do not modify                     
   private javax.swing.JButton jTextArea1;
   private javax.swing.JButton jEnter;
Joe
  • 11
  • 2
  • Unrelated: you should read about java naming conventions. Variable names go camelCase, all the time. You also *import* the classes you are using, there is no point in using fully qualified names such as `javax.swing.JButton` all the time. – GhostCat Sep 10 '18 at 14:43
  • And you **must** read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java ... as your code `Enternum == ""` simply doesn't do what you think it does. – GhostCat Sep 10 '18 at 14:43
  • Beyond that: it seems you are overburdening yourself. Comparing strings is a super basic topic. You should first collect some experience with all the basic things of java before writing GUI applications. Finally: GUI apps work in different way. You first think about the data they are supposed to visualize, and then you think up *models* that contain that data. To then think connect UI elements to model objects, and represent information. Long story short: you want to read the Oracle tutorial on swing top to bottom. – GhostCat Sep 10 '18 at 14:45
  • Thanks for the comment but it seems either the question was not clear but I am not concerned with string comparison although point taken. I want to be able to post data to an already open JFrame and as mentioned I can currently complete the task the only problem is that a new instance is generated which defeats the purpose.. – Joe Sep 10 '18 at 14:58
  • You shouldnt try to build a skyscraper when you don't know how to dig a basement. Seriously: learning programming is about going slowly, step by step. The **more** details you add (such as fancy UI elements), the **harder** it becomes for you to understand what your code is doing. Believe me: if you put too many things in your schedule too quickly, you slow yourself *down*. That wrong string comparison, in a simple program, you maybe find the bug yourself in a few minutes. Hidden in 100+ lines of Swing code, even an expert might overlook it and wonder "why does it not work". – GhostCat Sep 10 '18 at 15:02

0 Answers0