2

I have two class, one is for swing and main class, one is for actionperformed method

//class swing
package system;
import javax.swing.*;
import java.awt.event.*;

public class GUI{
   JFrame frame = new JFRame("frame");
   JTextField tx = new JTextField(10);
   JButton bt = new JButton("button");

   public void getTx(){
      return tx.getText();
   }

   public void init(){
      frame.setSize(200, 200);
      frame.add(tx);
      frame.add(bt);
      tx.setBounds(20, 20, 140, 50);
      bt.setBounds(20, 100, 120, 40);
      btaddcom.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            control.btclicked(e);
        }
    });
   }

   public GUI(){
      init();
   }

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

below is my other class

package system;
import java.awt;
import java.awt.event.*;

public abstract control implements ActionListener{
    public static void btclicked(ActionEvent e){
         GUI gui = new GUI();
         String txf = gui.getTx();
         JOptionPane.showMessageDialog(null, txf);
    }
}

My question is why I cannot get the value from JTextField tx, because it is always blank whenever text I filled it. Thanks

sytherion
  • 55
  • 1
  • 4
  • 4
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Aug 09 '13 at 02:18
  • Please learn java naming conventions and stick to them. – kleopatra Aug 24 '13 at 16:29

3 Answers3

4

You're making a new instance of the GUI class, so all the fields will be empty/reset.

Here's what you should do:

public abstract control implements ActionListener{
    public static void btclicked(ActionEvent e, GUI gui){
        String txf = gui.getTx();
        JOptionPane.showMessageDialog(null, txf);
    }
}

Usage:

btaddcom.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        control.btclicked(e, GUI.this);
    }
});
3

You are creating a new instance GUI. This has no relation ship to what is begin displayed on the screen, so any updates you try to make to it won't be reflected on the screen...

Instead, you should be passing either some kind of model (preferrabbly) or a reference to the GUI or JTextField.

Something more like...

public abstract control implements ActionListener{
    public static void btclicked(ActionEvent e, GUI gui){
         String txf = gui.getTx();
         JOptionPane.showMessageDialog(null, txf);
    }
}

For example.

Personally, I prefer to provide a model of some kind, as it prevent exposing additional content to the caller, as your btclicked method now has full control over your GUI class, which you may not want

MadProgrammer
  • 336,120
  • 22
  • 219
  • 344
0

It seems as if this was solved if I read the comments correctly. But just in case, there's also a basic syntax error. Your getTx() method is returning a string but you have it listed as void. I'm sure this was caught right away but I figure it doesn't hurt to point it out just in case. Cheers.

public String getTx(){
    return tx.getText();
}
K.S.
  • 123
  • 1
  • 8