Sir, thank you soo much! I've been helped by this tutorial. The only thing I've change was where I added the keylistener. I needed that all the jFrame was onfocus. So I put this lines on construct: this.addKeyListener(this); setFocusable(true); With theses lines, the jTextField it's not necessary. That was my problem, maybe it's the problem of someone else.
Thank you very much for your informative and easy to understands tutorials . I have three questions, 1. Is there any particular reason for using constructor instead of void method ? 2. If i recall correctly in tutorial 24 you mentioned commands within a constructor can be run without requiring a main method if there is a no user input, would have been possible to run this code without main method if there was no user input ? 3. Why our class needs to inherit from JFrame despite of having java.swing package imported?
+08regman 1) Constructors are just used when objects are created.You should use constructors to organize any setup that you'll need for the rest of the class. Any other methods do work for the class after the constructor has done it's business. 2) Sure you can run classes without a main method - only one of your classes needs a main (the entry point of your program). 3) Importing Swing classes gives you access to the library but inheritance is differenent
Almost what I'm looking for, except what if I wanted text frames to update depending on one another. Example: User enters "50" into TextField1(MPH) and TextField2 (KPH) automatically updates to show "80.46" or vise versa User could enter KPH and program calculates MPH? Thanks!
+EJ Media This is your code from your tutorial: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Gui extends JFrame implements KeyListener { JTextField keyText = new JTextField(80); JLabel keyLabel = new JLabel("Press fire button"); Gui () { keyText.addKeyListener(this); setSize(400, 400); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BorderLayout layout = new BorderLayout(); setLayout(layout); add(keyLabel, BorderLayout.NORTH); add(keyText, BorderLayout.CENTER); } public void keyTyped (KeyEvent e) { // not putting anything in this method } public void keyPressed (KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_F){ keyLabel.setText("You pressed the fire button"); } else { keyLabel.setText("You pressed the wrong key"); } } public void keyReleased (KeyEvent txt) { // not putting anything in this method } public static void main(String[] args) { Gui go = new Gui(); } }
Sir, To which Class.Object are the following methods attached to? setSize(400,400); setVisible(true); Don't these need to be referenced as SomeObjectName.setSize(400,400)? How come you refer to them using just the method name with arguments? Also, unclear as to what setLayout(layout); means. Its invoking the setLayout method from which class on the layout object?
S Raj Oh ok I see what you asking - I am trying to remember this code haha its been a while - ok here is what is going on -> Gui () is the constructor so that is why you dont need the object in front of it - in other words we are saying hey do these things when we build the object and so those are being used to create the screen. Since GUI () is extended off of JFRAME the methods are from JFRAME ... now actually a lot of work will be done in the constructors .. when you build the object
In the last part of the code. you defined Gui go = new Gui ( ); but you never defined "go" earlier. I wrote the same line as yours but it is not working
Saka Abdulrahman Try this code: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Gui extends JFrame implements KeyListener { JTextField keyText = new JTextField(80); JLabel keyLabel = new JLabel("Press fire button"); Gui () { keyText.addKeyListener(this); setSize(400, 400); setVisible(true); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BorderLayout layout = new BorderLayout(); setLayout(layout); add(keyLabel, BorderLayout.NORTH); add(keyText, BorderLayout.CENTER); } public void keyTyped (KeyEvent e) { // not putting anything in this method } public void keyPressed (KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_F){ keyLabel.setText("You pressed the fire button"); } else { keyLabel.setText("You pressed the wrong key"); } } public void keyReleased (KeyEvent txt) { // not putting anything in this method } public static void main(String[] args) { Gui go = new Gui(); } }
How many more videos do you plan on making for java? I need to learn java in 2 months so i need to know how many videos I need to watch to finish. EJ Media
Mani Cho Well I have looked at what some other you tube publishers call intermediate is not my intermediate ... I am introducing concepts in my beginner series that they introduce much later on ... so at the end of this you will know java pretty darn well
Mani Cho The hope is by the end you will be able to read code and understand how it works ... you also should start to be able to write some code from scratch or edit existing code - however you want to do it - editing existing code is always easier than writing from scratch -
Hey yeh I tried my own version which didnt work. So I copied and pasted the one you commented and again same thing (frame shows up with no content) but moving the setVisible part to the end worked! it is strange why it didn't work for me in the beginning. Cheers
sir i have 2 question the code (keyText.addKeyListener(this)) what do it mean second when i call the constructor in the main method how it read all the class thanks
karima ali Hello - the (keyText.addKeyListener(this)) line -- this adds the listener to the component so that it can listen to key events .. in this case the component is the text field which is our object keyText ................ the main method creates the object go - and the constructor forms the window and sets up the listener and then its done - now nothing else happens until a keyboard event gets triggered then the method keyPressed gets executed
i keep getting an error message at the line---- keyText.addKeyListener(this);----error message reads "incompatible types. Gui cannot be converted to KeyListener. Leaking this in constructor" anyone else having this problem? am I just making a silly mistake?
+Quinten Bredeveldt Can you explain why we need to implement KeyListener in the first place? I thought interfaces just say you need to implement these methods? So if I already typed those methods without typing 'implements KeyListener', there shouldn;t be a problem, right?
instead of keyReleased i typed keyRelesed, and it took me about one hour to find the error. you should really put the source code in the description. often we will save a tons of time and nerves.
+stef wth This was the first series I made so I made the mistake of not saving some of the code - but I am in the process of trying to get the code uploaded for these videos. Every video that I post these days always has the code in the description.
I typed up this lesson's code and posted it on github here: github.com/Taylomol001/Java-Tutorial-for-Beginners---43---GUI---Setting-up-KeyListener-Keyboard-Events-JLabel-and-Textbox
Why is there a need for ( extends JFrame ) in the vid? The previous vids didn't inherit JFrame yet was able to use it. And what does setLayout(layout) mean?
MrAirPork1 I don't have the code in front of me for 42 but I sort of remember I just needed to create an object for the method - we didnt need to extend it at the class level in that lesson
EJ Media Hmm your point made me realised that in 42 you made an object to access the methods. How come no object is needed here to access the JFrame methods?
MrAirPork1 There are different ways you can do things - in this case I wanted to access JFrame from the constructor - so at the time the object was created
Source:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame implements KeyListener {
JTextField keyText = new JTextField(80);
JLabel keyLabel = new JLabel("Press fire button");
Gui () {
keyText.addKeyListener(this);
setSize(400, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
add(keyLabel, BorderLayout.NORTH);
add(keyText, BorderLayout.CENTER);
}
public void keyTyped (KeyEvent e) {
// not putting anything in this method
}
public void keyPressed (KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_F){
keyLabel.setText("You pressed the fire button");
}
else {
keyLabel.setText("You pressed the wrong key");
}
}
public void keyReleased (KeyEvent txt) {
// not putting anything in this method
}
public static void main(String[] args) {
Gui go = new Gui();
}
}
Thank you sir, a very clear explanation and also a splendid video !
Sir, thank you soo much! I've been helped by this tutorial. The only thing I've change was where I added the keylistener. I needed that all the jFrame was onfocus. So I put this lines on construct:
this.addKeyListener(this);
setFocusable(true);
With theses lines, the jTextField it's not necessary.
That was my problem, maybe it's the problem of someone else.
It was my problem as well. Thank you
A very good video! Thanks 😉
10/10 "Best game ever" - IGN (srsly tho, good vid ;)
EJ Media person man, i love your face. Thank you!
thanks alot you saved my night!
Thank you for your help.
Thank you very much!
This was really helpful. Thanks :)
Thank you very much for your informative and easy to understands tutorials .
I have three questions,
1. Is there any particular reason for using constructor instead of void method ?
2. If i recall correctly in tutorial 24 you mentioned commands within a constructor can be run without requiring a main method if there is a no user input, would have been possible to run this code without main method if there was no user input ?
3. Why our class needs to inherit from JFrame despite of having java.swing package imported?
+08regman
1) Constructors are just used when objects are created.You should use constructors to organize any setup that you'll need for the rest of the class.
Any other methods do work for the class after the constructor has done it's business.
2) Sure you can run classes without a main method - only one of your classes needs a main (the entry point of your program).
3) Importing Swing classes gives you access to the library but inheritance is differenent
Helped a lot man. Thanks :)
What a great video :0
Almost what I'm looking for, except what if I wanted text frames to update depending on one another. Example: User enters "50" into TextField1(MPH) and TextField2 (KPH) automatically updates to show "80.46" or vise versa User could enter KPH and program calculates MPH? Thanks!
+Rudy Trevino Hi Rudy can you paste all the code - I haven't seen it in a while -thx
+EJ Media This is your code from your tutorial:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame implements KeyListener {
JTextField keyText = new JTextField(80);
JLabel keyLabel = new JLabel("Press fire button");
Gui () {
keyText.addKeyListener(this);
setSize(400, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
add(keyLabel, BorderLayout.NORTH);
add(keyText, BorderLayout.CENTER);
}
public void keyTyped (KeyEvent e) {
// not putting anything in this method
}
public void keyPressed (KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_F){
keyLabel.setText("You pressed the fire button");
}
else {
keyLabel.setText("You pressed the wrong key");
}
}
public void keyReleased (KeyEvent txt) {
// not putting anything in this method
}
public static void main(String[] args) {
Gui go = new Gui();
}
}
If i wanna check the CONTROL KEYS then what will VK follow?
Sir,
To which Class.Object are the following methods attached to?
setSize(400,400);
setVisible(true);
Don't these need to be referenced as SomeObjectName.setSize(400,400)? How come you refer to them using just the method name with arguments?
Also, unclear as to what setLayout(layout); means. Its invoking the setLayout method from which class on the layout object?
S Raj Oh ok I see what you asking - I am trying to remember this code haha its been a while - ok here is what is going on -> Gui () is the constructor so that is why you dont need the object in front of it - in other words we are saying hey do these things when we build the object and so those are being used to create the screen. Since GUI () is extended off of JFRAME the methods are from JFRAME ... now actually a lot of work will be done in the constructors .. when you build the object
I want use keypress java on different program but not work what should i do?
In the last part of the code. you defined Gui go = new Gui ( ); but you never defined "go" earlier. I wrote the same line as yours but it is not working
Saka Abdulrahman Try this code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame implements KeyListener {
JTextField keyText = new JTextField(80);
JLabel keyLabel = new JLabel("Press fire button");
Gui () {
keyText.addKeyListener(this);
setSize(400, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
add(keyLabel, BorderLayout.NORTH);
add(keyText, BorderLayout.CENTER);
}
public void keyTyped (KeyEvent e) {
// not putting anything in this method
}
public void keyPressed (KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_F){
keyLabel.setText("You pressed the fire button");
}
else {
keyLabel.setText("You pressed the wrong key");
}
}
public void keyReleased (KeyEvent txt) {
// not putting anything in this method
}
public static void main(String[] args) {
Gui go = new Gui();
}
}
How many more videos do you plan on making for java? I need to learn java in 2 months so i need to know how many videos I need to watch to finish. EJ Media
Mani Cho For the beginning series there are about 40 left
...
This is just the beginning series? I thought if I follow this path I'll learn most of java at least.
Mani Cho Well I have looked at what some other you tube publishers call intermediate is not my intermediate ... I am introducing concepts in my beginner series that they introduce much later on ... so at the end of this you will know java pretty darn well
Oh, thanks! I'll continue to watch your videos! :D
Mani Cho The hope is by the end you will be able to read code and understand how it works ... you also should start to be able to write some code from scratch or edit existing code - however you want to do it - editing existing code is always easier than writing from scratch -
For this code when I ran it, it loaded an empty frame at runtime... but when I moved "setVisible(true);" to the end of the GUI constructor it works
John Barry That's strange I will test it out tonight - did you use the code that I pasted into the video at all? Just curious
John Barry Just curious if you used the code I pasted into the message board of the video? thanks
Hey yeh I tried my own version which didnt work. So I copied and pasted the one you commented and again same thing (frame shows up with no content) but moving the setVisible part to the end worked! it is strange why it didn't work for me in the beginning. Cheers
sir i have 2 question
the code (keyText.addKeyListener(this)) what do it mean
second
when i call the constructor in the main method how it read all the class
thanks
karima ali Hello - the (keyText.addKeyListener(this)) line -- this adds the listener
to the component so that it can listen to key events .. in this case the component is the text field which is our object keyText ................ the main method creates the object go - and the constructor forms the window and sets up the listener and then its done - now nothing else happens until a keyboard event gets triggered then the method keyPressed gets executed
EJ Media thanks :))
i keep getting an error message at the line---- keyText.addKeyListener(this);----error message reads "incompatible types. Gui cannot be converted to KeyListener. Leaking this in constructor" anyone else having this problem? am I just making a silly mistake?
+Soren Bagley Can you paste the code in the message board so I can try it? thx
+EJ Media Sure thing. Thanks for the quick response
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Gui extends JFrame implements KeyListener {
JTextField keyText = new JTextField (80);
JLabel keyLabel = new JLabel ("press fire");
Gui(){
keyText.addKeyListener(this);
setSize(400,400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
add(keyLabel, BorderLayout.NORTH);
add (keyText, BorderLayout.CENTER);
}
public void keyTyped (KeyEvent e){
}
public void keyPressed (KeyEvent e){
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_F){
keyLabel.setText("you pressed the fire button");
}
else {
keyLabel.setText("you pressed the wrong button");
}
}
public void keyReleased (KeyEvent e){
}
public static void main(String[] args) {
Gui go = new Gui();
}
}
+Soren Bagley I will take a look at it tonight and see what I can find out
+EJ Media thanks a million
+Quinten Bredeveldt Can you explain why we need to implement KeyListener in the first place? I thought interfaces just say you need to implement these methods? So if I already typed those methods without typing 'implements KeyListener', there shouldn;t be a problem, right?
sir, i need u show class main pls
When are you going to make the mini game tutorials? :D
i want to add some stuff on JavaFX and then tie that into a game design tutorial
Ok.
instead of keyReleased i typed keyRelesed, and it took me about one hour to find the error. you should really put the source code in the description. often we will save a tons of time and nerves.
+stef wth This was the first series I made so I made the mistake of not saving some of the code - but I am in the process of trying to get the code uploaded for these videos. Every video that I post these days always has the code in the description.
ITS YOUR FAULT
I typed up this lesson's code and posted it on github here:
github.com/Taylomol001/Java-Tutorial-for-Beginners---43---GUI---Setting-up-KeyListener-Keyboard-Events-JLabel-and-Textbox
how come keycode is an int and not a char?
because not all keys have a char value
it doesnt fire at all, with no errors
Why is there a need for ( extends JFrame ) in the vid? The previous vids didn't inherit JFrame yet was able to use it.
And what does setLayout(layout) mean?
MrAirPork1 I don't have the code in front of me for 42 but I sort of remember I just needed to create an object for the method - we didnt need to extend it at the class level in that lesson
EJ Media Hmm your point made me realised that in 42 you made an object to access the methods. How come no object is needed here to access the JFrame methods?
MrAirPork1 There are different ways you can do things - in this case I wanted to access JFrame from the constructor - so at the time the object was created