How to use a mouse in Java swing?












0














How to do this?



I want to do this how can I do



public void mousePressed(MouseEvent e){
switch(e.getActionCommand){

case button1: System.out.println("button1 pressed");
break;
case button2: System.out.println("button2 pressed");
break;
case button3: System.out.println("button3 pressed");
break;
case button4: System.out.println("button4 pressed");
break;
case button5: System.out.println("button5 pressed");
break;
}
}









share|improve this question
























  • What do you expect a+b; to do??
    – shmosel
    Nov 21 at 0:48






  • 1




    You already are. What's the problem?
    – shmosel
    Nov 21 at 0:53










  • Ok... so what's the problem?
    – shmosel
    Nov 21 at 1:04










  • Did you mean MouseMotionListener, perhaps? ... is Oracle's information not enough info? The left side of that page has all sorts of other listener examples as well.
    – Paul T.
    Nov 21 at 1:06












  • One can have MouseListener, MouseWheelListener or a MouseMotionListener. What is it you are trying to do with a "mouseActionListener" ? Here is a link to Oracle's Java tutorials for the listeners.
    – prasad_
    Nov 21 at 2:43
















0














How to do this?



I want to do this how can I do



public void mousePressed(MouseEvent e){
switch(e.getActionCommand){

case button1: System.out.println("button1 pressed");
break;
case button2: System.out.println("button2 pressed");
break;
case button3: System.out.println("button3 pressed");
break;
case button4: System.out.println("button4 pressed");
break;
case button5: System.out.println("button5 pressed");
break;
}
}









share|improve this question
























  • What do you expect a+b; to do??
    – shmosel
    Nov 21 at 0:48






  • 1




    You already are. What's the problem?
    – shmosel
    Nov 21 at 0:53










  • Ok... so what's the problem?
    – shmosel
    Nov 21 at 1:04










  • Did you mean MouseMotionListener, perhaps? ... is Oracle's information not enough info? The left side of that page has all sorts of other listener examples as well.
    – Paul T.
    Nov 21 at 1:06












  • One can have MouseListener, MouseWheelListener or a MouseMotionListener. What is it you are trying to do with a "mouseActionListener" ? Here is a link to Oracle's Java tutorials for the listeners.
    – prasad_
    Nov 21 at 2:43














0












0








0


1





How to do this?



I want to do this how can I do



public void mousePressed(MouseEvent e){
switch(e.getActionCommand){

case button1: System.out.println("button1 pressed");
break;
case button2: System.out.println("button2 pressed");
break;
case button3: System.out.println("button3 pressed");
break;
case button4: System.out.println("button4 pressed");
break;
case button5: System.out.println("button5 pressed");
break;
}
}









share|improve this question















How to do this?



I want to do this how can I do



public void mousePressed(MouseEvent e){
switch(e.getActionCommand){

case button1: System.out.println("button1 pressed");
break;
case button2: System.out.println("button2 pressed");
break;
case button3: System.out.println("button3 pressed");
break;
case button4: System.out.println("button4 pressed");
break;
case button5: System.out.println("button5 pressed");
break;
}
}






java swing programming-languages






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 at 7:11









Yvette Colomb

20.2k1369107




20.2k1369107










asked Nov 21 at 0:40









Sameer

112




112












  • What do you expect a+b; to do??
    – shmosel
    Nov 21 at 0:48






  • 1




    You already are. What's the problem?
    – shmosel
    Nov 21 at 0:53










  • Ok... so what's the problem?
    – shmosel
    Nov 21 at 1:04










  • Did you mean MouseMotionListener, perhaps? ... is Oracle's information not enough info? The left side of that page has all sorts of other listener examples as well.
    – Paul T.
    Nov 21 at 1:06












  • One can have MouseListener, MouseWheelListener or a MouseMotionListener. What is it you are trying to do with a "mouseActionListener" ? Here is a link to Oracle's Java tutorials for the listeners.
    – prasad_
    Nov 21 at 2:43


















  • What do you expect a+b; to do??
    – shmosel
    Nov 21 at 0:48






  • 1




    You already are. What's the problem?
    – shmosel
    Nov 21 at 0:53










  • Ok... so what's the problem?
    – shmosel
    Nov 21 at 1:04










  • Did you mean MouseMotionListener, perhaps? ... is Oracle's information not enough info? The left side of that page has all sorts of other listener examples as well.
    – Paul T.
    Nov 21 at 1:06












  • One can have MouseListener, MouseWheelListener or a MouseMotionListener. What is it you are trying to do with a "mouseActionListener" ? Here is a link to Oracle's Java tutorials for the listeners.
    – prasad_
    Nov 21 at 2:43
















What do you expect a+b; to do??
– shmosel
Nov 21 at 0:48




What do you expect a+b; to do??
– shmosel
Nov 21 at 0:48




1




1




You already are. What's the problem?
– shmosel
Nov 21 at 0:53




You already are. What's the problem?
– shmosel
Nov 21 at 0:53












Ok... so what's the problem?
– shmosel
Nov 21 at 1:04




Ok... so what's the problem?
– shmosel
Nov 21 at 1:04












Did you mean MouseMotionListener, perhaps? ... is Oracle's information not enough info? The left side of that page has all sorts of other listener examples as well.
– Paul T.
Nov 21 at 1:06






Did you mean MouseMotionListener, perhaps? ... is Oracle's information not enough info? The left side of that page has all sorts of other listener examples as well.
– Paul T.
Nov 21 at 1:06














