Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Two small questions: keypressed event and regexp

Thread view: 
Claus K. - 06 May 2006 16:37 GMT
Hello everyone,

just a small thing which has been bugging me for some time now.

I have created a small GUI which serves as a Mudclient (works alright),
but there is a problem with this:

#code snippet:
public void keyPressed(KeyEvent arg0)
    {
...
        else if (arg0.getKeyCode() == KeyEvent.VK_NUMPAD1)
        {
            appletwindow.out.println("look");
            appletwindow.output.append("look" + "\n");
            appletwindow.addHistoryIndex("look");
            appletwindow.input.setText("look");
}
...

out is an Stream sending data to the server,
output is a textarea containing what is sent to and from the server,
history is like a unix shell command history, i.e. with arrow-up and
arrow-down keys, you can navigate between your last commands which will
appear in input, which is a textfield.

Now, what is happening if you press numpad-1 is that the command "look"
is sent over the stream, the command appears in the output textarea,
also in the history, but for the input-textfield, there is the number
"1" printed with the string "look" printed afterwards, of which I only
want the string "look" to appear, not the actual number which has been
pressed.

How can I manage to do that?

My second question is concerning regexps:

I have this coming over the stream:



and I have to filter it out, but all my expressions dont seem to catch
it.
I have tried to go for (for example)

Pattern color_pattern2 = Pattern.compile("\\[[0-9]+.[0-9]{2}[m]");

or a variation of

Pattern color_pattern =
Pattern.compile("\u001B"+"\\u005B"+"[0-9]+?"+"[m]");

which filters out most of those weird color codes (which they are), but
they fail to catch what I have described above. I think that is due to
the comma in there, but however I change the above, I just dont catch
it.

Easiest Solution would be to expand color_pattern I think, but Java
regexp works so differently from what I know from sed or perl, that I
am very frustrated by now.

Could someone please shed some light on those two - as I perceive them
- minor issues?

I would be very grateful,

Claus
hiwa - 07 May 2006 11:22 GMT
For the text field, you could set it uneditable and/or unfocusable.

For regex, your should formalize the characteristics of the target
text.
If it's just "[1;37m" and only numbers are variable, then regex would
be:
"\\[\\d;\\d\\dm".
Claus K. - 07 May 2006 14:02 GMT
Setting the Textfield temporarily unfocusable or uneditable didnt work
either, the key was to use keyReleased instead of keyPressed. Did not
think of trying that before just now (a friend suggested that). Now,
the numbers appear but are replaced upon keyRelease with whatever I
want to have there. This is not ideal, but at least workable, though I
presume there is a way to pipe those numbers to /dev/null which I just
have not found mentioned anywhere yet.

This proved to be a rather intersting question, actually.

As for the regexp, the one you suggested works, but somehow, you cannot
have two regexp matchers active at the same time (or at least for the
same line), which I just did not know (nor remember being stated
somewhere), so I had to combine the two to:

Pattern color_pattern =
Pattern.compile("\u001B"+"\\u005B"+"[0-9]??"+".??"+"[0-9]+?"+"[m]");

I finally learned the difference between ?? and ? and ?+ now as well.

Thank you for helping me, I really appreciate that.

Claus


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.