How to transfer data from the textfields in one class to the mainclass texa

I have two classes .

Both has the GUI button on it.

The class with main method has a JTextArea and few buttons.

The secong class has Label with textbox along with buttons.

Is there a way by which I can enter data in the second class (GUI) .

The data entered will show in the first Frames JTextArea.

[335 byte] By [maggiemaggiea] at [2007-11-26 18:58:47]
# 1

OK. I guess the two classes you have are two different JFrames.

Well you can create a private member on both classes of type JFrame.

Then you can create a method in both classes called something like "SetOtherJFrame(JFrame frame)" that receives a JFrame as parameter.

Then in that method you set the private class member to the parameter value.

Once you do that, you can access the other form programmatically with all of its members...

You could either set it to receive a myJFrame1 and myJFrame2 type parameter so that it contains all of the objects and methods of your particular JFrame implementation.

I hope this gets useful... let me know if it is not clear enough...

JN

janunezc@cra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thankyou Can u plz give me an example ,I can understand better.
maggiemaggiea at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 3

Well, your question doesn't make any sense, so no we can't help. You have a text area with some buttons and a label and a text box (whats a text box, there is no such component) and some buttons. You haven't described the interaction between the components so the text won't just magically appear in the first frame.

camickra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 4

First, I want to say:

It is clear what a textbox is... it may not be the exact name in JAVA but all developers in the world know what it is.

Second, what I understand from your question is: You have two different JFrames, let''s say myFrame1 and myFrame2 and you want to be able to modify the text of a textbox that is located in myFrame2 (let''s say myFrame2.myJTextField) by pressing a button in myFrame1.

Is it correct?

janunezc@cra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 5

> It is clear what a textbox is... it may not be the exact name in JAVA but

> all developers in the world know what it is.

No it isn't clear what a text box is. A text box holds text. It could be a JTextField, JTextArea, JTextPane. The answer to the question would be different depending on what the OP is talking about.

If the OP wants a complete answer, then the OP needs to learn how to ask a clear concise question with all the details so we don't have to guess what they are talking about. Don't forget this forum is used by many who may not speak English as a first language. So learn to use the proper name of the Object in question so everybody knows what you are talking about.

camickra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

Here is some sample code generated using Netbeans 5.5

FIRST myFrame1

--

/*

* NewJFrame.java

*

* Created on February 19, 2007, 9:39 PM

*/

package printmonitor; //Forget about this line

/**

*

* @author janunezc

*/

public class myFrame1 extends javax.swing.JFrame {

/** Creates new form NewJFrame */

public myFrame1() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jButton1 = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Change myFrame2");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jLabel1.setText("Content for textbox in myFrame2");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.addContainerGap()

.add(jLabel1)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 18, Short.MAX_VALUE)

.add(jButton1)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jLabel1)

.add(jButton1))

.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

myFrame2Ref.setTextBoxContent(this.jLabel1.getText());

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new myFrame1().setVisible(true);

}

});

}

public void setmyFrame2Reference(myFrame2 mf2) {

myFrame2Ref = mf2;

}

private myFrame2 myFrame2Ref = null;

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JLabel jLabel1;

// End of variables declaration

}

SECOND myFrame2

--

/*

* NewJFrame2.java

*

* Created on February 19, 2007, 10:22 PM

*/

package printmonitor; //Forget about this

/**

*

* @author janunezc

*/

public class myFrame2 extends javax.swing.JFrame {

/** Creates new form NewJFrame2 */

public myFrame2() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jButton1 = new javax.swing.JButton();

jTextField1 = new javax.swing.JTextField();

jButton1.setText("jButton1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextField1.setText("This text will be changed by myFrame1");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.addContainerGap()

.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new myFrame2().setVisible(true);

}

});

}

public void setTextBoxContent(String content) {

this.jTextField1.setText(content);

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JTextField jTextField1;

// End of variables declaration

}

THIRD: Main

/*

* Main.java

*

* Created on February 15, 2007, 10:25 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package printmonitor;//forget about this line

/**

*

* @author janunezc

*/

public class Main {

/** Creates a new instance of Main */

public Main() {

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

myFrame1 mf1 = new myFrame1();

myFrame2 mf2 = new myFrame2();

mf1.setmyFrame2Reference(mf2);

mf2.setVisible(true);

mf1.setVisible(true);

}

}

FOURTH

--

Let me know if it is clear enough now... start with the main class to understand what it does. Basically it creates a frame myFrame1 and another myFrame2

Then runs a method in myFrame1 to let it know what is the object where the text will be transfered to...

Finally it opens both forms...

MyFrame2 implements a method to expose access to external callers to modify content of jTextField1

MyFrame1 calls such method.

Let me know if this was useful or if it just does not make any sense.

JN

janunezc@cra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 7
Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
camickra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 8

Try this for ideas

package testing;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

public class Test

{

public static void main(String[] args)

{

JFrame frame = new JFrame("Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JTextField input = new JTextField(20);

final JTextField output = new JTextField(20);

input.getDocument().addDocumentListener(new DocumentListener()

{

public void insertUpdate(DocumentEvent de) { output.setText(input.getText()); }

public void removeUpdate(DocumentEvent de) { output.setText(input.getText()); }

public void changedUpdate(DocumentEvent de) {}

});

frame.add(input, BorderLayout.NORTH);

frame.add(output, BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

}

#

duckbilla at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 9

> Try this for ideas

If you just want the text to show up in two components, then you would share the model:

JTextField textField1 = new JTextField();

JTextField textField2 = new JTextField( textField1.getDocument() );

But the OP talks about text areas and text boxes, which is why I asked for clarification. Don't waste time guessing what the OP wants. The OP needs to learn to ask a clear question.

camickra at 2007-7-9 20:39:30 > top of Java-index,Desktop,Core GUI APIs...
# 10
What is an OP? Or would I rather not know?!
clarkana at 2007-7-9 20:39:31 > top of Java-index,Desktop,Core GUI APIs...
# 11
Original Poster
camickra at 2007-7-9 20:39:31 > top of Java-index,Desktop,Core GUI APIs...
# 12
ThankyouIt worked.
maggiemaggiea at 2007-7-9 20:39:31 > top of Java-index,Desktop,Core GUI APIs...