Can a DefaultCellEditor respond to custom events.

J

JoeyB

All,

New to Java programming and this one has me stumped. I've got a JTable
that I've defined a DefaultCellEditor for. That works great. If the
user types the wrong thing in the JTable, it gets corrected. The
problem I have is that Ive got a subclass of ActionListener that is
responsible for pasting numbers into the JTable from the clip board.
This class is using JTable's setValueAt method. I'd like to have my
CellEditor review this data before it's allowed to set the value of my
CellEditor's JFormattedTextField but I can't find any way to call my
CellEditor procedurally. I can see that the CellEditor responds to
keyboard events but is there a way to also have it respond to user
defined events? Then, when my ActionListener derived class starts to
write data into the JTable, it could fire the event and the
DefaultCellEditor would respond to the event and verify/allow/disallow
the data. I know I could put some duplicate validation code in the
ActionListener but it seems I shouldn't have to do this (messy). I'm
sure I'm making this problem harder than it is. Any help is
appreciated.
 
M

markspace

JoeyB said:
All,

New to Java programming and this one has me stumped. I've got a JTable
that I've defined a DefaultCellEditor for. That works great. If the
user types the wrong thing in the JTable, it gets corrected. The
problem I have is that Ive got a subclass of ActionListener that is
responsible for pasting numbers into the JTable from the clip board.


This sounds like the wrong direction. Normally you set a filter on your
document to prevent unwanted characters, or use InputVerifier or use
some type of formatter. ActionListener sounds like it might be too low
level.

<http://java.sun.com/javase/6/docs/api/javax/swing/text/DocumentFilter.html>
<http://java.sun.com/javase/6/docs/api/javax/swing/InputVerifier.html>
<http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultFormatter.html>

It's hard to be sure, though, since you question is wide ranging.

To get more help, I'd have to see some sort of SSCCE. Like, a table
with one cell and a specific format you're having issues with. Some
things have pre-built formatters (like dates and some numbers), others
you have to roll your own.
 
J

John B. Matthews

JoeyB said:
New to Java programming and this one has me stumped. I've got a
JTable that I've defined a DefaultCellEditor for. That works great.
If the user types the wrong thing in the JTable, it gets corrected.
The problem I have is that Ive got a subclass of ActionListener that
is responsible for pasting numbers into the JTable from the clip
board. This class is using JTable's setValueAt method. I'd like to
have my CellEditor review this data before it's allowed to set the
value of my CellEditor's JFormattedTextField but I can't find any way
to call my CellEditor procedurally. I can see that the CellEditor
responds to keyboard events but is there a way to also have it
respond to user defined events? Then, when my ActionListener derived
class starts to write data into the JTable, it could fire the event
and the DefaultCellEditor would respond to the event and
verify/allow/disallow the data. I know I could put some duplicate
validation code in the ActionListener but it seems I shouldn't have
to do this (messy). I'm sure I'm making this problem harder than it
is. Any help is appreciated.

You might look to see if an InputVerifier would complement your
CellEditor:

<http://java.sun.com/javase/6/docs/api/javax/swing/InputVerifier.html>
<http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
#inputVerification>
 
J

JoeyB

Thanks for your responses. I'm not sure I'm describing the problem
well. Maybe I can re-phrase the question. Is there a way to call my
custom built celleditor procedurally? In other words, I'd like to be
able to call the same code that fires when a user presses the 'enter'
key to be called procedurally. The code shouldn't make the distinction
between data entered from a paste from clipboard and data entered
through the keyboard. Specifically, I'd like like this code taken from
my DefaultCellEditor derived class to respond to also respond my own
handmade event that I'd fire procedurally each time I call JTable's
setValueAt method:

ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
0),"check");

ftf.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!ftf.isEditValid()) { //The text is invalid.
if (userSaysRevert()) { //reverted
ftf.postActionEvent(); //inform the editor
}
} else try { //The text is valid,
ftf.commitEdit(); //so use it.
ftf.postActionEvent(); //stop editing
} catch (java.text.ParseException exc) { }
}
});

Is there a way that AbstractAction can be registered to handle Event
derived events?
 
J

John B. Matthews

JoeyB said:
Thanks for your responses. I'm not sure I'm describing the problem
well. Maybe I can re-phrase the question. Is there a way to call my
custom built celleditor procedurally? In other words, I'd like to be
able to call the same code that fires when a user presses the 'enter'
key to be called procedurally. The code shouldn't make the
distinction between data entered from a paste from clipboard and data
entered through the keyboard. Specifically, I'd like like this code
taken from my DefaultCellEditor derived class to respond to also
respond my own handmade event that I'd fire procedurally each time I
call JTable's setValueAt method:
[...]

Is there a way that AbstractAction can be registered to handle Event
derived events?

Sorry, I'm still not following what you want to do. Here's another
example of using an InputVerifier to allow alternate date formats. It
verifies both typed and pasted text:

<http://groups.google.com/group/comp.lang.java.programmer/
msg/d9f7a7702139b48f>

You can invoke your InputVerifier's public verify() method from
anywhere. In the example above, override invalidEdit() to omit the beep
when enter is pressed on a valid entry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top