Search code examples
javaswingconstructorjtextfieldswingworker

how to pass textfield from main class to another class


How to pass the textfield from main class pri to CounterTask1 class.

Below program is an example. Real program contains similar construction.

In CounterTask1 class the textfield add with another string.If click print button it should print textfield in terminal.

Advance Thanks.

import java.io.*;
import javax.swing.*; 
import java.awt.event.*;  
import javax.swing.SwingWorker;
import java.util.List;
public class pri 
{
 JFrame Frame1 = new JFrame();
 JLabel SourceLabel1    = new JLabel("Source Name"); 
 JTextField SourceField1 = new JTextField(20);
 public void MainFrame()        
 {
  final CounterTask1 task1 = new CounterTask1();
  Frame1.setLayout(null);    
  JButton Print = new JButton("Print");
  Print.setBounds(10,10,100,30);
  Print.addActionListener(new ActionListener()
  { public void actionPerformed(ActionEvent ae)
    { String sourcename=SourceField1.getText();
      System.out.println("Printing in Terminal "+sourcename);
      task1.execute();          } });

  JButton Exit =  new JButton("Exit");
  Exit.setBounds(10,50,100,30);
  Exit.addActionListener(new ActionListener()
  { public void actionPerformed(ActionEvent ae)
    { System.exit(0); } }); 

        SourceField1.setBounds(130,10,100,30);
        Frame1.add(SourceField1);
        Frame1.add(Print);
        Frame1.add(Exit);
        Frame1.pack();

        Frame1.setSize(250,150);
        Frame1.setLocation(100,100);
        Frame1.setVisible(true);
        Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} //MainFrame

public static void main(String[] args)
{   SwingUtilities.invokeLater(new Runnable()
    { public void run()
        {
            pri frame = new pri();
            frame.MainFrame();  }   });
}
  } 

class CounterTask1 extends SwingWorker<Integer, Integer> 
 {
protected Integer doInBackground() throws Exception
 {

        String one = SourceField1.getText();
        String two = "Thanks !";
        String Addst = one +two ;
        System.out.println("printing in Task" + Addst);
        return 0;

  }// protected main class

protected void process(List<Integer> chunks)
{
    System.out.println(chunks);  
}

} // counter task

Solution

  • I would do things differently -- pass the text to the SwingWorker in its constructor:

      Print.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            String sourcename = SourceField1.getText();
            System.out.println("Printing in Terminal " + sourcename);
    
            // note change in constructor
            // this way getText() is called on the EDT.
            CounterTask1 task1 = new CounterTask1(SourceField1.getText());
            task1.execute();
         }
      });
    

    and in the other class:

    class CounterTask1 extends SwingWorker<Integer, Integer> {
       private String text;
    
       public CounterTask1(String text) {
          this.text = text;
       }
    
       protected Integer doInBackground() throws Exception {
    
          String one = text;
          String two = "Thanks !";
          String Addst = one + two;
          System.out.println("printing in Task" + Addst);
          return 0;
       }
    

    Note:

    • If you need the second class to call methods of the first, then you will want to pass a reference of first into second, similar to how I am passing a String above.
    • Be sure not to make Swing calls from the doInBackground() method.
    • Learn and adhere to Java naming convention so that we can better understand your future code posts. Class names should start with a capital letter and field, variable and method names with a lower-case letter.