1

I need little Help with my Java project.

I have about 60 buttons which should each run another application located in computer. I know how to set mouseClicked event to do this:

 public void mouseClicked(MouseEvent e) {
        try {      
            Process p = Runtime.getRuntime().exec("address");
        } catch (IOException ex) {
            Logger.getLogger(CustomAct1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

but I don't know how to set different address to each button without creating seperated class for every single button. I now use this:

CustomAct open = new CustomAct();
but1.addMouseListener(open);

but I need to do it also for "but2", "but3" and so on.

Thank in advance.

2
  • Add your Buttons into an array of Buttons. Then iterate over it in order to addMouseListener Commented Feb 9, 2015 at 21:14
  • Don't use a MouseListener. A button was designed to use an ActionListener to handle a mouse clicked. Commented Feb 9, 2015 at 22:20

3 Answers 3

3

One approach is to iterate over all your buttons (which means you have to keep them in an Array / Collection). Then, your code would look like

for(Button b : yourButtonCollection){
     CustomAct action = createCustomAction(b);
     b.addMouseListener(action);
}

(I assume CustomAct extends MouseListener, as you've used it that way)

Another, possibly more clean approach is to extend the existing Button class. This way, you do not need to iterate over all buttons after they've been created. Hence, this requires to know the executables' path when creating the buttons.

Your custom class could look like this:

public class ExecutorButton extends Button implements EventHandler<MouseEvent> {

    private String myExecutable;

    public ExecutorButton(String pathToExecutable) {
        this.myExecutable = pathToExecutable;
        this.addEventHandler(MouseEvent.MOUSE_CLICKED,this);
    }

    @Override
    public void handle(MouseEvent event) {
        System.out.println("Executing > "+myExecutable);
        executeMyExecutable();
    }
}

(The code above is from JavaFX, since you did not specify whether you're using FX / Swing / AWT / ... However, the principle is the same for all.)

Then, create your Buttons and specify the path to their respective executables:

Button btn = new ExecutorButton("/path/to/executable");
Sign up to request clarification or add additional context in comments.

Comments

3

Start of by creating an array with JButtons, and then use the getSource() method.

public void mouseClicked(MouseEvent event) 
{
   if(event.getSource() instanceof JButton) 
   {
       (JButton)event.getSource().whatEverYouWantToDo();
   }
}

Comments

-1

If you are using java 1.8, there is a really good tutorial here about how to use lambda expressions to make this easier (and arguably cleaner): http://www.codejava.net/java-core/the-java-language/java-8-lambda-listener-example

You can do this like (from the tutorial):

button.addActionListener(e -> {
    System.out.println("Handled Lambda listener");
    System.out.println("Have fun!");
});

If you want to read more on lambda expressions, see http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

Note this will only work with JDK 1.8+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.