• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

Overriding an Application :::::::::::::::::::::::::::::::::::::::

I wrote a java code that has a button that opens an application............but how can i press a button or menu inside that application without using the mouse, just simply by a program???  Can you give me a sample code??  Thanks..
0
casper_the_ghost
Asked:
casper_the_ghost
  • 11
  • 9
  • 2
1 Solution
 
objectsCommented:
button.doClick();
0
 
objectsCommented:
or call actionPerformed() of the listener if that all you want.
0
 
gnoonCommented:
Also, u can use mouseMove() and mousePress() in java.awt.Robot class.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
gnoonCommented:
0
 
casper_the_ghostAuthor Commented:
how woul the pogram know which buttonsto click inside that application???  I mean for button.doClick, actionPerfored(),mouseMove(), or mousePressed()........do i need to put the location of the command inside those methods??
0
 
objectsCommented:
Not sure I understand the question, if you want to press a certain button, surely you already know which button it is you want to press :)
In your question for example that would be the button that opens the application.

You don't need the location to call doClick()
0
 
casper_the_ghostAuthor Commented:
i meant that i have a button in the panel called "openApp" for example which opens a Word application, and another button called "closeApp" that closes the Word application.........what is the code that will closes that application in Java????
0
 
objectsCommented:
closeApp.doClick();
0
 
casper_the_ghostAuthor Commented:
Here is the code for opening an application..........................................................
Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE");

now, What is a code that closes it???  I got to have a path in it so the computer would know which Application it would close otherwise what is it closing????  
That doClick() doesn't work unless it's not complete.  Do i put the path inside doClick() or what class is it under??  I can't find doClick in the member functions??
0
 
objectsCommented:
I don't think you can close it.
Yopu could try killing the process returned by exec() but it may have forked another process, in which case you can't close (without a native solution)
0
 
casper_the_ghostAuthor Commented:
*******How can you kill a process returned by exec()???

*******how about destroy()??

*******Here's my whole code......in the if statement it opens the application when the user clicks the second tab, now in the else statement i need a code to close it when the user clicks a different tab (A tab other than the second tab).....


package jasper;

import java.awt.* ;
import javax.swing.*;
import java.awt.event.* ;
import javax.swing.event.* ;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import java.awt.event.KeyEvent;

public class MyTabbedPane extends JFrame
{

  JTabbedPane jt = new JTabbedPane() ;

  public MyTabbedPane() {
    Container c = this.getContentPane() ;
    c.add(jt,BorderLayout.CENTER) ;


    jt.addChangeListener(new MyChangeListener());
    jt.add(new Panel()) ;
    jt.add(new Panel()) ;
    jt.add(new Panel()) ;

    jt.addKeyListener(new MyKeyListener());

    //jt.setSelectedIndex(2);
    setSize(400,300) ;
    setVisible(true) ;


  }

  class MyKeyListener implements KeyListener {
    public void keyTyped(KeyEvent keyEvent) {
      if(keyEvent.getKeyCode() == KeyEvent.VK_A) {
        System.out.println("A was pressed...");
      }
    }

    public void keyPressed(KeyEvent keyEvent) {
      if(keyEvent.getKeyCode() == KeyEvent.VK_A) {
        System.out.println("A was pressed...");
      }

    }

    public void keyReleased(KeyEvent keyEvent) {
    }
  }

  class MyChangeListener implements ChangeListener {
    public void stateChanged(ChangeEvent e)
{
      //jt.getSelectedIndex() ;

      JTabbedPane p = (JTabbedPane) e.getSource() ;
      if(p.getSelectedIndex()== 1)
      {
        //do something
        try
        {
                   //Process a = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE");
          Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE");
        }

        catch (java.io.IOException ex) { }
      }


        //code how to connect to other programm
     // }

      else
      {
        //a.destroy();


      }

    }
  }






  public static void main(String[] args) {
    MyTabbedPane myTabbedPane1 = new MyTabbedPane();
  }

}
0
 
objectsCommented:
> *******how about destroy()??

thats the one, but it won't work if the exe forks itself.
0
 
casper_the_ghostAuthor Commented:
can you show me a code that could make this work using destroy()????  I don't know what you mean by....."it won't work if the exe forks itself".  Give me a complete code in the else statement.  Thanks
0
 
objectsCommented:
try the following as a test:

Process p = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE");
p.destroy();

0
 
casper_the_ghostAuthor Commented:
I know it can work like that, but the thing is it won't show at all when i need to.......i need a condition with it like the if and else statement i have on my program and just follow the instructions.......i need to open a program when i click on the middle tab and close the program when i hit the other tabs.
0
 
casper_the_ghostAuthor Commented:
I don't mean to pressure you, but I really need to finish this in a few minutes.............i have to leave school in a few minutes.
0
 
objectsCommented:
if you want to perform an action when a tab is chnaged use a ChangeListener on your JTabbedPane.
0
 
objectsCommented:
which I've just noticed you already have.
0
 
objectsCommented:
keep the Process as a member var so you can access it.

  class MyChangeListener implements ChangeListener {
    Process process = null;

    public void stateChanged(ChangeEvent e)
{
      //jt.getSelectedIndex() ;

      JTabbedPane p = (JTabbedPane) e.getSource() ;
      if(p.getSelectedIndex()== 1)
      {
        //do something
        try
        {
                   //Process a = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE");
          process = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE");
        }

        catch (java.io.IOException ex) { }
      }


        //code how to connect to other programm
     // }

      else
      {
         if (process!=null)
         {
           process.destroy();
           process = null;
        }


      }

    }
0
 
casper_the_ghostAuthor Commented:
Thank you so much....................what do i do next so you can get points.....that was a lot of help......i've been trying everything the whole day even taskkill in Runtime and i couldn't make it work........i was close with the destroy(), but i get some errors just because i didn't put it in the right place.   Thank you!!!!
0
 
objectsCommented:
Sorry it took so long, I wasn't exactly sure which bit you were having problems with.
You just need to accept my comment as an answer :)
0
 
casper_the_ghostAuthor Commented:
thanks again
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 11
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now