Question

I am having a hard time figuring out what to do with it. For example, I have this on another class:

public class Methods {

    private String EmployeeName;
    private int SalaryDeduction;
    private int IDCard;

    public void setEmpName(String name) {
        this.EmployeeName = name;
    }

    public String getEmpName() {
        return EmployeeName;
    }

    public void setSalaryDed(int sdvalue) {
        this.SalaryDeduction = sdvalue;
    }

    public int getSalaryDed() {
        return SalaryDeduction;
    }

    public void setIDCard(int idcard) {
        this.IDCard = idcard;
    }

    public int getIDCard() {
        return IDCard;
    }

}

I need to use the above methods unto my next class. For example, the SetEmpName method to acquire the contents of my EmployeeName JTextField and then return it via GetEmpName with a button click to display it below the textfield. It will look like:

Employee Info: James

Button Click

Employee Info: James

"Hello James"

EDIT

I already know how my Set methods work, thanks man. So far, I am in dire need of knowledge on how to get my Get methods working with a click of a button. This is my program so far.

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

public class MainFrame extends JFrame implements ActionListener  
 {

   //UI Components Declaration


   JLabel EmpInfL = new JLabel("Employee Info:");
   JLabel SalDedL = new JLabel("Salary Deduction:");
   JLabel IDCL = new JLabel("ID Card");

   JTextField EmpInfTF = new JTextField(8);
   JTextField SalDedTF = new JTextField(9);
   JTextField IDCTF = new JTextField(8);

   JButton OkB = new JButton();
   JButton ClearB = new JButton();

   String EmployeeName
   int SalaryDeduction;
   int IDCard;

   //Constructor
   public MainFrame() {

   super("Programming Activity");
   setSize(300,100);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
   setLayout(new FlowLayout());

   add(EmpInfL);
   add(EmpInfTF);


   add(SalDedL);
   add(SalDedTF);


   add(IDCL);
   add(IDCTF);


   OkB.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
   Methods SetGet = new Methods();

   SetGet.setEmpName(EmpInfTF.getText());


   }


      public static void main(String [] args)
         {

        new MainFrame();
         }
}
Was it helpful?

Solution

As I see you're trying to use Swing. If you are using something like actionListeners, then, you can get the string from JTextField.getText() and store it into setEmpName:

something like this:

Methods yourObject = new Methods(); yourObject.setEmpName(EmployeeName.getText()) in your actionListener method.

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

public class MainFrame extends JFrame implements ActionListener  
 {

   //UI Components Declaration


   JLabel EmpInfL = new JLabel("Employee Info:");
   JLabel SalDedL = new JLabel("Salary Deduction:");
   JLabel IDCL = new JLabel("ID Card");
   JLabel sayHelloLabel = new JLabel("");
   JTextField EmpInfTF = new JTextField(8);
   JTextField SalDedTF = new JTextField(9);
   JTextField IDCTF = new JTextField(8);

   JButton OkB = new JButton();
   JButton ClearB = new JButton();

   String EmployeeName
   int SalaryDeduction;
   int IDCard;

   //Constructor
   public MainFrame() {

   super("Programming Activity");
   setSize(300,100);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
   setLayout(new FlowLayout());

   add(EmpInfL);
   add(EmpInfTF);


   add(SalDedL);
   add(SalDedTF);


   add(IDCL);
   add(IDCTF);
   add(sayHelloLabel);

   OkB.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e)
   {
   Methods SetGet = new Methods();

   SetGet.setEmpName(EmpInfTF.getText());
   sayHelloLabel.setText("Hello, "+SetGet.getEmpName());

   }


      public static void main(String [] args)
         {

        new MainFrame();
         }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top