• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get JTextField value from another class  RSS feed

 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

how to get JTextField value from another class. I tried through getmethod. then also i didnt get.



Thanks
murali

vertizone
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual way to do it is via an instance to which you got a reference.
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohan cheepu wrote:Hi,

how to get JTextField value from another class. I tried through getmethod. then also i didnt get.

vertizone


ya, use get, thats right.. but use get to return a String...
like this :



would you mind posting your "get" method here? we can know why it didn't work..

 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mr shrinath,


I will try it


regards
murali

 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i tried , it didnt work. it gets string default value (ex:String s="hello") when declare the string object.but it didnt JTextField data. it gives exception is Null pointer ;



regards
murali
 
Allen Leggett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan:
I ~believe~ your post is like mine. Are you saying, "MyClass1" wants to get a jTextField from another SWING App. called "MyClass2"?
This is my problem and not answered to my satisfaction. Perhaps we can work together for an answer, please?

Kind regards,
Allen
 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

[MG:] Removed mail contact info.
http://faq.javaranch.com/java/UseTheForumNotEmail

regards
murali
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> yes, exactly. i am online in gmail. xxxxxxxx [MG]Removed mail contact info in quote

we can assume, then, that you require no further help on this topic (as it is now non-forum)
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may be you both are trying to patch the data through two different applications running two different jvm..?
why dont you invoke the other swing(which has jtextfield) from your main program and see that it is running while you collect your value?
because, the null pointer is thrown when swingApp1 tries to access data in swingApp2 after swingApp2 died..
so keep both alive, if you dont want swingApp2 to be shown on the screen, set its visibility false and access its data through swingApp1..

I'd like to see the relevant parts of your code, incase you needed help..
 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

How do i get the JTextField of Main class value to second class

i want to display value in JTextField in secondclass

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



public class Main implements ActionListener

{

JFrame jf;
JTextField jt;
Main()
{


}
public static void main(String args[])
{
Main m=new Main();
m.form();
}

void form()

{
jf=new JFrame("test");
Container cp=jf.getContentPane();
cp.setLayout(null);

jt=new JTextField();

cp.add(jt);
jt.setBounds(50,50,100,30);
jf.setSize(400,200);
jf.setVisible(true);
jf.setResizable(false);

}

public void actionPerformed(ActionEvent ae)
{}
public String get()
{
return jt.getText();

}
}




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



public class second implements ActionListener
{

JFrame jf;
JTextField jt;
JButton jb;
Main m;
Solar()
{
}
public static void main(String args[])
{
second s=new second();
s.form();


}

void form()

{
jf=new JFrame("test");
Container cp=jf.getContentPane();
cp.setLayout(null);
jt=new JTextField();
jb=new JButton();
jb.addActionListener(this);

cp.add(jt);
cp.add(jb);
jb.setBounds(180,180,50,50);
jt.setBounds(50,50,100,30);
jf.setSize(400,400);
jf.setVisible(true);
jf.setResizable(false);


}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb)
{


}


m=new Main();
jt.setText(m.get());
}

}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> How do i get the JTextField of Main class value to second class
> i want to display value in JTextField in secondclass

your attempt at doing it, tries to get the value of second() to Main(),
so I've changed the code to do that (modify if you want it the other way around)

(code tags pointless)

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
class Main
{
JFrame jf;
JTextField jt;
void form()
{
jf=new JFrame("test");
Container cp=jf.getContentPane();
cp.setLayout(null);
jt=new JTextField();
cp.add(jt);
jt.setBounds(50,50,100,30);
jf.setSize(400,200);
jf.setVisible(true);
jf.setResizable(false);
}
}
class second implements ActionListener
{
JFrame jf;
JTextField jt;
JButton jb;
Main m;
public static void main(String args[])
{
second s=new second();
s.form();
}
void form()
{
jf=new JFrame("test");
Container cp=jf.getContentPane();
cp.setLayout(null);
jt=new JTextField();
jb=new JButton();
jb.addActionListener(this);
cp.add(jt);
cp.add(jb);
jb.setBounds(180,180,50,50);
jt.setBounds(50,50,100,30);
jf.setSize(400,400);
jf.setVisible(true);
jf.setResizable(false);
}
public void actionPerformed(ActionEvent ae)
{
m=new Main();
m.form();
m.jt.setText(jt.getText());
}
}

also note you can set the textfield's documents the same, so that changes in either are reflected in the other.
 
mohan cheepu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi

Thanks Michael for helping.





regards
murali



 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whatever Michael Dunn said, plus, i've to add something more.. whatever your "form()" is doing, it is the constructor's job according to me.. so i propose it should be in the constructor.. not cumpolsorily, but good way of coding.. or maybe just my thought..

glad that its solved now :) :thumbup:
 
Allen Leggett
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael:
Would you please take a look at http://www.coderanch.com/t/451512/IDEs-Version-Control-other-tools/Returning-value-from-Netbeans-SWING

My apologies,
Allen Leggett
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't help with any Netbeans stuff, but the basics of what you're trying to do is to have a method() return the result of the search.
This method() could be in another class, perhaps just a method of the main class, but whichever way you do it, the query cannot be allowed
to be run from the Swing thread (EDT), otherwise you'll block all events/repainting until the method returns.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!