Hello.
I have a problem when I'm programming a special subtitle (SubRip/srt)
editor in Java.
I use a JTable (referred to as "table" in the code below) and when the
user presses Shift+Enter the table should select row n+1, where n is the
currently selected row. Ctrl+Enter does the opposite, selects row n-1.
The problem is that the code for Shift+Enter doesn't work and my
co-students at my university don't understand why either! Ctrl+Enter
works like a charm, only Shift+Enter does nothing!
Debugging showed me that if I program it to select n+2 (!) it does just
what I want, with the exception that it will never select the last row
of the table. It will disregard it and select an imaginary row outside
of the table.
Code follows:
public void keyPressed(KeyEvent e) {
[...]
//selmodel is the ListSelectionModel of the JTable.
//mod is the String representation of the modifier keys pressed.
//table is the JTable.
else if ((mod.equals("Shift") || mod.equals("Ctrl")) && e.getKeyCode()
== KeyEvent.VK_ENTER) {
int selectedrow = table.getSelectedRow();
if (selectedrow != -1) {
ListSelectionModel selmodel = table.getSelectionModel();
if (mod.equals("Shift")) {
if (selectedrow + 1 < table.getRowCount()) {
selmodel.setSelectionInterval(selectedrow + 1,selectedrow + 1);
}
} else if (mod.equals("Ctrl")) {
if (selectedrow > 0) {
selmodel.setSelectionInterval(selectedrow - 1,selectedrow - 1);
}
}
}
}
[...]
}
Looking forward to your replies.
/kristnjov
...
> I have a problem when I'm programming a special subtitle (SubRip/srt)
What does SubRip/srt mean to you? It means nothing to me.
> editor in Java.
> I use a JTable (referred to as "table" in the code below) and when the
> user presses Shift+Enter the table should select row n+1, where n is the
> currently selected row. Ctrl+Enter does the opposite, selects row n-1.
Note that the normal navigation behaviour of a table is to
use 'enter' for moving down rows, and 'shift enter' to move
up. Your navigation scheme hardly represents the
'path of least surprise'.
> The problem is that the code for Shift+Enter doesn't work
Perhaps you need to 'consume' the event so that it does not
propagate back to the table and 'fight against' the behaviour
you expect.
OTOH - perhaps you should revise the table navigation entirely.
Andrew T.
Deniz Dogan - 22 Sep 2006 14:45 GMT
> ...
>> I have a problem when I'm programming a special subtitle (SubRip/srt)
>
> What does SubRip/srt mean to you? It means nothing to me.
Explaining it would not at all tell you more about the problem. Anyways,
SubRip (often identified by the .srt file extension) is a subtitle text
format. The format is as follows:
532 //line number
00:23:56,800 --> 00:23:59,700 //duration
-How's it going? //the text to be shown on 1 or more rows
-Quite well, actually
\n
etc.
And as I said before, this has nothing to do with the problem, whatsoever.
>> editor in Java.
>> I use a JTable (referred to as "table" in the code below) and when the
[quoted text clipped - 5 lines]
> up. Your navigation scheme hardly represents the
> 'path of least surprise'.
Another very popular subtitle editor program for Windows is called
Subtitle Workshop (not open-source) and it uses the same key bindings
for moving up and down in the table. Most translators of amateur
subtitles are familiar with those keys from the program and that's why I
want the same behavior in my program. If I get a lot of requests to
change the key bindings, I might implement a configuration file reader,
so that users can define their own key bindings.
>> The problem is that the code for Shift+Enter doesn't work
>
> Perhaps you need to 'consume' the event so that it does not
> propagate back to the table and 'fight against' the behaviour
> you expect.
I'll see if I can do that, thanks!
> OTOH - perhaps you should revise the table navigation entirely.
I'll consider it, but do you have an suggestions that would improve it?
> Code follows:
>
[quoted text clipped - 5 lines]
> else if ((mod.equals("Shift") || mod.equals("Ctrl")) && e.getKeyCode()
> == KeyEvent.VK_ENTER) {
Hi Deniz:
Try something like this (see below). Also google for "For example, the
appropriate way to check that SHIFT and BUTTON1" and check the javadocs
for getModifiersEx.
int shiftMask = InputEvent.SHIFT_DOWN_MASK;
int ctrlMask = InputEvent.CTRL_DOWN_MASK;
int selectedrow = table.getSelectedRow();
if (selectedrow != -1) {
if ((e.getModifiersEx() & (shiftMask | ctrlMask)) == shiftMask) {
System.out.println("Shift");
} else if ((e.getModifiersEx() & (shiftMask | ctrlMask)) ==
ctrlMask) {
System.out.println("Ctrl");
}
}
Cheers,
Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -
Deniz Dogan - 23 Sep 2006 19:36 GMT
>> Code follows:
>>
[quoted text clipped - 23 lines]
> }
> }
Thank you for the tip, Dan! I shall try it out!
And as for the actual problem I was having, I've found out that I had
actually programmed the program to do a whole other thing than what I
really wanted! It's not the table itself that should listen for the
keys, it's a couple of text boxes that show specific information _from_
the table. Hence, problem solved! :-)
Thanks a lot to you both of you guys for the help, I appreciate it!