Results 1 to 9 of 9
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default question on swing coding?

    Java Code:
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    
    public class Example extends JFrame {
    
        public Example() { // what does this do? Why is it necessary? why does it have parentheses?
            initUI();       // what does this do?also, Why are there parentheses?
        }
    
        public final void initUI() {  // what is "final" in this? Why is it final?
    
            JPanel panel = new JPanel();
            getContentPane().add(panel);
    
            panel.setLayout(null);
            panel.setToolTipText("A Panel container");
    
            JButton button = new JButton("Button");
            button.setBounds(100, 60, 100, 30);
            button.setToolTipText("A button component");
    
            panel.add(button);
    
            setTitle("Tooltip");
            setSize(300, 200);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() { //absolutely no idea what this does...
                public void run() {
                    Example ex = new Example(); */what does this do? Why is it necessary? I dont understand this.\*
                    ex.setVisible(true);
                }
            });
        }
    }
    also, I dont quite understand the use of brackets in this coding. Could you also explain that? Thanks in advanced guys.
    Last edited by MW130; 01-05-2013 at 04:49 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: question on swing coding?

    As you've been advised in your other posts in more words than this - take a step back. I get the feeling you are biting off way more than you can chew - break things down so that you can learn one step at a time. One of your best bets is to start learning from the basics, then build up - every one of your question can be answered in the following website, should you choose to take the time to step through it
    The Java™ Tutorials

  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: question on swing coding?

    I know that the following just creates an object which is a JFrame. A JFrame is Java's class for a window.

    Java Code:
    	public static void main(String[] args) {
    		Example ex = new Example(); // <<<< this creates an Example obect which is a JFrame (aka a window)
    		ex.setVisible(true);
    	}
    All of the other business about Runnable, the run() method and invokeLater() all have to do about Threads.

    Here is a link to the documentation of invokeLater()


    Here is a link to a tutorial on Threads:
    Defining and Starting a Thread (The Java™ Tutorials > Essential Classes > Concurrency)

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: question on swing coding?

    I know that the following just creates an object which is a JFrame. A JFrame is Java's class for a window.

    Java Code:
    	public static void main(String[] args) {
    		Example ex = new Example(); // <<<< this creates an Example obect which is a JFrame (aka a window)
    		ex.setVisible(true);
    	}
    All of the other business about Runnable, the run() method and invokeLater() all have to do about Threads.

    I think that the window will work in the simplified main() method that I wrote. I'm not sure that I understand either why we would use invokeLater()

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: question on swing coding?

    I'm not sure that I understand either why we would use invokeLater()
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

    Default Re: question on swing coding?

    Quote Originally Posted by kaydell2 View Post
    ...
    All of the other business about Runnable, the run() method and invokeLater() all have to do about Threads.

    I think that the window will work in the simplified main() method that I wrote. I'm not sure that I understand either why we would use invokeLater()
    Using SwingUtilities.invokeLater(new Runnable() {....}); guarantees that the code inside of the Runnable will be queued onto the Swing event thread. Not doing this when creating your JFrame/GUI will usually work, but it may not always work and it may fail in both a predictable and unpredictable fashion, the former I've seen when using certain look and feels, and the latter is at especial risk since we're dealing with a threading issue.

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: question on swing coding?

    ok but one question when it says example ex= new example, why does it say the "ex" and why does it say "new" example? Thanks

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: question on swing coding?

    In the very first response in this thread, you've been advised to go through the tutorial. A forum is no substitute for systematic learning.

    If you consistently ignore all attempts to help you by pointing you to learning material, I'll close this thread as it's going nowhere.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: question on swing coding?

    Quote Originally Posted by MW130 View Post
    ok but one question when it says example ex= new example, why does it say the "ex" and why does it say "new" example? Thanks
    Yeah, you really need to back up and start the introductory tutorials from the very beginning else you will end up with nothing but frustration. To borrow from medicine, you can't try to learn neurosurgery without first learning basic anatomy and physiology.

Similar Threads

  1. Replies: 8
    Last Post: 05-03-2012, 06:37 PM
  2. Huffman coding algorithm question?
    By knguye88 in forum New To Java
    Replies: 1
    Last Post: 03-12-2012, 09:06 AM
  3. Beginner Question About Java Coding
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 03-09-2011, 09:41 PM
  4. question about coding conventions
    By gib65 in forum New To Java
    Replies: 8
    Last Post: 08-05-2010, 04:24 AM
  5. swing coding
    By priya_gurnani166 in forum AWT / Swing
    Replies: 8
    Last Post: 01-30-2009, 07:18 AM

Tags for this Thread

Posting Permissions

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