Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Do we need AWT to learn Swing? + One more question

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Do we need AWT to learn Swing? + One more question

    Hi,
    I learnt that Java had AWT in the first place, then Swing was developed. When I took a look at Java book by Herbert Schildt, he says that we should know AWT to learn Swing better. Is this really that necessary?

    Second, I have some codes. I was just trying to form a window. Not filling it or making my app usable. Here is the code:
    This is the class I build my window.
    import java.awt.*;
    public class myWindow extends Frame{
    	Frame pencere = new Frame();
     
    	public void myMethod(Frame f){
    		f = pencere;
    		f.setSize(500, 600);
    		f.setVisible(true);
     
    	}
     
    }

    This is the main class which should 'initialize' my window. At least I guess so
    import java.awt.*;
     
    public class myFirstClass extends Frame{
     
    	public static void main(String[] args) {
    		myWindow pencerem = new myWindow();
    		pencerem.myWindow(new Frame("Did it work?"));
    	}
     
    }

    I know it has problems like it won't close I should have added something to make the window disappear. But this was just an experiment.
    For now, what I want to know is that:
    With this code [pencerem.myWindow(new Frame("Did it work?"));], I set my window(frame) and it shows up. But I don't know what my window should be called in the main method. How do I know that? Or how could I put this in a better way?


    Thanks in advance, for all your comments and helps. They are much appreciated.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    I learnt that Java had AWT in the first place, then Swing was developed. When I took a look at Java book by Herbert Schildt, he says that we should know AWT to learn Swing better. Is this really that necessary?
    Don't you believe the author?


    Your code snippet:

    import java.awt.*;
    public class myWindow extends Frame{
    	Frame pencere = new Frame();
     
    	public void myMethod(Frame f){
    		f = pencere;
    		f.setSize(500, 600);
    		f.setVisible(true);
     
    	}
     
    }

    The code above is bad because it violates one of general rules: changing argument reference in method body:

    f = pencere;

    So the object passes into myMethod() is useless, it never be used.

    And

    public class myWindow extends Frame{
    	Frame pencere = new Frame();

    the myWindow already extends a Frame, then you create another Frame object in the class, this is useless also.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:32 PM.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Thank you for your time ha.minh.nam.

    So, I have to learn AWT too

    About my code, you said I shouldn't change argument reference in method body. But I can change it in the constructors, right? I guess I thought myMethod as a constructor.

    Second, you said I don't need to create another Frame object if myWindow already extends a Frame. So then, how would I set properties of my window without creating one object for it? I just can't think how to do that.

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    About my code, you said I shouldn't change argument reference in method body. But I can change it in the constructors, right? I guess I thought myMethod as a constructor.
    It does not matter either constructor or normal method. Doing so means you pass an object but not use it (you assign the passing object reference to another object).

    Second, you said I don't need to create another Frame object if myWindow already extends a Frame. So then, how would I set properties of my window without creating one object for it? I just can't think how to do that.
    Your class extends Frame so it inherits all the Frame's properties and methods, that means you can call Frame's method directly in your class, like this:

    import java.awt.*;
    public class myWindow extends Frame{
    	public void myMethod(){
     
    		setSize(500, 600);
    		setVisible(true);
     
    	}
     
    }

    myMethod is a trivial method, not a constructor.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:33 PM.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Learnt a better way to answer now from you lol
    It does not matter either constructor or normal method. Doing so means you pass an object but not use it (you assign the passing object reference to another object).
    I remember that in some videos I learnt from, they passed something. Is it possible that they passed only primitive variables like int, long etc? I mean is this wrong in every case or it has some usage areas?
    Your class extends Frame so it inherits all the Frame's properties and methods, that means you can call Frame's method directly in your class, like this:
    Okay, so if I want to use your window in my main method, I write this code:
    public static void main(String[] args) {
       myWindow exampleWindow = new myWindow();
       exampleWindow.myMethod();
    }
    And this would show my window. Is that correct?

    And if we use a constructor instead of myMethod, we could do this easier. I learnt the difference between method and constructor more clearly now.

    How about passing variables and/or objects to methods like I did? Does this thing I did have a place? I guess I should type some code about it, so I can make my question clear...

    BTW, thank you again. You have been very helpful to me. I feel I am improving just right now

  6. #6
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Is it possible that they passed only primitive variables like int, long etc? I mean is this wrong in every case or it has some usage areas?
    Yes, primitive variables are passed as copies, unlike object references. It is not about wrong or right, it is a bad practice and should be avoided.

    And this would show my window. Is that correct?
    SURE.

    How about passing variables and/or objects to methods like I did? Does this thing I did have a place? I guess I should type some code about it, so I can make my question clear...
    No one prevents you from passing variables, you should avoid bad practice however.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:33 PM.

  7. The Following User Says Thank You to ha.minh.nam For This Useful Post:

    beer-in-box (August 25th, 2011)

  8. #7
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    I was looking for a code from the videos and tutorials which I try to learn from to show you. Then I realized an embarrassing mistake I have done. What I wanted to do was not change "pencere" to "f". They do the reverse to modify pencere in methods and constructors. So, because I doubt myself now, could you please tell me if this would be right thing to do:

    import java.awt.*;
    public class myWindow extends Frame{
    	Frame pencere = new Frame();
     
    	public void myMethod(Frame f){
    		pencere = f;
    		f.setSize(500, 600);
    		f.setVisible(true);
     
    	}
    }

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    I'm just going to add that, IMHO, if you're looking at something like "do I have to learn this?" then you're doing it wrong. Learn about things you think are interesting. If you think Swing is interesting, then you should think AWT is interesting, because it's the foundation of Swing. If you don't think Swing is interesting, then why bother learning it? Why would you ever hesitate to learn something? JUST GO LEARN IT.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by ha.minh.nam View Post
    Yes, primitive variables are passed as copies, unlike object references.
    This is misleading. Everything in Java is passed by value. I repeat: everything in Java is passed by value.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by beer-in-box View Post
    I was looking for a code from the videos and tutorials which I try to learn from to show you. Then I realized an embarrassing mistake I have done. What I wanted to do was not change "pencere" to "f". They do the reverse to modify pencere in methods and constructors. So, because I doubt myself now, could you please tell me if this would be right thing to do:

    import java.awt.*;
    public class myWindow extends Frame{
    	Frame pencere = new Frame();
     
    	public void myMethod(Frame f){
    		pencere = f;
    		f.setSize(500, 600);
    		f.setVisible(true);
     
    	}
    }
    What do you people say about this code? Is "pencere = f;" right here? Or is it some bad coding too? And if so, why?

    Quote Originally Posted by KevinWorkman View Post
    I'm just going to add that, IMHO, if you're looking at something like "do I have to learn this?" then you're doing it wrong. Learn about things you think are interesting. If you think Swing is interesting, then you should think AWT is interesting, because it's the foundation of Swing. If you don't think Swing is interesting, then why bother learning it? Why would you ever hesitate to learn something? JUST GO LEARN IT.
    Thank you for your comment on that. You are absolutely right. But here is the thing: I was stuck at AWT and couldn't do what I wanted to do. And the authors of the books I read were saying that, although AWT is the base of Swing, it is also not used these days.
    And I just wanted to ask. Why did I want this? Because I had the fear of not being able to learn this.

    And now, I am on my way to learn this as much as I can. And I took one step into Swing too.

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by beer-in-box View Post
    What do you people say about this code? Is "pencere = f;" right here? Or is it some bad coding too? And if so, why?
    That really depends on what you're going for. I will point out that it doesn't make a ton of sense to initialize pencere to a new JFrame if you're just going to replace it by the instance passed into the constructor.


    Quote Originally Posted by beer-in-box View Post
    Thank you for your comment on that. You are absolutely right. But here is the thing: I was stuck at AWT and couldn't do what I wanted to do. And the authors of the books I read were saying that, although AWT is the base of Swing, it is also not used these days.
    And I just wanted to ask. Why did I want this? Because I had the fear of not being able to learn this.

    And now, I am on my way to learn this as much as I can. And I took one step into Swing too.
    I learned Swing before I worried about what exactly AWT was- they're so interrelated that it's hard to learn one without picking up on the other. They aren't separate. They work together and are related, so learning one IS learning the other, at least a little bit. And of course AWT is still used today.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. The Following User Says Thank You to KevinWorkman For This Useful Post:

    beer-in-box (August 25th, 2011)

  14. #12
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by KevinWorkman View Post
    That really depends on what you're going for. I will point out that it doesn't make a ton of sense to initialize pencere to a new JFrame if you're just going to replace it by the instance passed into the constructor.
    Hmm. I see what you are telling now But still I am not so clear. What if I didn't initialize pencere and just declare it? Would it be smarter then? Sorry, still noob about some stuff.


    Quote Originally Posted by KevinWorkman View Post
    I learned Swing before I worried about what exactly AWT was- they're so interrelated that it's hard to learn one without picking up on the other. They aren't separate. They work together and are related, so learning one IS learning the other, at least a little bit. And of course AWT is still used today.
    Trying to learn AWT helped me, because I couldn't find a nice guide about Swing. People have their own way of coding. They feel content when they use it. But I don't So, when I read two different articles, I get confused. But after I read about AWT, everything is clearer.
    What I suspected was that maybe, maybe they weren't so related. I mean I still handle events with AWT Event class for the most of time. We can't say they aren't related.

    I just didn't know much when I tried to learn Swing/AWT.

  15. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by beer-in-box View Post
    Hmm. I see what you are telling now But still I am not so clear. What if I didn't initialize pencere and just declare it? Would it be smarter then? Sorry, still noob about some stuff.
    What happened when you tried that? I wouldn't worry too much about doing things the best way. If it works, it works. You'll come back in a couple months and kick yourself for half the things you're doing anyway. Don't waste time stressing over stuff that doesn't really matter. Learn the ideas before you worry about best practices.



    Quote Originally Posted by beer-in-box View Post
    Trying to learn AWT helped me, because I couldn't find a nice guide about Swing.
    Are you kidding: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Quote Originally Posted by beer-in-box View Post
    What I suspected was that maybe, maybe they weren't so related. I mean I still handle events with AWT Event class for the most of time. We can't say they aren't related.
    They are very related.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #14
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by KevinWorkman View Post
    What happened when you tried that? I wouldn't worry too much about doing things the best way. If it works, it works. You'll come back in a couple months and kick yourself for half the things you're doing anyway. Don't waste time stressing over stuff that doesn't really matter. Learn the ideas before you worry about best practices.
    Basicly, you ssay that one can't learn perfect in the first time and this process is never gonna finish I get you now.

    Quote Originally Posted by KevinWorkman View Post
    I was trying to find it in my own language and couldn't find That's how all this started.

    I think we can consider this topic as "SOLVED" now. Thank you guys. For your support, help and opinions.

  17. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by beer-in-box View Post
    Basicly, you ssay that one can't learn perfect in the first time and this process is never gonna finish I get you now.
    Yep, and that never changes. I've been programming for almost 10 years now, and I'll still look at something I wrote a couple months ago and start shaking my head.

    Quote Originally Posted by beer-in-box View Post
    I was trying to find it in my own language and couldn't find That's how all this started.
    Ohh okay, that's fair. We have a bunch of people from all over the place on these forums, maybe you could start a topic asking for resources that are in your native language?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. The Following User Says Thank You to KevinWorkman For This Useful Post:

    beer-in-box (August 25th, 2011)

  19. #16
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by KevinWorkman View Post
    Yep, and that never changes. I've been programming for almost 10 years now, and I'll still look at something I wrote a couple months ago and start shaking my head.
    Since I am a young guy and my lessons have nothing to do with programming (If someone tells me that medicine is related to programming, then it is something different), for me it will take longer. Damn...


    Quote Originally Posted by KevinWorkman View Post
    Ohh okay, that's fair. We have a bunch of people from all over the place on these forums, maybe you could start a topic asking for resources that are in your native language?
    I should buy a book. And a translated one. People say that translations make it more difficult Despite that fact, I am planning to get one (Herbert Schildt -translated of course). But before doing this, I want to be sure that I am really interested in this stuff, and I want to learn if I really can do with this.

    Thank you for your support Kevin. Again.

    One month ago, all I wanted to do is to code a GUI based Java calculator or maybe a library database for myself. I was planning to take small steps. But here I am, getting deeper and deeper. I gotta tell you, that calculator is not an easy thing to code lol

  20. #17
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by beer-in-box View Post

    import java.awt.*;
    public class myWindow extends Frame{
    	Frame pencere = new Frame();
     
    	public void myMethod(Frame f){
    		pencere = f;
    		f.setSize(500, 600);
    		f.setVisible(true);
     
    	}
    }
    Yes, that's fine.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:34 PM.

  21. The Following User Says Thank You to ha.minh.nam For This Useful Post:

    beer-in-box (August 27th, 2011)

  22. #18
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by KevinWorkman View Post
    This is misleading. Everything in Java is passed by value. I repeat: everything in Java is passed by value.
    Thanks Kevin for reminding me about this fundamental stuff.
    Actually everything is passed by value. The value here is memory address of the variable right?

    Please correct me if I am wrong.

    Thanks.

  23. #19
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by ha.minh.nam View Post
    Thanks Kevin for reminding me about this fundamental stuff.
    Actually everything is passed by value. The value here is memory address of the variable right?

    Please correct me if I am wrong.

    Thanks.
    Recommended reading: JavaRanch Campfire - Cup Size: a Story About Variables
    And its follow-up: JavaRanch Campfire - Pass By Value, Please
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  24. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ha.minh.nam (August 30th, 2011)

  25. #20
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Thanks Kevin for the links.

  26. #21
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    what a noob.... you are extending Frame class so Y making an object of the same class inside that class, to set its properties you can do it using "this" keyword in the constructorlike...
    this.setSize(new Dimension(800,600));
    this.setVisible(true);
    and etcetra!

  27. #22
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by punkz_N0t_Dead View Post
    what a noob.... you are extending Frame class so Y making an object of the same class inside that class, to set its properties you can do it using "this" keyword in the constructorlike...
    this.setSize(new Dimension(800,600));
    this.setVisible(true);
    and etcetra!
    I think "this" is not needed when you do it inside of the constructor.

  28. #23
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,610
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Do we need AWT to learn Swing? + One more question

    Quote Originally Posted by beer-in-box View Post
    I think "this" is not needed when you do it inside of the constructor.
    It's actually never needed for things like that, unless you're in a local scope that has variables hiding global variables with the same name. Try it out!

    punkz_N0t_Dead- Be nice, there's nothing wrong with being new to this stuff. You should especially be nice, as the advice you're giving shows that you yourself are also a "noob". Don't insult people.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Do I HAVE to learn Generics?
    By hexwind in forum Collections and Generics
    Replies: 3
    Last Post: August 14th, 2011, 12:16 PM
  2. [SOLVED] Trying to learn and I'm stuck
    By knightknight in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 1st, 2011, 05:17 PM
  3. New to this..so lost trying to learn...
    By ryan1234 in forum Object Oriented Programming
    Replies: 5
    Last Post: September 21st, 2010, 06:35 PM
  4. here to learn and grow
    By cejay in forum Member Introductions
    Replies: 2
    Last Post: March 1st, 2010, 03:04 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM