2

I have two classes, first one is NewJFrame.java. Its code is-

package javaapplication10;
import java.awt.Label;
public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }



    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        f = new javax.swing.JTextField();
        l = new java.awt.Label();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        l.setText("label1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(139, 139, 139)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(f, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(l, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(185, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(f, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(22, 22, 22)
                .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(186, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.out.println("Calling");
        NewClass1 n = new NewClass1();
        n.st();

    }                                        



    public static void main(String args[]) {

        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);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    protected javax.swing.JTextField f;
    protected javax.swing.JButton jButton1;
    protected java.awt.Label l;

}

I am using netbeans. Main lines of codes are -

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            System.out.println("Calling");
            NewClass1 n = new NewClass1();
            n.st();

        } 

and variables declaration

protected javax.swing.JTextField f;
protected javax.swing.JButton jButton1;
protected java.awt.Label l;

My second class is NewClass.java and its code is - package javaapplication10;

import java.awt.Label;
public class NewClass1 extends NewJFrame{

    public void st()

    {
        l.setText(f.getText());
    }

}

what I want to do is to click on the button(jbutton1) and the text written in the textfield(f) must be set in the Label(l). When I am clicking on the button "Calling" is printed on the console but the text on Label is not changing. What is the problem? Please Help.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ajay Sainy
  • 279
  • 1
  • 9
  • 21
  • I am sorry 'tf' is wrong. Its 'f' only. I have edited the question. – Ajay Sainy Mar 30 '13 at 20:23
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) `import java.awt.Label; public class NewJFrame extends javax.swing.JFrame {` Don't mix Swing (`JFrame`) & AWT (`Label`) components unless necessary. In this case use a `JLabel` instead of `Label`. 3) Don't extend frame (or dialog) unless necessary. In this case, just use an instance of the component. – Andrew Thompson Mar 30 '13 at 22:51

1 Answers1

2

Why not you directly change the text of JLabel l? in following way:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
  System.out.println("Calling");
  //NewClass1 n = new NewClass1();
  //n.st();
   l.setText(f.getText());   
}    

UPDATE
Ok here u go , if you wish to do it via child class:

change jButton1ActionPerformed to this way:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
  System.out.println("Calling");
  NewClass1 n = new NewClass1(this);
  n.st();
}

And Change the constructor of NewClass as follows:

NewJFrame njf;
public NewClass(NewJFrame jf)
{
  this.nfj = jf;
}
public void st()
{
  nfj.l.setText(nfj.f.getText());
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • I want to do it by using another class. (inheritance) – Ajay Sainy Mar 30 '13 at 20:24
  • Thankyou...Its working. Can you please explain or provide me some link to read about it? – Ajay Sainy Mar 30 '13 at 20:42
  • @AjaySainy: First of all I want you to go through this official tutorial of inheritance http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html .. And secondly I would suggest you to create your classes in such a way that you use the object of child class instead of super class to make changes in superclass components using proper setter and getter method. In above example Inheritance is doing nothing..only member access is come into play.. – Vishal K Mar 30 '13 at 20:43