Wow I'm so glad I found this set of tutorials. I looked forever online and found such useless stuff on mouse listeners, and this is just what I needed.
I have been following your tutorials for quite sometime now and I must say they are really good. Easy and simple to understand! You have really made learning Java easier. Thanks a ton! :)
bucky, I spent about an hour studying oracle's api and after about another hour of experimentation I looked it up on youtube and this helped soooooo much!
I feel like Bucky is my personal mentor...any time I need help, I ask thenewboston and he always has a tutorial on it and he always explains it to me perfectly
you can add the "getX()" and "getY()" in dragging event so u can see how your mouse changes coordinates while you are moving pressing the mouse button. cheers!
The event is clicking, moving it, and then releasing it, it doesnt matter if you did it fast or slow, thats why you dont get MouseClicked, because from when you pressed, to when you released, you moved the mouse...
It's because you put your handler class inside the GUI constructor, and not as its own private class. It should be outside the GUI constructor, but still inside the GUI class. I did the same thing by accident.
Abstract means that you've got to overwrite all existing methods within the class, so probably you forgot to overwrite one of those - it's pretty well working like it's stated in the vid
When i used to jump to higher videos to see how hard it is and i used to be like what is this i am never gonna learn it and now i am like meh! Piece of cake ~.~
I had to put a "package" line to head the classes. It also said something like "statusbar can be final". And it prompted me to put @Override before all of the methods. No errors, but no GUI. I am using Netbeans, but it seems to work pretty much the same way.
@fakeface20 I have kind of just understood this, so this explanation might be wrong, but: String.format("The item %s", item) Means that the text is "The item " + a String object called item. String.format("The number is %d", num) The text becomes "The number is " + a (for example) int called num. This was written in notepad, it probably looks better there than here on youtube comments. Well @thenewboston Great tutorials! And it makes it even more great when you see what you are doing :)
try this: inside mouseMoved(), write mousepanel.setBackground(new Color(100, event.getX(), event.getY())); but make sure the height and width of the panel doesn't exceed 255 or exception will be thrown =)
i have looked. and tried to figure out what i am doing wrong. even copied it word for word. when i run my main the window pops up but when i click nothing happens. under my public void MouseClicked method it is underlined saying event is never used locally.
Im stuck on a program for school, how can I make the user input characters until he inputs an upper case letter w/ JOptionPane? also Have the user input Strings until he inputs the word "Quit". The case of the letters should not matter.
@amiorkovtube File>Export...>(window will pop up)Java>Runnable Jar File , then under launch configuration select the file that has your main method in it. this gives you a .jar, im not sure how to get an .exe :(
The mouseEntered method will never work (you wouldn't see the "You entered the area" message, because the method mouseMoved captures the same event. Apart from that little "flaw", great tutorial :-)
@JKTCGMV13 @amiorkovtube You cant get an .exe thats only with c/c++, a completely different langouage (with a few similaritys) I plan to learn c++ and objective-c after java :D keep programmin!
MouseMoves works but MouseDragged doesn't for me :( Edit: Actually it turns out it works, just doesn't work when I'm trying to detect which button is being dragged.
I hava all these GUI tutorials in one file. So I have big window with bunch of buttons when I run it. For some reason I can't add status bar to it. Is that because of "SetLayout..."? Please, help
Games. Nice nice, started a little over a week ago, the only thing that takes a while to learn is the GUI i would say, due to it's large choices of classes, functions, etc
You may have noticed when the mouse pointer enters the Window "You entered the area" message doesn't pops up which had included in his mouseEntered method but instead " YOu moved the mouse" appears in the status bar which was defined in mouseMoved method. Does anyone know why is this happening?
ujjwal mainali ***** It's not a typo its to do with precedence of java since the mouseMoved event has precedence over the mouseEntered event it's statement get overridden, the only way to get the "You entered the area" code to be written it is to clear the mouseMoved method i.e you can do this: public void mouseMoved(MouseEvent event){} Basically: mouseMoved > mouseEntered
***** Actually, I think it's because it already executed the mouseEntered method, and changed the status bar, but as you enter you also moved the mouse, so it quickly changes the text again. You know how fast computers moved these days, so we couldn't see the changes with our eyes.
If you want to see coordinates with mouse movement public void mouseMoved(MouseEvent event){ statusbar.setText(String.format("You're moving mouse at %d,%d", event.getX(),event.getY())); }
Not trying to be rude. lol...but anywayz, i personal like it, The new boston doesn't really flood the sub. list. he just runs out of mins for the video. although he is a partner, i wouldn't like to watch a 45 min. long vid. lol.....but he's alright.
hi. something is wrong here. for mouseEntered although the coluor changed to blue but in the statusbar, this sentence appears : you moved the mouse and it should be you entered the area.someone explain it to me . tq
The same happened for me. When the cursor enters the mousepanel, the listener is calling the mouseEntered method, but it is also simultaneously calling the mouse moved method as well. I think that is part of the reason why he changed the background color to red so you could see that both events are happening at the same time. The text output from mouseMoved is just showing up over the text output from mouseEntered. If we had not set any text output for the mouseMoved method and instead changed the color when the user moves the cursor, I would guess that the text output from mouseEntered would appear in the text field when you enter the mousepanel.
If you want to see the "you entered the area" text in the status bar, keep the cursor steady in the area, press alt+tab and switch to the java window "the title"..
I thought using the implements thing it would get all the events from MouseListener and MouseMotionListener. How come you had to write out all the methods?
From what I understand from the previous tutorials (mainly the ActionListener tutorial # 53), is that the Listener classes are an abstract class with a few abstract methods within them. As a result of being an abstract class, any other class that wants to utilize its methods, will be required to overwrite any of the abstract methods. It just so happens that MouseListener has 4 classes that need to be overwritten, while MouseMotionListener has 2. I hope that helps!
S3CR3AL He didn't go over this in any of his tutorials but you are partially true. implements means you are using an interface which means that you have to override ALL of its method. @Epicnerd73 the reason why he has to rewrite everything is because he used implements not extends. extend = inheritance implement = interface (Think of interface as a template of methods that you MUST define, although the actual word template is used in java/c++ or something else)
Luke Xu I was going to reply with the difference but you beat me to it. But yeah implement = interface - every method must be overwritten, extend is just standard inhertiance :)
Then you haven't imported the java.awt.event if you think about it for a second :), I even tried to take away the java.awt.event and I had to do the same thing, that's why you import it. To make it easier to use the functions/classes
Ya thats exactly what it was. I gave up before I really thought about it. Sucks when things are just to simple or easy for my mind to think of those first. Kind of like the first engine i rebuilt I later found out all I needed was gas.
Yeah, I finished all these and watched 66 tutorials of the android development series, then I started another serie by Travis ( MyBringBack ) and I was sooo sure I was right about the coding part, after 1 hour of looking I restarted eclipse and it worked. Lol
Tip: Hover over Class name with error and click "Add Unimplemented Methods"
Saves Typing all 6 methods
Bucky is the best person I've ever met in this planet.
How can you not be creative? Your programs and tutorials are just the best to selfstudy this stuff
Learning IS fun. That's what these video's are for.
True true :)
Dude I don't like your profile picture, no offence :)
lol
Epicnerd73 Watch 'The Shining' by Stanley Kubrick, you'll know where it's from :)
+Mistermaarten150 HEEEEEEEEEEEEERRRRRRRRRRRRRESSSSSSSSSS JOHNNNNNYYYY
"but hey, I'm not that creative. that's what my therapist told me" lmao
That's um, pretty sad, was bucky down at the time? Who knows the background info?
+Morte It was a joke.
You rock, dude. Your tutorials are totally saving my ass this semester. Thanks!
Wow I'm so glad I found this set of tutorials. I looked forever online and found such useless stuff on mouse listeners, and this is just what I needed.
I have been following your tutorials for quite sometime now and I must say they are really good. Easy and simple to understand! You have really made learning Java easier. Thanks a ton! :)
And did you learned Java =p?
bucky, I spent about an hour studying oracle's api and after about another hour of experimentation I looked it up on youtube and this helped soooooo much!
Who knew how fun a program that changes from white to red could be lol...
Thanks once again for another awesome tutorial!
I feel like Bucky is my personal mentor...any time I need help, I ask thenewboston and he always has a tutorial on it and he always explains it to me perfectly
you are the most useful man on the internet,,, you rule
thanks for this bucky. Youve been saving uni students for 10 years.
thank you so much for this tut!! saved me from lots of headaches trying to decrypt my profs lecture notes...
"Learning is not that fun" - Bucky 2009
Nice. We're on our way to creating a simple game through this tutorial
this program was jut amazing!!!!!!!
good job, follow master bucky and spell just wrong
you can add the "getX()" and "getY()" in dragging event so u can see how your mouse changes coordinates while you are moving pressing the mouse button.
cheers!
Thanks
This helped me so much when i was trying to learn how to do mouse events thank you so much
The event is clicking, moving it, and then releasing it, it doesnt matter if you did it fast or slow, thats why you dont get MouseClicked, because from when you pressed, to when you released, you moved the mouse...
It's because you put your handler class inside the GUI constructor, and not as its own private class. It should be outside the GUI constructor, but still inside the GUI class.
I did the same thing by accident.
Abstract means that you've got to overwrite all existing methods within the class, so probably you forgot to overwrite one of those - it's pretty well working like it's stated in the vid
this is by far the funnest program we've made ;)
When i used to jump to higher videos to see how hard it is and i used to be like what is this i am never gonna learn it and now i am like meh! Piece of cake ~.~
thank you so much man. I really appreciate your tutorials. Cheers!
Bucky, you MAKE learning fun. :) Thanks so much man.
you are awesome sir........
Thank you Bucky I was looking for this!
I had to put a "package" line to head the classes. It also said something like "statusbar can be final". And it prompted me to put @Override before all of the methods. No errors, but no GUI. I am using Netbeans, but it seems to work pretty much the same way.
@fakeface20
I have kind of just understood this, so this explanation might be wrong, but:
String.format("The item %s", item)
Means that the text is "The item " + a String object called item.
String.format("The number is %d", num)
The text becomes "The number is " + a (for example) int called num.
This was written in notepad, it probably looks better there than here on youtube comments.
Well
@thenewboston
Great tutorials! And it makes it even more great when you see what you are doing :)
people get addicted to smoking and drinking
im addicted to your videos XD
for real man you explain things very good
try this: inside mouseMoved(), write mousepanel.setBackground(new Color(100, event.getX(), event.getY())); but make sure the height and width of the panel doesn't exceed 255 or exception will be thrown =)
If you change the colors so that mouse pressed is black and then the rest are white, and then you click really fast loads of times, you have a strobe.
@Coolcl0wn
It's easier to tell the program "if dragging", instead of "if mouse moving and mouse down" every time.
Thank you for posting this video. What would be the Java code to write text into an existing word (.docx) file?
Mouse released is when you press down the mouse (hold down) and stop pressing.
Thank you very very very much!!!!! I could draw the path on the map!!! you saved my ass =*
GJ again bucky, reply please, you thinking about going far into java? e.g. TCP communication, servers clients.
Thank you for great tutorials! :)
You are awesome. Thank you so much.
of the world! Trust me, i'm saying it from Switzerland
Yes the game itself is coded in Java but I am fairly certain that the .exe only access the game files and launcher. Hope this helped.
i have looked. and tried to figure out what i am doing wrong. even copied it word for word.
when i run my main the window pops up but when i click nothing happens.
under my
public void MouseClicked method it is underlined saying event is never used locally.
you're my hero
Wouldn't it be more useful to 'getX' and 'getY' on 'mouseMoved'? Would that update the coordinates while it's moving or just when it starts moving?
you are creative men i think you know it :D
Present sir, Ankit
Im stuck on a program for school, how can I make the user input characters until he inputs an upper case letter w/ JOptionPane?
also
Have the user input Strings until he inputs the word "Quit". The case of the letters should not matter.
@amiorkovtube File>Export...>(window will pop up)Java>Runnable Jar File , then under launch configuration select the file that has your main method in it.
this gives you a .jar, im not sure how to get an .exe :(
Is there any method for Mouse scrolling ?
Like when you move the wheel of the Mouse something must happen
Memo Thunder addMouseWheelListener. I don't know the methods which we need to override but you can check it.
Sure, Thank you i will try it :)
Eshan Patil thanks
+Memo Thunder there is a listener called "mouseWheelListener". use it!
C'mon! If the're wasn't any method for mouse scrolling you wouldn't be in the comments section xD
Sadly the case "You entered the area" never takes place cause it gets overwritten by "You moved the button"...
I tried that and, yes, it works :D
you shoud make like a little homework , just for practice and then make the exercise just to see if we made it right! :)
the only tutorial that helped me figured out the mouse events, great work, man, and i guess u got a lot of girls in real life huh?
The mouseEntered method will never work (you wouldn't
see the "You entered the area" message, because the method
mouseMoved captures the same event.
Apart from that little "flaw", great tutorial :-)
I'm curious why it gives you the mouse click location after you click. Shouldn't it also say "You released the mouse."
C'mon Hoss! Get it together!
5 people just like to see the world burn Mr. Wayne.....
very good.thanks
how would you apply the changing of the background to a button instead of the whole screen?
Is there any way I can get the first and the last point of the mouseDragged ?I tried it in some ways but I didn't got it successfully
@JKTCGMV13 @amiorkovtube You cant get an .exe thats only with c/c++, a completely different langouage (with a few similaritys) I plan to learn c++ and objective-c after java :D
keep programmin!
That's why he added the setBackground method.
MouseMoves works but MouseDragged doesn't for me :(
Edit: Actually it turns out it works, just doesn't work when I'm trying to detect which button is being dragged.
lol *high voice*Oh i didn't enter yet...*normal voice* OH i entered
I hava all these GUI tutorials in one file. So I have big window with bunch of buttons when I run it. For some reason I can't add status bar to it. Is that because of "SetLayout..."? Please, help
@burhan1992 really cool, thanks for sharing that =)
I would like some tutorials on sockets, i don't understand it very well, but i would really like to learn sockets.
I tried typing the exact same code yet it does not work for some reason. And why doesn't your code have " public static void main(String[] args)" ???
I suppose his main method is in his apples class.
can't wait till i'm at this tutorial
Which tutorial are you on?
now 67
i mean 68
Games. Nice nice, started a little over a week ago, the only thing that takes a while to learn is the GUI i would say, due to it's large choices of classes, functions, etc
he is up in 81 now :D:D:D:D
Great tutorials!!! (i'm on 56 or something :P)
You may have noticed when the mouse pointer enters the Window "You entered the area" message doesn't pops up which had included in his mouseEntered method but instead " YOu moved the mouse" appears in the status bar which was defined in mouseMoved method. Does anyone know why is this happening?
ujjwal mainali ***** It's not a typo its to do with precedence of java
since the mouseMoved event has precedence over the mouseEntered event it's statement get overridden, the only way to get the "You entered the area" code to be written it is to clear the mouseMoved method i.e you can do this:
public void mouseMoved(MouseEvent event){}
Basically: mouseMoved > mouseEntered
***** Actually, I think it's because it already executed the mouseEntered method, and changed the status bar, but as you enter you also moved the mouse, so it quickly changes the text again. You know how fast computers moved these days, so we couldn't see the changes with our eyes.
you should be knighted !
sir Bucky Roberts of America.
Sorry. Could not get the released and entered message.
If you want to see coordinates with mouse movement
public void mouseMoved(MouseEvent event){
statusbar.setText(String.format("You're moving mouse at %d,%d", event.getX(),event.getY()));
}
I don't need it but thanks
7:08 EPIC VOICE CRACK!
And what if I want to drag and image from one point to wherever inside the window? :D
This wont work for me.
Not trying to be rude. lol...but anywayz, i personal like it, The new boston doesn't really flood the sub. list. he just runs out of mins for the video. although he is a partner, i wouldn't like to watch a 45 min. long vid. lol.....but he's alright.
thank you so so so so much
This is not making a distinction between left-mouse click/release/drag and right mouse click/release/drag
hi. something is wrong here. for mouseEntered although the coluor changed to blue but in the statusbar, this sentence appears : you moved the mouse and it should be you entered the area.someone explain it to me . tq
The same happened for me. When the cursor enters the mousepanel, the listener is calling the mouseEntered method, but it is also simultaneously calling the mouse moved method as well. I think that is part of the reason why he changed the background color to red so you could see that both events are happening at the same time. The text output from mouseMoved is just showing up over the text output from mouseEntered. If we had not set any text output for the mouseMoved method and instead changed the color when the user moves the cursor, I would guess that the text output from mouseEntered would appear in the text field when you enter the mousepanel.
If you want to see the "you entered the area" text in the status bar, keep the cursor steady in the area, press alt+tab and switch to the java window "the title"..
Tq. Pragnesh
@kripis0 *slaps face* its called the freaking launcher that launches the jar file... Please, you're welcome to use your brain at anytime
why is it my mouse works only in statusbar area instead at the whole window
can anybody explain the released mouse i never get that in my status bar thanks
no... actionscript 4 isn't out, this is java... actionscript 4 is coming in 2010. Early 2010, as stated by adobe.
DAFUQ mine has an error saying class is abstract can someone help me pls?
did you eventually added the listeners to the statusbar?
this was fun
bucky could probably make a tutorial on that
When I try this program, I get the error "selection does not contain a main type"
Who's Hoss?
Btw i commented on most video's to help people :)
How could you get the program to output the pointer's location as you move it?
event.getX()
In the mouseMoved method put
statusbar.setText(String.format("Position at X:%d Y:%d", event.getX(), event.getY()))
I thought using the implements thing it would get all the events from MouseListener and MouseMotionListener. How come you had to write out all the methods?
From what I understand from the previous tutorials (mainly the ActionListener tutorial # 53), is that the Listener classes are an abstract class with a few abstract methods within them. As a result of being an abstract class, any other class that wants to utilize its methods, will be required to overwrite any of the abstract methods. It just so happens that MouseListener has 4 classes that need to be overwritten, while MouseMotionListener has 2. I hope that helps!
S3CR3AL Ok thanks
S3CR3AL He didn't go over this in any of his tutorials but you are partially true.
implements means you are using an interface which means that you have to override ALL of its method.
@Epicnerd73 the reason why he has to rewrite everything is because he used implements not extends. extend = inheritance
implement = interface (Think of interface as a template of methods that you MUST define, although the actual word template is used in java/c++ or something else)
Luke Xu I was going to reply with the difference but you beat me to it. But yeah implement = interface - every method must be overwritten, extend is just standard inhertiance :)
ahah you should add an event that when the mous enters the window a nasty website like lemon***** pops up and then send it to a firend xD
please add more tutorial on different concept spacefic in Gui
For some reason I had to use (java.awt.event.MouseEvent event) inorder for java to say that I implimented the methods.
Then you haven't imported the java.awt.event if you think about it for a second :), I even tried to take away the java.awt.event and I had to do the same thing, that's why you import it. To make it easier to use the functions/classes
Ya thats exactly what it was. I gave up before I really thought about it. Sucks when things are just to simple or easy for my mind to think of those first. Kind of like the first engine i rebuilt I later found out all I needed was gas.
Yeah, I finished all these and watched 66 tutorials of the android development series, then I started another serie by Travis ( MyBringBack ) and I was sooo sure I was right about the coding part, after 1 hour of looking I restarted eclipse and it worked. Lol
Genil
Where is mouse double clicked?