Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree3Likes

Thread: Swing vs AWT

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Swing vs AWT

    Can someone give me some sample code of Swing and AWT code? They have to be separate. I'm trying to tell the difference but I can't seem to find anything with AWT...

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Swing vs AWT

    Have you tried google? Or the API? What have you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    I've tried google, all I can find is stuff for Swing and not AWT.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Swing vs AWT

    Quote Originally Posted by Army View Post
    I've tried google, all I can find is stuff for Swing and not AWT.
    There's a lot more stuff posted regarding Swing because it has largely supplanted AWT, but yet if you search, you will find plenty of posts showing AWT code. I don't think our posting any random "sample" code is going to help you much. If you have a specific question, please ask it, as we're much better at helping this way.

  5. #5
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    Well, can you atleast tell me the difference between them both? As in what is the difference in syntax of the code pertaining to one another.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Swing vs AWT

    Quote Originally Posted by Army View Post
    Well, can you atleast tell me the difference between them both? As in what is the difference in syntax of the code pertaining to one another.
    This has all been well discussed previously both here and on other sites. Surely you've Googled something like "difference between swing and awt" and have gone through some of the hits. Surely you're not asking us to re-enumerate that which has already been well discussed previously. Surely you'll instead tell us if you have any specific points of confusion.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Swing vs AWT

    The Java language sets the syntax for coding a program. Its the classes that are AWT or Swing. Many of the Swing classes are in the javax.swing package and have classnames that start with the letter: J
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    Never mind. You guys do some Yoda **** with my questions...
    Last edited by KevinWorkman; 06-19-2012 at 08:33 PM. Reason: Let's keep it PG, please!

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Swing vs AWT

    Quote Originally Posted by Army View Post
    Never mind. You guys do some Yoda **** with my questions...
    That's the spirit! All we've asked you to do was clarify what you're confused about so we can better help you. What part of the API has you confused? What did you find on google that didn't make sense?

    If you think it's unreasonable for us to ask what you've done so far before we help you, for free, in our spare time, then much luck to you in your programming endeavors...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Swing vs AWT

    I'm so happy Darryl told us the trick how to see the old version of a post ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    Java Code:
    package gui;
    
    import java.awt.*;
    import java.awt.event.*;
    	
    
    
    
    public class MouseClicks {
    	TextField objTF1;
    	TextField objTF2;
    	
    	public static void main(String [] args){
    		MouseClicks MC = new MouseClicks();	
    		
    		}
    	
    	public MouseClicks(){
    		Frame f = new Frame();
    		Button button  = new Button("Button :D");
    		button.setBounds(30, 30, 40, 10);
    		button.addMouseListener(new MyMouseListener());
    		
    
    		objTF1 = new TextField("Text 1", 100);
    		objTF2 = new TextField("Text 2", 100);
    		objTF1.setBounds(90, 70, 20, 10);
    		objTF2.setBounds(90, 70, 20, 10);
    			f.add(objTF1);
    			f.add(objTF2);
    			f.add(button);
    			f.addWindowListener(new WindowAdapter(){
    				public void windowClosing(WindowEvent we){
    					System.exit(0);
    				}
    			});
    		
    		}
    		f.setSize(600, 600);
    		f.setVisible(true);
    	}
    	class MyMouseListener extends MouseAdapter{
    		public void mouseClicked(MouseEvent me){
    			
    			
    		}
    	}
    Can someone tell me why it keeps showing that I have an error at

    Java Code:
    f.setSize(600, 600);
    f.setVisible(true);

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Swing vs AWT

    it keeps showing that I have an error
    Please post the full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    Java Code:
    package gui;
    
    import java.awt.*;
    import java.awt.event.*;
    	
    
    
    
    public class MouseClicks {
    	TextField objTF1;
    	TextArea objTF2;
    	
    	public static void main(String [] args){
    		MouseClicks MC = new MouseClicks();	
    		}
    	
    	public MouseClicks(){
    		Frame f = new Frame();
    		f.setSize(600, 600);
    		f.setVisible(true);
    		Button button  = new Button("Button :D");
    		button.setBounds(40, 30, 40, 10);
    		//button.addMouseListener(new MyMouseListener());
    		
    
    		objTF1 = new TextField("Text 1", 100);
    		objTF2 = new TextArea("Text 2", 100, 100);
    		objTF1.setBounds(90, 70, 20, 10);
    		
    			f.add(objTF1);
    			f.add(objTF2);
    			f.add(button);
    			f.addWindowListener(new WindowAdapter(){
    				public void windowClosing(WindowEvent we){
    					System.exit(0);
    				}
    			});
    		
    		}
    	}
    	/**class MyMouseListener extends MouseAdapter{
    		public void mouseClicked(MouseEvent me){
    			
    			
    		}
    	}
    	**/
    I got that error corrected but now, when i run it all I have is a very large button. Like my whole frame is a button.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Swing vs AWT

    The issue is one of the layout managers. You're adding a bunch of things to a Frame which by default uses BorderLayout. If you add components to a BorderLayout using container, it will add it BorderLayout.CENTER and will fill the whole container, covering anything that was added previously. The solution is to read up on the layout managers (link coming soon), and to use appropriate layouts for your GUI.

    Edit:
    Please see: Layout Manager Tutorial

  15. #15
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    Quote Originally Posted by Fubarable View Post
    The issue is one of the layout managers. You're adding a bunch of things to a Frame which by default uses BorderLayout. If you add components to a BorderLayout using container, it will add it BorderLayout.CENTER and will fill the whole container, covering anything that was added previously. The solution is to read up on the layout managers (link coming soon), and to use appropriate layouts for your GUI.

    Edit:
    Please see: Layout Manager Tutorial
    But using JPanels means that I need to use Swing, and I'm using AWT frames...

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Swing vs AWT

    Quote Originally Posted by Army View Post
    But using JPanels means that I need to use Swing, and I'm using AWT frames...
    I don't recall mentioning anything about JPanels in my post above. Please point it out if I am in error. The layout managers are a construct that is shared by both AWT and Swing.

  17. #17
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Swing vs AWT

    What the heck??? I agree 100% with you army.. Do you people know how annoying it is when you ask a question on a forum and people say "Go search it on Google?" Have you any idea how dumb you sound? Why is the forum existent then? If you're just gonna say "Go search for the answer elsewhere." Why run a damn forum and allow public access to ask questions??? "SURELY YOU MUST BE AN IDIOT!" Instead of doing your best to answer the question presented, you firstly look for faults, finding reason to make the question out to be an unworthy one, then follow up by beating around the bush, never addressing the question properly and giving vague answers that doesn't even scratch the surface of the question! So what the question is answered elsewhere, answer it a-frigging-gain! Or better yet, if your'e so sure it's answered elsewhere, post the damn link! Simple. If you don't have anything good to say/contribute, don't say anything at all. I've only been a member of this forum just recently and I've already seen this kinda crap dozens of times, and it's always coming from the "Moderators" supposedly the best this site has to offer... REALITY CHECK: It's not cool, not cool at all.
    "And that's not all Linus.."
    - Agent Caldwell.
    (Nothing personal, someone had to tell you people)

    Anyways, to your question bro...
    AWT (Abstract Window Toolkit) and Swing are both basically the same thing in that they are both GUI (Graphical User Interface) Components. AWT was developed earlier than swing, so you can say swing is a more sophisticated (modern if you may) version of AWT. Swing overall is more lightweight and friendly to the user because in addition to things like buttons, and check boxes, Swing provides even more advanced components such as tabbed panel, scroll panes, trees, tables, and lists. So it's a bit more flexible and robust. Compared to AWT where you have to render each component (TextField, Button, TextArea, etc..) that's specific to that particular dialog you're creating.

    Here's a simple greeting program using the AWT:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    public class AWTTextbox extends Frame implements ActionListener
    {
        public static void main (String args [])
        {
            new AWTTextbox ();
        }
        DialogTest dt;
        static TextField tf1;
        public AWTTextbox ()
        {
            Button btn = new Button ("Click Here.");
            tf1 = new TextField (10);
            btn.addActionListener (this);
            add (btn, "North");
            add (tf1, "South");
            tf1.setBackground (Color.green);
    
            dt = new DialogTest (this);
    
            setSize (200, 80);
            setVisible (true);
        }
    
        public void actionPerformed (ActionEvent e)
        {
            dt.setVisible (true);
    
        }
    
        public static void confirm (String name)
        {
            tf1.setText ("Hello! " + name);
        }
    }
    
    class DialogTest extends Dialog implements ActionListener
    {
        TextField tf2;
        public DialogTest (Frame f1)
        {
            super (f1, "AWT Dialog Box", true);
            setLayout (new FlowLayout ());
            Button btn1 = new Button ("Enter");
            tf2 = new TextField (10);
            btn1.addActionListener (this);
            add (new Label ("What is your Name?"));
            add (tf2);
            add (btn1);
    
            setSize (200, 125);
        }
    
        public void actionPerformed (ActionEvent e)
        {
            AWTTextbox.confirm (tf2.getText ());
            setVisible (false);
        }
    }
    And here's a program doing pretty much the same thing using swing:
    Java Code:
    import javax.swing.*;
    public class swingGUI {
        public static void main(String[] args) {
           
            String name = JOptionPane.showInputDialog("What is your name?");
            JOptionPane.showMessageDialog(null, "Hello " + name + "!");         
        }
    }
    Right away you'll notice that the swing code is nearly 10 times shorter. Mainly because in the code for the AWT program there's creating and rendering of buttons, TextFieds, ActionListners, button positions, size, etc.. The program even calls and runs through several user-defined methods; a tedious and sometimes complex task especially for a programmer new to java.
    In the case of swing, all that is done automatically, so it's simple as JOptionPane.showxxxxxDialog! And even still, the appearance of swing is customizeable in many ways from the imageIcons, buttons, labels etc... (I love swing, so I'm a bit biased for it). Anyways that being said, it's a good practice to use a combination of AWT and swing in your GUI.


    Images of AWT Dialogs:

    Swing vs AWT-javaawt1.jpgSwing vs AWT-javaawt2.jpg

    Swing vs AWT-javaawt3.jpg


    Images of swing Dialogs:

    Swing vs AWT-swing1.jpgSwing vs AWT-swing2.jpg

    And there it is... your full answer, simple and uncomplicated.
    CHEERS!

    "The great majority of mankind are satisfied with appearances, as though they were realities, and are often more influenced by the things that seem than by those that are."
    - Niccolò Machiavelli.
    Attached Thumbnails Attached Thumbnails Swing vs AWT-javaawt2.jpg  
    Last edited by Tha Boss; 06-20-2012 at 01:29 AM.

    E.Hughes
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

  18. #18
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Swing vs AWT

    Wow, thanks dude! I was kind of taken aback by how much less code Swing is compared to AWT!

    I have another question, how do you use an Applet and Swing together? Since you need certain methods like init(),start(), paint(), etc. how would you use the components form Swing when to me it seems like you can't use your f.add() or normal object items...
    Last edited by Army; 06-20-2012 at 01:10 AM.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Swing vs AWT

    There is a difference between helping someone learn how to solve programming problems and providing them with the solution.
    Heard the one about the the hungry man and giving him a fish vs teaching him how to fish.
    The more you exercise your brain the stronger it will get. Copying and pasting does not provide much thought.
    You have to make the mistakes to learn. You have to try all the different deadends to be a better programmer.

    I'm not here to solve people's problems. I want to help them learn how to solve problems on their own.
    Several things an OP should know: How to find things with google.
    How to read the API doc.
    How to ask a question and provide all needed info to describe the problem.
    Last edited by Norm; 06-20-2012 at 01:17 AM.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Swing vs AWT

    Yeah yeah yeah.... I expected one of you mods to be saying that sooner or later...
    But get this... teaching someone is showing them by example... no beating around the bush when they ask you a question and pointing them back and forth giving waffled tidbits of information they cant work with. Any case he's not the only one that's going to benefit.. I made it very informative. Maybe even better than professional tutorials you'll find elsewhere...

    "Don't hate... Appreciate"
    DISCUSSION CONTINUED ON PAGE 2!
    Last edited by Tha Boss; 06-20-2012 at 02:33 AM.

    E.Hughes
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

Page 1 of 2 12 LastLast

Similar Threads

  1. I need help about swing...
    By CANN1 in forum AWT / Swing
    Replies: 2
    Last Post: 03-31-2012, 02:50 PM
  2. Swing To Jsp
    By jim in forum Advanced Java
    Replies: 1
    Last Post: 10-09-2011, 11:51 PM
  3. help with Swing (GUI)
    By Bagzli in forum New To Java
    Replies: 8
    Last Post: 04-04-2011, 12:18 AM
  4. where is the swing jar?
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:58 PM
  5. map javax.swing.text.Element to javax.swing.text.View
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •