问题
===EDIT MONTHS AFTER ORIGINAL POST===
The question was motivated by a need to know which cell of an 11x11 grid (array of JTextField) was active when a button was clicked. I had asked the wrong question and had chosen the wrong tool (setActionCommand), though it worked at the time via string manipulation. (I finally chose to completely rewrite what was a horribly-convoluted example of tortured code.)
A better solution than using setActionCommand() is the Answer below that uses .setName().
================================================
I do this for JTextField cell
:
cell.setActionCommand("55");
I push cell
onto a stack named staq
and later pop
it off in a method with the following:
JTextField f = staq.pop(); \\ this works fine
System.out.println(f.command); \\ this gives error mentioned below
Error: "command has private access in JTextField"
In Netbeans watch window for f
, I am able to watch f.command
and there's the "55". But there's no getCommand, no getActionCommand, nothing for JTextField
that returns a String
that could contain "55".
So it is that I ask:
(a) what's the point of setActionCommand
for JTextField
and/or
(b) how do you get its contents?
(Last week I was able to get at "command" via text manipulation of evt.getComponent().toString()
but there's no getComponent() for JTextField
nor does anything else appear to have promise.)
(I'm back to feeling stupid and frustrated. Maybe my design's just what's stupid.)
(And maybe the fact that I can't add a tag for setActionCommand since my reputation isn't 1500 [missed it by a mere 1475] is my clue that I'm biting off more than I can chew from the wrong part of the horse before the cart that's headed over the dam under the bridge.)
回答1:
Consider situation when you add to your JTextField following listener. I can add same listener to another JTextField (which has action command set to 77). This way in ActionListener I can recognize which one triggered the event.
myTextField.addActionListener(new TestActionListener());
mySecondTextField.addActionListener(new TestActionListener());
public class TestActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
if(event.getActionCommand().equals("55")){
//Source of you action is your JTextField
System.out.println(((JTextField)event.getSource()).getText());
}
if(event.getActionCommand().equals("77")){
//There might be different component having this set to 77 using same ActionListener
}
}
}
回答2:
If you add ActionListener to this JTextField it will have to have implemented public void actionPerformed(ActionEvent event) method.
Then if you're adding same action listener to multiple JTextFields you can recognize them in actionPerformed by
event.getActionCommand() which in this case would return 55 if your JTextField was the source of the Action.
Of course alternatively you can test for your JTextField by (depends if you have a reference to all your text fields that might be a source of the action):
if (event.getSource().equals(yourJTextField){
/// implement here how action on JTextField should be handled
}
回答3:
There is no getActionCommand
for a JTextField
. If setActionCommand
is used to set a "commandstring" for the field, the only way I've seen to access that string is for a KeyPressed
event to be thrown for that field. Then, the string literal "command=" will be a substring of getComponent().toString())
for the event.
The format of that (very long) string is such that the first character of the "commandstring" follows the substring "command=". The "commandstring" is followed by a comma. The code below will get the job done if evt.getComponent().toString())
is passed to it, where evt
is the event parameter.
public String commandToCell(String s){
int start = s.indexOf("command") + 8;
int end = s.indexOf(",", start);
return s.substring(start,end);
}
One then wonders why there exists setActionCommand
for a JTextfield
in the first place.
回答4:
To accomplish the goal of identifying which cell in an 11x11 grid an event occurred in, how about the .setName()
method of JTextField
?
static JTextField[][] cells;
...
cells = new JTextField[11][11];
...
cells[i][j] = new JTextField();
cells[i][j].setName("" + Integer.toHexString(i).charAt(0)
+ Integer.toHexString(j).charAt(0));
...
staq.push(cells[someRow][someCol]);
...
static JTextField cell;
...
cell = staq.pop();
int row = Integer.parseInt("" + cell.getName().charAt(0), 16);
int col = Integer.parseInt("" + cell.getName().charAt(1), 16);
来源:https://stackoverflow.com/questions/19391869/whats-the-purpose-of-jtextfield-setactioncommand-how-do-you-access-it-progra