Please, help!
Hello, everybody!
I'm trying to have control over a key typed in a JTable. I want enable the
processing of this key only under certain circumstances. So I wrote this
code in the constructor of the JTable:
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),"validar");
getActionMap().put("validar",
new AbstractAction()
{
privatestaticfinallong serialVersionUID = 3210268230549563142;
publicvoid actionPerformed(ActionEvent e)
{
if (/* some condition is true */)
{
// enable key
}
// do nothing
}
}
);
The problem is that I don't know what code of method call to write inside the
condition block to enable the key. I know that if I wasn't using a JTable, but
a JTextField, for example, I just would have to call thepostActionEvent
method on the JTextField, but this method doesn't exist on the JTable. So,
what code do I place there to enable the key?
Thank you.
Marcos
# 1
If I was using a JTextField instead of a JTable I would solve the problem with this code:
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "validar");
getActionMap().put("validar",
new AbstractAction()
{
private static final long serialVersionUID = 3210268230549563142;
public void actionPerformed(ActionEvent e)
{
if (/* some condition is true */)
{
postActionEvent(); // this would solve the problem to me if I was using a JTextField
}
// do nothing
}
}
);
The postActionEvent method solves the problem with a JTextField. What do I use with a JTable?
Message was edited by:
Marcos_AntonioPS
# 3
I'm sorry for my insistence on this, but I'm really stuck in my application with this problem. I can't proceed until I find a solution to this. I just want to enable/disable a key under certain condition in a JTable. The JTable component is only offering me the choice to disable a key, not enable. Why all the components don't reach like the JTextField, which has a postActionEvent method for this situation? This is a feature that we need not only in a JTextField component. In Delphi I could just write:
procedure TLogin.btnOkKeyPress(Sender: TObject; var Key: Char);
begin
if {condition ok} then
begin
// key will be precessed normally. Java don't let me do this
end
else
Key := #0; // ignore key. Java only let me do this
end;
Marcos