简体   繁体   中英

How can I set a textfield in a jframe to setEditable(true) by pressing a button in another jframe?

I have tried this on my other jframe:

public class frmB extends javax.swing.JFrame {

    public frmB() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
        frmA a = new frmA();
        a.jTextField1.setEditable(true);
    }
  1. You will want to learn and stick to Java naming conventions: class names should begin with an upper case letter for one. Following these conventions will make your code much easier for others (such as us ) to understand, and thus easier for others to help you.
  2. When you create a new frmA object in the jButton1ActionPerformed method, you're doing just that, creating a new frmA object, one that is completely unrelated to the other one that is displayed.
  3. To call methods on the one that is displayed, you need a valid reference to it, and this can be obtained by passing this reference in via a constructor or setter method parameter.
  4. Whatever you do, do not try to solve this by making any methods or variables static. That will break your application in the long run.
  5. Later we'll discuss why it's not a good idea to have multiple JFrames displaying in a single application.

Note that your problem has nothing to do with Swing and all to do with basic Java -- any class can have multiple instances made from it, and each one will be unique and fully distinct from the other. Changing the state of one instance will have no effect on any of the others.


You ask if I can make code out of it, and so sure, here's an example.

Say you have a class A that has a JTextField called textField and a method called setText(String text) that takes the String passed in and places it into the textField:

// this is in class A
public void setText(String text) {
  textField.setText(text);
}

And your situation you have a class B1 with a JButton that tries to call a method of A, but does it by creating a new A object like so:

   button.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent arg0) {
        A a = new A();   // creates a new A object
        a.setText("Hello from B1!");  // and calls its method
     }
   });

But again, this won't work since the code above creates a new A object which is not the same as the displayed A. The key is to pass a reference of A into this class and then call the method off of it, like my B2 class so:

class B2 extends JPanel {
   private JButton button = new JButton("Press ME");
   private A a;  // field to hold a reference to A object

   public B2(final A a) {  // constructor parameter to get A refrence
      this.a = a;    // assign reference
      setBorder(BorderFactory.createTitledBorder("B2"));
      add(button);      
      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            a.setText("Hello from B2!");  // now can use the reference in program
         }
      });
   }
}

A runnable example program:

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class Foo {
   public static void main(String[] args) {
      A a = new A();
      B1 b1 = new B1();
      B2 b2 = new B2(a);

      JPanel panel = new JPanel(new GridLayout(0, 1));
      panel.add(a);
      panel.add(b1);
      panel.add(b2);

      JOptionPane.showMessageDialog(null, panel);
   }
}

class A extends JPanel {
   private JTextField textField = new JTextField(10);

   public A() {
      add(textField);
      textField.setEditable(false);
      textField.setFocusable(false);
      setBorder(BorderFactory.createTitledBorder("A"));
   }

   public void setText(String text) {
      textField.setText(text);
   }
}

class B1 extends JPanel {
   private JButton button = new JButton("Press ME -- this does nothing");

   public B1() {
      add(button);
      setBorder(BorderFactory.createTitledBorder("B1"));
      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            A a = new A();
            a.setText("Hello from B1!");
         }
      });
   }
}

class B2 extends JPanel {
   private JButton button = new JButton("Press ME -- this works!!");
   private A a;

   public B2(final A a) {
      this.a = a;
      setBorder(BorderFactory.createTitledBorder("B2"));
      add(button);      
      button.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            a.setText("Hello from B2!");
         }
      });
   }
}

为什么在要让它们都将frmA作为参数传递给frmB的类中创建一个新的jframe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM