-
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.
Code :
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 :)
Code :
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
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:
Code :
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:
So the object passes into myMethod() is useless, it never be used.
And
Code :
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
-
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
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).
Quote:
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:
Code :
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
-
Re: Do we need AWT to learn Swing? + One more question
Learnt a better way to answer now from you lol
Quote:
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?
Quote:
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:
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 :)
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
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.
Quote:
And this would show my window. Is that correct?
SURE.
Quote:
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
-
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:
Code :
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);
}
}
-
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
ha.minh.nam
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
beer-in-box
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:
Code :
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
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
beer-in-box
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
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
KevinWorkman
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
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
beer-in-box
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
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
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
KevinWorkman
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
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
beer-in-box
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
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?
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
KevinWorkman
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
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
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
beer-in-box
Code :
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
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
KevinWorkman
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
ha.minh.nam
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
-
Re: Do we need AWT to learn Swing? + One more question
Thanks Kevin for the links.
-
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!
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
punkz_N0t_Dead
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.
-
Re: Do we need AWT to learn Swing? + One more question
Quote:
Originally Posted by
beer-in-box
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.