JTextField cannot capture value from different classes

Hi all,

The solution is it have two different classes, class 1 and class 2. I wan to send a value from class 1 to the JTextField in the class 2. I had try many times, but no idea for it. The value can send from class 1 but textfield in class 2 cannot receive. Anyone got idea. Below is the code:

Class 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class1 implements ActionListener {

private JFrame frame = new JFrame();

private JLabel lblEmpId = new JLabel("Employee ID :");

private JTextField txtEmpId = new JTextField(10);

private JPanel pnlMain = new JPanel();

private JButton btnConfirm = new JButton("Confirm");

public Class1() {

btnConfirm.setActionCommand("Confirm");

btnConfirm.addActionListener(this);

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

public JPanel createContentPane() {

pnlMain.add(lblEmpId);

pnlMain.add(txtEmpId);

pnlMain.add(btnConfirm);

return pnlMain;

}

//Button action command

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if("Confirm".equals(command)) {

submitResult();

}

}

//Submit search result

public void submitResult() {

Class2 class2 = new Class2();

String result = new String();

result = txtEmpId.getText();

class2.sendResult(result);

}

public static void main(String args[]) {

new Class1();

}

}

Class 2

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class2 {

private static JFrame frame = new JFrame();

private static JTextField txtEmpId = new JTextField(10);

private static JPanel pnlMain = new JPanel();

public Class2() {}

public static JPanel createContentPane() {

pnlMain.add(txtEmpId);

return pnlMain;

}

public void sendResult(String result) {

System.out.println("Result here = "+result);

txtEmpId.setText(result);

}

public static void main(String args[]) {

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

}

Thanks in advance.

[2683 byte] By [arjensena] at [2007-11-27 11:54:26]
# 1

Hi there,

I'm also a newbie in java, but perhaps I can help you.

Why you have 2 "main" method ?

I assume you start your code from Class1.

If like that, remove all the code inside "main" method of Class2, put inside the Constructor of Class2 --> public Class2(){}

And try to run again from Class1

Thunder_Blasta at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

I had so much free time so i modified your code:

class1:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class1 implements ActionListener {

private JFrame frame = new JFrame();

private JLabel lblEmpId = new JLabel("Employee ID :");

private JTextField txtEmpId = new JTextField(10);

private JPanel pnlMain = new JPanel();

private JButton btnConfirm = new JButton("Confirm");

public Class1() {

btnConfirm.setActionCommand("Confirm");

btnConfirm.addActionListener(this);

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

public JPanel createContentPane() {

pnlMain.add(lblEmpId);

pnlMain.add(txtEmpId);

pnlMain.add(btnConfirm);

return pnlMain;

}

//Button action command

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if("Confirm".equals(command)) {

submitResult();

}

}

//Submit search result

public void submitResult() {

Class2 class2 = new Class2();

String result = new String();

result = txtEmpId.getText();

class2.createUI(); // added a code here

class2.sendResult(result);

}

public static void main(String args[]) {

new Class1();

}

}

class2:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class2 {

private static JFrame frame = new JFrame();

private static JTextField txtEmpId = new JTextField(10);

private static JPanel pnlMain = new JPanel();

public Class2() {}

public static JPanel createContentPane() {

pnlMain.add(txtEmpId);

return pnlMain;

}

public void sendResult(String result) {

System.out.println("Result here = "+result);

txtEmpId.setText(result);

}

public void createUI() { // change the name because you had two main

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

}

Yannixa at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hi,

I change code for the Class 2.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class2 {

private JFrame frame = new JFrame();

private JTextField txtEmpId = new JTextField(10);

private JPanel pnlMain = new JPanel();

public Class2() {

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

public JPanel createContentPane() {

pnlMain.add(txtEmpId);

return pnlMain;

}

public void sendResult(String result) {

System.out.println("Result here = "+result);

txtEmpId.setText(result);

}

}

In this situation, when each time click the button in Class 1, it will create a new object but i only wan remain a single object and the latest result display at this same object. Got any idea?

arjensena at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

i modified it again. it should work fine now

class1:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class1 implements ActionListener {

private JFrame frame = new JFrame();

private JLabel lblEmpId = new JLabel("Employee ID :");

private JTextField txtEmpId = new JTextField(10);

private JPanel pnlMain = new JPanel();

private JButton btnConfirm = new JButton("Confirm");

private boolean loaded;

private Class2 class2;

public Class1() {

setLoaded(false);

btnConfirm.setActionCommand("Confirm");

btnConfirm.addActionListener(this);

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

public JPanel createContentPane() {

pnlMain.add(lblEmpId);

pnlMain.add(txtEmpId);

pnlMain.add(btnConfirm);

return pnlMain;

}

//Button action command

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if("Confirm".equals(command)) {

submitResult();

}

}

//Submit search result

public void submitResult() {

if(isLoaded() == false)

class2 = new Class2(this);

String result = new String();

result = txtEmpId.getText();

class2.sendResult(result);

}

public boolean isLoaded(){

return loaded;

}

public void setLoaded(boolean value){

loaded = value;

}

public static void main(String args[]) {

new Class1();

}

}

class2:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Class2 {

private JFrame frame = new JFrame();

private JTextField txtEmpId = new JTextField(10);

private JPanel pnlMain = new JPanel();

public Class2(final Class1 c) {

c.setLoaded(true);

Container contentPane = frame.getContentPane();

pnlMain = createContentPane();

contentPane.add(pnlMain);

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.addWindowListener(new WindowListener() {

public void windowActivated(WindowEvent e) {

}

public void windowClosed(WindowEvent e) {

}

public void windowClosing(WindowEvent e) {

c.setLoaded(false); // set back to false if window is close

}

public void windowDeactivated(WindowEvent e) {

}

public void windowDeiconified(WindowEvent e) {

}

public void windowIconified(WindowEvent e) {

}

public void windowOpened(WindowEvent e) {

}

});

frame.pack();

frame.setSize(350,350);

frame.setVisible(true);

}

public JPanel createContentPane() {

pnlMain.add(txtEmpId);

return pnlMain;

}

public void sendResult(String result) {

System.out.println("Result here = "+result);

txtEmpId.setText(result);

}

}

Yannixa at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi Yannix, ur code are running well, thx. I have 1 question that is why the previous class 2 object auto dispose? how u make for it?

arjensena at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Hi Yannix, ur code are running well, thx. I have 1

> question that is why the previous class 2 object auto

> dispose? how u make for it?

what do you mean by auto dispose?

can you explain more?

Yannixa at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 7

In the class 1, when each time click the button in class 1, it execute

public void submitResult() {

Class2 class2 = new Class2();

..

}

When click the button again, the previous class 2 object dispose and it create a new object. So my ques is how do u dispose the previous obj?

arjensena at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 8

ok! i think i got the idea you need to add a method in your class2 like

public void disposeMe(){

frame.dispose();

}

call it in your class1 when class2 already exist.

Yannixa at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...
# 9

I suggest you learn how to use the "code" formatting tags if you want help in the future.

Do you notice how the code posted by yannix is much easier to read than your unformatted code. It only takes a few minutes to learn how to use the forum correctly.

camickra at 2007-7-29 18:55:54 > top of Java-index,Desktop,Core GUI APIs...