Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I create these classes with netbeans Jframe form. I put a checkbox and a button in class1 and a textfield in class2.

I want to sent a text to the textfield in class2 with class1. I don't recieve error but code isn't work. Could you help me for fix it? Thanks.

Java
package test;
public class class1 extends javax.swing.JFrame {
public class1() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents()...                        

public void sent(class2 theOtherClassInstance) {
    if (jCheckBox1.isSelected()) {
        theOtherClassInstance.jTextField1.setText("That's it"); // this jtextfield in the class2
    }
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    class2 theOtherClassInstance = new class2();
    sent(theOtherClassInstance);

    this.setVisible(false);
    new class2().setVisible(true);
}                                        

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new class1().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
// End of variables declaration                   
}


This is the class2:

Java
package test;
public class class2 extends javax.swing.JFrame {
    public class2() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents()...                       

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new class2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}
Posted
Updated 8-Jan-16 20:31pm
v4
Comments
Sergey Alexandrovich Kryukov 8-Jan-16 14:14pm    
Not clear. You never explained what is classB...
Your progress in English is impressive, and, if Java is very complicated for you, just learn it to make it easy. :-)
—SA
bugsoul 9-Jan-16 2:35am    
Thanks for suggestion Sergey. But I can't learn with easy things. I can learn just like project only:) I put my whole code. If you look at it, I'll be very happy. Thanks.

1 solution

At a guess, jtextfield is defined as an instance member of the class classB, but you're trying to access it like a class (static) member.

This is a common problem for users coming from VB.NET, since VB.NET creates a default instance of your Form classes for you, and makes them available as global variables with the same name as the class. (So Form1.TextBox1 accesses the control on the default instance of the Form1 class.) This was done for backwards-compatibility with VB6, and always causes confusion for people moving to a different language!

You'll need an instance of the class in order to access the instance members:
Java
public first(classB theOtherClassInstance) {
    initComponents();
    if (jCheckBox1.isSelected()) {
        theOtherClassInstance.jtextfield.setText("That's it !");
    }
}


NB: It always helps if you post the error message you're getting. :)
 
Share this answer
 
Comments
bugsoul 8-Jan-16 15:04pm    
Thanks Richard, this time that error appered;
"jtextfield has private access in classB"

Maybe I have to use a button click event,
Richard Deeming 8-Jan-16 15:06pm    
That just means that jtextfield is declared as private. Change it to public, and the code should work.
bugsoul 8-Jan-16 15:16pm    
OK I understand. Thanks. Netbeans don't access me for change initComponents(). I'll find the solution.
bugsoul 8-Jan-16 15:38pm    
I change the public and add button event but unfortunately it isn't work.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
classB theOtherClassInstance = null;
first(theOtherClassInstance);
}
Richard Deeming 8-Jan-16 15:41pm    
You're passing in null as the instance of classB. You need to pass in the actual instance of the class that you want to update.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900