> Hai ,
> I want to know how to get the instance
> names and values of components on a frame. if
> any body knows anything on this
> lease help me
> Advance Thanks
What names? The names of the instance variables in the source code?
What values? A component doesn't have 'a value'. Text components have some kind of text and other values. Buttons and labels have texts and icons. A custom component might have a custom value...
-Puce
You can get all the components with a recursive function like...
public void getComponentList(Container container, List<Component> components) {
for (Component component : container.getComponents()) {
if (component instanceof Container)
getComponentList((Container) component, components);
components.add(component);
}
}
Hai Please......
i want to write a method it should accept the Frame object as a parameter .
this method print all the instance name and values of the component on that frame object
example:
Frame fobj=new Frame();
JTextField jTextField1=new JTextField(" ABCD ");
fobj.add(jTextField1);
...
..
.
.
.
Now i want that method to print
name = jTextField1 and its Value=ABCD
like this ....
If 'n' number of components are on that frame it should print all components instance names and values
> JTextField jTextField1=new JTextField(" ABCD ");
> Now i want that method to print
> name = jTextField1 and its Value=ABCD
Components don't have a "name". The name is used in your source program. Once the program is compiled, the computer doesn't care about the name you give it, its just an Object in memory. Have you ever looked in a class file? Do you see the variable names anywhere in the file?
> > JTextField jTextField1=new JTextField(" ABCD ");
> > Now i want that method to print
> > name = jTextField1 and its Value=ABCD
>
> Components don't have a "name". The name is used in
> your source program. Once the program is compiled,
> the computer doesn't care about the name you give it,
> its just an Object in memory. Have you ever looked in
> a class file? Do you see the variable names anywhere
> in the file?
Interesting! I've never looked at class files, but I thought you could get the instance variables with getClass().getDeclaredFields(). Then call getName() on the Field objects to get their names.
But this would work only for instance variables of course. If your components variables are just local variables in a method and then added to the Container, then this reflection approach wouldn't work.
Another technique, and maybe the better one:
Use the 'name' property of the components! I think that some GUI builders like NetBeans' Matisse set them automatically to the name of the instance method. Otherwise set them explicitly.
Then get all components from your container (something like getComponents()) and get their names with getName().
But for the value this is a little bit trickier as I mentioned above. There's no "value" property, since it depends on the type of the component.
If your just interested in the values of text fields, one approach could be to explicitly test the components with something like
if (comp instanceof JTextField){
}
But if you have control over the container, a better approach would be to make a List of JTextFields and then just iterate over that list, getting their names and values.
-Puce
Message was edited by:
Puce
Hai
I had written my code like thisSolve my problem
// this means frame object
java.awt.Component[] components=this.getContentPane().getComponents();
if(components instanceof JTextField){
//getting the value in the text field
components.getText();
//gettting the name of the component
components.getName();
but here were not getting instance name
// we are getting instance names on components like this
but here we are not getting value
java.lang.reflect.Field[] _field;
_field=this.getClass().getDeclaredFields();
_field.getName();
}
Advance Thanks
Hai
I had written my code like thisSolve my problem
// this means frame object
java.awt.Component[] components=this.getContentPane().getComponents();
if(components instanceof JTextField){
//getting the value in the text field
components.getText();
//gettting the name of the component
components.getName();
but here were not getting instance name
// we are getting instance names on components like this
but here we are not getting value
java.lang.reflect.Field[] _field;
_field=this.getClass().getDeclaredFields();
_field.getName();
}
Advance Thanks
Hai
I had written my code like thisSolve my problem
// this means frame object
[b]java.awt.Component[] components=this.getContentPane().getComponents();
if(components[i] instanceof JTextField){[/b]
//getting the value in the text field
components[i].getText();
//gettting the name of the component
components[i].getName();
but here were not getting instance name
// we are getting instance names on components like this
but here we are not getting value
java.lang.reflect.Field[] _field;
_field=this.getClass().getDeclaredFields();
_field[i].getName();
}
Advance Thanks
> //gettting the name of the component
> components.getName();
As I said: If you're not using a GUI builder that sets the 'name' property to the name of the instance variable, you have to do it yourself:
myTextField1.setName("myTextField1");
So the best thing would be to use a GUI builder that synchronizes the 'name' property with the instance variable name. (I think NetBeans does that.)
-Puce
Message was edited by:
Puce
Hai ..
Thanks for guiding me here i had copied my example code i am getting name value null
import javax.swing.*;
import java.awt.*;
import java.util.*;
/**
*
* @author naveen
*/
public class Test extends javax.swing.JFrame {
/** Creates new form RaviTest */
public RaviTest() {
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.
*/
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(jTextField1);
jTextField1.setBounds(180, 60, 100, 20);
getContentPane().add(jTextField2);
jTextField2.setBounds(180, 90, 90, 20);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(170, 140, 81, 26);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
Component[] comp=this.getContentPane().getComponents();
for(int i=0;i<comp.length;i++)
{
if(comp[i] instanceof JTextField)
{
getT((JTextField)comp[i]);
}
}
}
public void getT(JTextField jt)
{
System.out.print("name "+jt.getName());
System.out.println("value "+jt.getText());
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new RaviTest().show();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
OUTPUT AS :
if we enter some text in text box and press the button we get like this
name null value ABCD
name null value EFGH
may be some components will have name but if they names are not set
then we get null value
what i want is at that i want the jTextField1.. as name(ie.. object name) when the name is not set how to do it>
Well, it seems that I was wrong, and NetBeans doesn't synchronize the name property :-(
Still you could set them manually (and maybe document this as a requirement).
Or use the reflection approach to get the names (instance variables only!)
Field[] fields = getClass().getDeclaredFields()
for (Field field : fields){
if (JTextField.class.isAssignableFrom(field.getType())){
string name = field.getName();
JTextField tf = (JTextField) field.get(this);
string value = tf.getText();
}
}
Note: I didn't test that code, but maybe it works with something like that.
-Puce
Message was edited by:
Puce
> Thanks Pauce
> But we are not getting local
> varaibles .how to get when components added during
> runtime
Well, what name do you want when a component is added like this:
addComponent(new JTextField());
or like this:
for (int i=0; i<10; i++){
JTextField field = new JTextField();
addComponent(field);
}
?
This is neither possible using reflection (as I said: instance variables only) nor does it make much sense.
As I suggested before: Always set the name property (document this as a requirement).
-Puce
Swing comes with a built in shortcut for printing the component hierarchy to System.out. This might be more information than you need but will definately contain the name and text value of any components.
Make the frame the focus ancestor and press Ctrl+Shift+F1 to see the output of that frame.
Hope this helps.