Java Tutorial for Beginners - 42 - GUI - Setting up Listener, User Events and Input

preview_player
Показать описание
In this tutorial we will go over how to make our program respond to user input
Рекомендации по теме
Комментарии
Автор

Thank you so much for uploading this. I've been looking all over for a good gui tutorial and none of them explain in such great detail whats going on and why you're doing what you do. I hope you continue this series

kwajomensah
Автор

Your videos are so informative, I hope you'll keep making more of them!

aysunrhn
Автор

Thank You for your Awesome Explanation & God Bless You.

xxtreemtv
Автор

suppose we got 3 separate buttons and we need 3 separate messages to be displayed, in this case how to map different event to corresponding buttons?

deepinurheart
Автор

Do you know why I can't use interface unless I make the class abstract?

jun
Автор

Sir
JButton button is a global type of variable declared outside method. How come we can access it inside a method without creating an object of GUI method

deepinurheart
Автор

Thank you very much for these GUI videos. It is starting to make sense. There are so many J-classes and Event handlers that it becomes intimidating, but the progression from video 41 into 42 makes at least this event handling scenario clear.  I have two questions at this point if you don't mind.  First, is there advantage to doing the button variable declaration at class level and then the initialization of the object within the method rather than doing the whole thing at class level?  

 Second, I looked at Oracles' list of "Listeners".  From the descriptions, it looks like they can have overlapping functions (like the various MouseListeners)... is this right, or is each Listener specific to different, non-overlapping events?

punated
Автор

can't I create a JFrame form, drag and drop buttons, labels and let my ide create the code for me?
and thanks for the videos I'm really enjoying java so much because of you
.

mostafaattia
Автор

Thanks a lot Ej. Please do upload Enumerations tutorial as well. There is not even one tutorial there on YouTube which explains clearly the concept of enumerations. No one is explaining clearly the logic, why we use it..etc.Thanks again.

rbflowin_TV
Автор

I'm a little bit confused now. How does the method actionPerformed get performed by calling In the video about interfaces you created the superclass and the interface seperately. But here you did create them in the same file and I don't know which method belongs to which.

thtguyy
Автор

I've been struggling with learning Core Java for quite a while. I have been closely following your excellent videos and have a much better grasp so far till now, all Thanks to you!
In this video, how is invoking the interface method actionPerformed(ActionEvent event)?
Also, it appears that this interface method requires an argument of type ActionEvent as in it's signature. What value did we pass here? How is it you coded the method body without using the Actionevent argument that it needs and just used a simple System.out.println() command? SHouldn't the argument be used somewhere in it's body?
Many Thanks!

sraj
Автор

Does this work on jcreator? I copied all the codes yet my frame didn't show anything. But label "Swing; and blank frame. :(

mhelmejidana
Автор

Another way I've seen ActionListener() assigned to components, is not to have the class implement ActionListener() (which then has the actionPerformed() method in the class body), but rather do so by .addActionListener(new ActionListener(){ actionPerformed() in here)}, where each creation/assignment of the Listener to a component forces you to implement the actionPerformed()method. With that said, it's easy to see how each component will be assigned a unique event since the event is tied directly to the assigning of the Listener to it. How do we assign unique events when the class implements the Listener and there is only one actionPerformed() in the class?

punated
Автор

I tried setting up the Action Listener to my pre-existing code. However, i get a error message when i type "implements ActionListener" to my Item class.
It says: "Item is not abstract and does not overide abstract method actionPerformed(ActionEvent) in ActionListener."
It gives me two options, either netbeans can make "Item" abstract or "Implement all abstract methods"
if I click implement it generates this method:

@Override
    public void actionPerformed(ActionEvent e) {
        throw new supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

I noticed you do not have this method and you're GUI class is not abstract either.
If you or anyone can shine some light on this issue it would be much appreciated since I am stumped. For now I have gone with the latter and implemented the abstract classes but I am unsure why.

I apologize for the length of this comment or if it causes any confusion,  because it caused me a couple headaches trying to research else where.


My Code:

Item.java

package mainProject;
import javax.swing.*;
import java.awt.event.*;

public class Item implements ActionListener {
    private int itemQuantity;
    private String itemName;
    private double itemPrice;
  
    public void ActionPerformed(ActionEvent event){
        
        
    }
    
    public Item(){
       itemQuantity = 0;  
    }
    
    public int addItem(int addItem){
        this.itemQuantity = this.itemQuantity + addItem;
        return itemQuantity;
    }
    
    public String addName(String addName){
        this.itemName = addName;
        return itemName;
    }
    
    public double addPrice(double addPrice){
        
        this.itemPrice = addPrice;
        return itemPrice;
    }
    
    public void displayItemInfo (){
        System.out.println("Item Quantity: " + itemQuantity);
        System.out.println(itemName);
        System.out.println("Price: " + "$" + itemPrice);
    }
}

DavidGarcia-uzox
Автор

Hello first thank you very much on this Tutorial its very helpful
its just i have a problem when i made my Gui class it say : Gui is abstract and it doesn't overwrite abstract .... everything else work just fine!
pls help no matter what i do it say that
thanks

rodyabd
Автор

i don't get the Jbutton button global varaible. Why is it a variabe?
And why does declaring button as a new object in createAndShowGui method doesn't need JButton before it like the usual (JButton button = new JButton("Submit");

MrAirPork