One can have MouseListener, MouseWheelListener or a MouseMotionListener. What is it you are trying to do with a "mouseActionListener" ? Here is a link to Oracle's Java tutorials for the listeners.
– prasad_
Nov 21 at 2:43




One can have MouseListener, MouseWheelListener or a MouseMotionListener. What is it you are trying to do with a "mouseActionListener" ? Here is a link to Oracle's Java tutorials for the listeners.
– prasad_
Nov 21 at 2:43












1 Answer
1






active

oldest

votes


















0














to answer your question let's start by switch/case statement, it can be used with numbers and String in Java. So you need to deal with String when capturing the user interaction in your listener, the solution is: you need to use the method setName(...) on your Swing components and capture that name which is a String in your listener. Look at this example:



    public void mousePressed(MouseEvent e){
Component c = (Component) e.getSource();
switch(c.getName()){
case "button1": System.out.println("button1 pressed");
break;
case "button2": System.out.println("button2 pressed");
break;
case "button3": System.out.println("button3 pressed");
break;
case "button4": System.out.println("button4 pressed");
break;
case "button5": System.out.println("button5 pressed");
break;
}
}


But don't forget to use the method setName("buttonX") for each button when you create,because that name you passed in the method must match to one of the names in your switch/case statement.






share|improve this answer























  • You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
    – rod.poli.diniz
    Nov 21 at 19:59











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403725%2fhow-to-use-a-mouse-in-java-swing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














to answer your question let's start by switch/case statement, it can be used with numbers and String in Java. So you need to deal with String when capturing the user interaction in your listener, the solution is: you need to use the method setName(...) on your Swing components and capture that name which is a String in your listener. Look at this example:



    public void mousePressed(MouseEvent e){
Component c = (Component) e.getSource();
switch(c.getName()){
case "button1": System.out.println("button1 pressed");
break;
case "button2": System.out.println("button2 pressed");
break;
case "button3": System.out.println("button3 pressed");
break;
case "button4": System.out.println("button4 pressed");
break;
case "button5": System.out.println("button5 pressed");
break;
}
}


But don't forget to use the method setName("buttonX") for each button when you create,because that name you passed in the method must match to one of the names in your switch/case statement.






share|improve this answer























  • You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
    – rod.poli.diniz
    Nov 21 at 19:59
















0














to answer your question let's start by switch/case statement, it can be used with numbers and String in Java. So you need to deal with String when capturing the user interaction in your listener, the solution is: you need to use the method setName(...) on your Swing components and capture that name which is a String in your listener. Look at this example:



    public void mousePressed(MouseEvent e){
Component c = (Component) e.getSource();
switch(c.getName()){
case "button1": System.out.println("button1 pressed");
break;
case "button2": System.out.println("button2 pressed");
break;
case "button3": System.out.println("button3 pressed");
break;
case "button4": System.out.println("button4 pressed");
break;
case "button5": System.out.println("button5 pressed");
break;
}
}


But don't forget to use the method setName("buttonX") for each button when you create,because that name you passed in the method must match to one of the names in your switch/case statement.






share|improve this answer























  • You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
    – rod.poli.diniz
    Nov 21 at 19:59














0












0








0






to answer your question let's start by switch/case statement, it can be used with numbers and String in Java. So you need to deal with String when capturing the user interaction in your listener, the solution is: you need to use the method setName(...) on your Swing components and capture that name which is a String in your listener. Look at this example:



    public void mousePressed(MouseEvent e){
Component c = (Component) e.getSource();
switch(c.getName()){
case "button1": System.out.println("button1 pressed");
break;
case "button2": System.out.println("button2 pressed");
break;
case "button3": System.out.println("button3 pressed");
break;
case "button4": System.out.println("button4 pressed");
break;
case "button5": System.out.println("button5 pressed");
break;
}
}


But don't forget to use the method setName("buttonX") for each button when you create,because that name you passed in the method must match to one of the names in your switch/case statement.






share|improve this answer














to answer your question let's start by switch/case statement, it can be used with numbers and String in Java. So you need to deal with String when capturing the user interaction in your listener, the solution is: you need to use the method setName(...) on your Swing components and capture that name which is a String in your listener. Look at this example:



    public void mousePressed(MouseEvent e){
Component c = (Component) e.getSource();
switch(c.getName()){
case "button1": System.out.println("button1 pressed");
break;
case "button2": System.out.println("button2 pressed");
break;
case "button3": System.out.println("button3 pressed");
break;
case "button4": System.out.println("button4 pressed");
break;
case "button5": System.out.println("button5 pressed");
break;
}
}


But don't forget to use the method setName("buttonX") for each button when you create,because that name you passed in the method must match to one of the names in your switch/case statement.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 30 at 7:11









Yvette Colomb

20.2k1369107




20.2k1369107










answered Nov 21 at 9:41









rod.poli.diniz

116118




116118












  • You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
    – rod.poli.diniz
    Nov 21 at 19:59


















  • You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
    – rod.poli.diniz
    Nov 21 at 19:59
















You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
– rod.poli.diniz
Nov 21 at 19:59




You cannot set the button name in the constructor. There you can only set the text presented in the user interface. You must use setName(...) method to set the button's name during the construction phase. Then in the event processing you can capture in the code I provided above. Please if you enjoyed the answer press the button with the arrow up to give me some points. I did it in your question.
– rod.poli.diniz
Nov 21 at 19:59


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403725%2fhow-to-use-a-mouse-in-java-swing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'