Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when a button has been pressed in an external Java application?

Tags:

java

swing

I need to integrate a custom spell-checker into an existing Java application without an Automation API.

It should work like this:

  1. In the external application A, the user opens a window, where he/she enters some text. In that window there is a button "Spellchecker".
  2. When the user presses the "Spellchecker" button, my program B should read the text from A's text field and put it into the custom spellchecker.

How can I detect that some button has been pressed in an external Java application?

Update 1: I tried to install my own AWT event listener to detect events in other applications.

    Toolkit.getDefaultToolkit().addAWTEventListener(new MyAWTEventListener(), AWTEvent.MOUSE_MOTION_EVENT_MASK);

    while (true)
    {
        Thread.sleep(1);
    }

But it doesn't work.

Update 2: Replacing the system event queue doesn't work, either.

private void queuePushingExperiment() throws InterruptedException,
        InvocationTargetException {
    EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();

    queue.push(new MyEventQueue());

    EventQueue.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            System.out.println("run");
        }
    });
}

public class MyEventQueue extends EventQueue {

    @Override
    public SecondaryLoop createSecondaryLoop() {
        System.out.println("createSecondaryLoop");
        return super.createSecondaryLoop();
    }

    @Override
    protected void dispatchEvent(AWTEvent event) {
        System.out.println("dispatchEvent");
        super.dispatchEvent(event);
    }

    @Override
    public AWTEvent getNextEvent() throws InterruptedException {
        System.out.println("getNextEvent");
        return super.getNextEvent();
    }

    @Override
    public AWTEvent peekEvent() {
        System.out.println("peekEvent");
        return super.peekEvent();
    }

    @Override
    public AWTEvent peekEvent(int id) {
        System.out.println("peekEvent");
        return super.peekEvent(id);
    }

    @Override
    protected void pop() throws EmptyStackException {
        System.out.println("pop");
        super.pop();
    }

    @Override
    public void postEvent(AWTEvent theEvent) {
        System.out.println("postEvent");
        super.postEvent(theEvent);
    }

    @Override
    public void push(EventQueue newEventQueue) {
        System.out.println("push");
        super.push(newEventQueue);
    }

}

Update 3: java.awt.Window.getOwnerlessWindows() and EventQueueMonitor.getTopLevelWindows() both return empty arrays even though there is a JFrame open at time of their invokation.

Update 4: I noticed that I can't write to the file C:\Program Files\Java\jdk1.7.0_25\jre\lib\accessibility.properties and currently the line assistive_technologies=com.sun.java.accessibility.AccessBridge is commented out. That may cause the aforementioned problems with the accessibility objects.

like image 834
Dmitrii Pisarenko Avatar asked Jun 20 '13 07:06

Dmitrii Pisarenko


People also ask

How to check if a button is clicked using Java?

To be able to check if a button is clicked using Java, we create a button and add an event handler (or event listener) to the button, so that when the button is clicked, a method can be called. We can create the method to do anything such as output that the button was clicked or anything we want it to do. Alright, so now, let's go through the code.

How to check which button is pressed when you don't have access?

2. How to check which button is pressed when you don’t have access to the button object Regardless of how it was created, you can identify a button that has just been clicked through the ActionEvent ‘s getTarget () method. Then, in the same block of code, you can retrieve the button label text once the target is cast to the Button type.

How do I get the object of a button in Java?

When a button is clicked, the button object can be accessed by invoking the getTarget () method of the resulting ActionEvent. The button can be identified by using its label text or, if the button has no text, by its id if this was encoded when the button was created.

How to detect if a button has been clicked in PowerApps?

Currently, within PowerApps, the Pressed property of the Button control could not be used to detect if a button has been clicked. As an alternative solution, I think the global variable could achieve your needs.


1 Answers

How can I achieve this? I thought that for every Java program a separate JVM is launched.

Actually java app A could be run from app B. You just call the A's main() method. So in fact you start B.main() which runs necessary B code and then calls A.main() to run A. In this case you can get started window (or frame) using Window class method.

public static Window[] getWindows()

After that just go down all the subcomponents of the found Window checking their classes and when you find JButton check the buton's text or image to find necessary instance. Then just add your listener there.

like image 198
StanislavL Avatar answered Oct 14 '22 22:10

StanislavL