Martin819
Martin819

Reputation: 535

Java Multiple Button

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.

Upvotes: 1

Views: 527

Answers (3)

Trevi Awater
Trevi Awater

Reputation: 2407

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();
   }
}

Upvotes: 3

jp-jee
jp-jee

Reputation: 1523

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");

Upvotes: 3

ThePerson
ThePerson

Reputation: 3236

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+

Upvotes: -1

Related Questions