Link to home
Start Free TrialLog in
Avatar of omavideniz
omavideniz

asked on

Posting peer events

hi,

i managed to post events to eventqueue for some component and those components successfully get events but peers are not effected by the event, i.e. the selection event was not effect the Item state visually on the List. so is it possible to get the peer effected from the event?

thanks.
Avatar of sudhakar_koundinya
sudhakar_koundinya

<<listening>>
Why do you want to use events to change the selection of a list, instead of simply using the select(int index) method?
Yuri is right to ask such a question. I see this asked all the time. The probably can probably be solved with a better program design.

I think the problem usually arises from people including the event handling code in the listener methods. For example, say you have an email application with a send button. You might write code like this:

public void actionPerformed(ActionEvent evt) {

  String command = evt.getActionCommand();
  if (command == "SEND") {
    String subject = getSubject();
    String body = getBody();
    String address = getAddress();

    try {
      Socket socket = new Socket("127.0.0.1", 25);
      OutputStream out = socket.getOutputStream();
      out.write("helo");
      //etc...
    }
    catch (Exception e) { /* whatever... */}
  }
}

Now, if you don't want to write redundant code, you have call actionPerformed to send the email. This is okay, if the only time it is called is when you click send. But what happens if you decided later that you would like to add the Ctrl-X keystroke short cut.

The solution is to separate the functionality. So, create another method like this:

public boolean sendEmail() {
  String subject = getSubject();
  String body = getBody();
  String address = getAddress();

  try {
    Socket socket = new Socket("127.0.0.1", 25);
    OutputStream out = socket.getOutputStream();
    out.write("helo");
    //etc...
  }
  catch (Exception e) { /* whatever... */}
}

Then your actionPerformed code would look like this:

public void actionPerformed(ActionEvent evt) {
  String command = evt.getActionCommand();
  if (command == "SEND") sendEmail();
}

And you could set up a separate keystroke listener. When it is determined that Ctrl-X is pressed, you can send the email without having to generate a bogus ActionEvent.

Hope that helps,
Rob
ASKER CERTIFIED SOLUTION
Avatar of Sasha_Mapa
Sasha_Mapa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very interesting Sasha. What I'm wondering: can you also do it the other way around? I mean this: not CREATE native key codes and mouse moves, etc. but SCAN native key codes and mouse moves, before they actually happen? This would be interesting for this thread: https://www.experts-exchange.com/jsp/qShow.jsp?qid=20096739
You can definitely make bogus events. Take a look at the MouseEvent constructor:

MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int clickCount, boolean popupTrigger)

omavideniz, you if are trying to write some demo software (or something similar) such that a viewer can see mousemovements, buttons clicked etc without actually touching the mouse, then Robot is the way to go. However, if you want to force event handling code take a look at my earlier comment.
No, Yuri, the peer is the native code that gives Java the connection to native events - you don't have access to them otherwise.

Of course you can do anything you want with native code...

rkenworthy: What's your point with the MouseEvent constructor? You can create bogus events for Java code, but not for native code. If for example you take the approach in the question (posting events on the event queue), then it'll work just fine for swing components (as they are pure Java), but you can't make bogus events for peers (again AFAIK).

Sasha Maryanovsky.

As for the other thread, Yuri, I've never worked with J++ or MS' libraries for Java, but it's very possible that they have APIs that are low-level and windows specific enough to allow an application to catch alt+tab and similars before the system processes them. However, again, they would not be processed before the peers - the event will still originate in some native peer, just that native peer will intercept the event before windows handles it.

Sasha Maryanovsky.
Sasha, what does AFAIK mean?

The MouseEvent constructor demonstrates its possible to create a MouseEvent and pass it to mousePressed (or whatever method).

My whole point is that unless you are writing demo software, it is bad design to manufacture bogus AWT events, when you can accomplish the same thing in other ways.

It would help if omavideniz clarify exactly what the goal is.

Rob
AFAIK = As Far As I Know

Sasha : Thanx for your comments on that other Thread (although not helpfull unfortunately..., but it was informative)
Avatar of omavideniz

ASKER

hi,

what i actually trying to do is to create a container composed of given number of Lists, and the events occured on one of these Lists should broadcast to others.
so i want to use a standart way( if there really is ) to achieve this, not calling th select() or etc. May be using Robot would help, i don't have time to try.

omavideniz.
hi,
i take a glance on java api documents about robot and found that it isn't helpfull more than sending fixed messages, that the target window couldn't be set for the messages.
> the target window couldn't be set for the messages.
There is no target window! Robot's mouse events are relative to the upper-left point of the screen (they are low level, system global events, remember?)... Key events obviously go to the component that currently has the focus...

Sasha Maryanovsky.
ok i suppose so but setting focus to target manually will not be safe to have it get the window message. because may be user will change the focused window.
that of course before the message sent to it.
omavideniz:

You have many open questions:

https://www.experts-exchange.com/jsp/qShow.jsp?qid=20222327
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20261522
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20255833
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20248161
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247596
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247451
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239442
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20235419
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20234355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20230738
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20227630
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20210091
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163508
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20156064
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20151257
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20149551
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107807
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107283
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20096836
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095000
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20094242
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20081432
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20068029
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040399
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040320

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator

2/6