How to use a mouse in Java swing?
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
|
show 5 more comments
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
What do you expecta+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 meanMouseMotionListener
, 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 haveMouseListener
,MouseWheelListener
or aMouseMotionListener
. 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
|
show 5 more comments
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
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
java swing programming-languages
edited Nov 30 at 7:11
Yvette Colomb♦
20.2k1369107
20.2k1369107
asked Nov 21 at 0:40
Sameer
112
112
What do you expecta+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 meanMouseMotionListener
, 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 haveMouseListener
,MouseWheelListener
or aMouseMotionListener
. 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
|
show 5 more comments
What do you expecta+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 meanMouseMotionListener
, 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 haveMouseListener
,MouseWheelListener
or aMouseMotionListener
. 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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 aMouseMotionListener
. 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