raz
raz

Reputation: 492

JTextField is not working as expected in Netbeans IDE

I have one Class named MainFrem

public class MainFrem extends javax.swing.JFrame {

    public MainFrem() {
        initComponents();

    }

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

        ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        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(74, 74, 74)
                .addComponent(ok)
                .addContainerGap(144, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(ok)
                .addContainerGap(70, Short.MAX_VALUE))
        );

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

    private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
        new SecondClass().setVisible(true);
        System.out.println("Ok button Pressed.");
        new SecondClass().FeelIn.setText("Operation Successful.");

    }                                  

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton ok;
    // End of variables declaration                   
}

And the Second Class is

public class SecondClass extends javax.swing.JFrame {
    public SecondClass() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        FeelIn = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        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(48, 48, 48)
                .addComponent(FeelIn, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(FeelIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SecondClass().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    public javax.swing.JTextField FeelIn;
    // End of variables declaration                   
}

I am using NetBeans IDE and most of the code is generated by the IDE. in the class named Mainfrem I wrote the code below and this is the only code is written in the two class by me.

private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
    new SecondClass().setVisible(true);
    System.out.println("Ok button Pressed.");
    new SecondClass().FeelIn.setText("Operation Successful.");

}

In the class named MainFrem I take a JButton and set the Action Listener like as show above, and in class named SecondClass I just add a JTextField named FeelIn. I am trying to press the ok button in the classMainFrem and change the JTextField in the SecondClass as Operation Successful.. The JTextField in the SecondClass is not changing to Operation Successful Is there a way to solve the problem.

Upvotes: 0

Views: 313

Answers (2)

Majora320
Majora320

Reputation: 1351

You are calling new SecondClass() both times. I assume that what you want to do is this:

private void okActionPerformed(java.awt.event.ActionEvent evt) {
    SecondClass frame = new SecondClass()                                   
    frame.setVisible(true);
    System.out.println("Ok button Pressed.");
    frame.FeelIn.setText("Operation Successful.");
}

Upvotes: 1

Ferit
Ferit

Reputation: 9647

private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
    new SecondClass().setVisible(true); // An anonymous SecondClass instance
    System.out.println("Ok button Pressed.");
    new SecondClass().FeelIn.setText("Operation Successful."); // Another anonymous instance?

}

You are just creating new anonymous instances of SecondClass, that's the problem. Basically, you are just creating instances and throwing them away.

Upvotes: 2

Related Questions