ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
but how come in java , the name of a button is the text caption of it ?? how to get the "name" of this jbutton which is button01 , kind of like a unique name(though not exactly) for a component ...
i do this in public void actionPerformed(ActionEvent evt
am i doing something wrong or
what is the correct way to use this method or
are there other better methods to get that bloody "button01" in strings ??
//thanks in advance(dont laugh)
REASON EDIT ::you know what ?? ... "php" is damn cool ... ^_^
A button is a button; it has no "name" (what are you talking about?). You can have variables point to a Button object, but that has nothing to do with the button itself. You can test to see if your button is the same as the one that is being pointed to by "button01":
>> "A button is a button; it has no "name" (what are you talking about?) ..."
language problem i believe , it should be "the name of a field" or "how to return a name in strings format of a given field" but i could be wrong too , for i'm just a few days old in java ...
your solution is correct and it works , but i found another solution through java reflection to *just* get that bloody name of a button as strings ... arrrrrrrggggh ... hopes that its not too expensive ...
both button01.setActionCommand("aString"); and if (evt.getSource() == button01) work and probably faster but i think strings for ActionCommand could be use for other tricks ...
below is the trick that i tried ::
Code:
import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
...
public JButton button01;
...
public String getPerformedComponent(Object PerformedObj)
{
Component comp1;
String s1 , s2 ;
s2 = "" ;
s1 = "" ;
try {
Class c1 = Class.forName("nameOfthisClass");
// Constructor construct[] = c1.getDeclaredConstructors();
Field f1[] = c1.getDeclaredFields();
for (int i = 0; i < f1.length; i++)
{
s1 = f1[i].getType().getName();
if (s1.lastIndexOf(".") > 0) {
s1 = f1[i].getType().getName().substring(s1.lastIndexOf(".")+1,s1.length());}
Object obj = f1[i].get(this);
if(obj instanceof Component)
// return (Component)obj ;
if((Component)obj == PerformedObj) {
System.out.println("===================== found PerformedObj start ...");
System.out.println(f1[i].getName()+" of <"+s1+">");
s2 = f1[i].getName();
System.out.println("===================== found PerformedObj end ...");
continue ;
}
System.out.println(f1[i].getName()+" of <"+s1+">");
}
} catch (ClassNotFoundException ERROR) {
System.out.println("this ClassNotFoundException ==> another puff of cigarette ...");
}
catch (IllegalAccessException ERROR) {
System.out.println("this IllegalAccessException ==> reinsert a better piece of music cd ...");
}
return s2;
}
public void actionPerformed(ActionEvent evt){
String result = "" ;
...
result = getPerformedComponent(evt.getSource());
JOptionPane.showMessageDialog(null , "getPerformedComponent(Object PerformedObj):string; \n\r\n\r== "+result , "...",JOptionPane.INFORMATION_MESSAGE);
...
}
note :: could have some mistakes , i have to delete the extras ...
Using reflection a lot can slow down the program execution. If your program isn't big, using lots of interfaces and exposing services, I suggest that you avoid reflection.
just found another method without reflection(i think) ...
Code:
...
public JFrame frame;
public JPanel panel1,panel2,panel3;
...
public String getPerformedComponentC(Container container , Object PerformedObj){
String s2= "" ;
int count = container.getComponentCount();
//could probably be nested further more to do a GlobalFindComp
for (int i=0; i<count; i++) {
Component comp = container.getComponent(i);
if(comp == PerformedObj) {
s2 = comp.getName();
System.out.println("... found PerformedObj ===> " + s2);
break;
}
}
return s2;
}
public String do_fetch(String L) {
String title = L;
JButton button[];
button = new JButton[10];
panel1.setName("panel1");
frame = new JFrame(title);
panel1 = new JPanel();
//could probably be nested further more eg. JPanel panel[];
for (int i = 0; i<button.length ; i++) {
button[i] = new JButton("??");
button[i].setName("button"+(i+1));
button[i].setSize(80,32);
button[i].addActionListener(this);
panel1.add(button[i]);
}
panel1.setLayout(new GridLayout(0,1,10,5));
frame.getContentPane().add(panel1, BorderLayout.NORTH);
frame.setSize(100, 480);
frame.setVisible(true);
return L ;
}
public void actionPerformed(ActionEvent evt){
JOptionPane.showMessageDialog(null, "getPerformedComponent(Object PerformedObj):string; \n\r\n\r== "+
getPerformedComponentC(panel1, evt.getSource()) , "...",JOptionPane.INFORMATION_MESSAGE);
//probably give a frame.getContentPane() if global find ...
}
btw ... during the process i hit upon a c#(c sharp ??) snipplet ...
Code:
private int counter;
private int locY;
private void btnAdd_Click(object sender, System.EventArgs e)
{
counter += 1;
locY += this.btnAdd.Height + 2;
Button myButton = new Button();
myButton.Name="Button " + counter;
myButton.Text="Button " + counter;
//set the x location same as btnAdd, and y = locY
myButton.Location= new Point(btnAdd.Location.X, locY);
//set its MouseEnter, MouseLeave and Click events
myButton.MouseEnter += new System.EventHandler(this.btn_MouseEnter);
myButton.MouseLeave += new System.EventHandler(this.btn_MouseLeave);
myButton.Click += new System.EventHandler(this.btn_Click);
//add this button to the form
this.Controls.Add(myButton);
}
arrrrgggghhhh ... days and days of music(specifically tang dynasty , the band , a china band) ... still havent pass that bloody button !!!!
note :: could have some mistakes which i wouldnt know ...
Going back to your first post, button01 is a variable name. It is an object of type JButton.
All objects wont have a name, in this case there is one.
Subsequently, you refer to an object by its variable name only. If your are "few days old in java", reflection is not
the correct thing to be mastering.
Everytime somebody calls out your name, you dont go about finding out your name first... you KNOW your name. Same in
programming. You refer to variable names, and they know what you want to do.
i think my specific intention is after knowing that somebody is going to call without firstly giving "a unique name(though not exactly)" then i need to do something , etc , etc , etc and then only allow them to do what had been presumingly unilaterally or bilaterally(usually in the first case) agreed upon before-hand within the owner's panels/containers/or_whatever ... preferably we are able to set/get their declared(or not) fields or whatever ...
just an intention ... frankly speaking , i'm not too sure about what i'm talking about either ... ^_^
infact i'm kind of going into this JFrame with some kind of Listener now , one thing at a time ... slowly taking my own sweet time i guess , probably will be back to the above mentioned intention much much later on ...
//i think i will like this java ... just for the fun of it ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.