Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: What method is called first: main() or paint() if my class extends JFrame?

  1. #1
    siluk is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default What method is called first: main() or paint() if my class extends JFrame?

    that's it..

  2. #2
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: What method is called first: main() or paint() if my class extends JFrame?

    main? I know that paint() is a methods inherited from class java.awt.Window. By, main... are you talking about public static void main(). If you are talking about that function, then, yes, it executes first. It is the start of the program. The object JFrame must be initialized before you start using paint, and you have to start a program before creating or initializing an Object, thus, JFrame's Paint() method cannot be used until the program starts.

    If it is another main() function thing, then explain what you mean by main(), in context.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  3. #3
    siluk is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: What method is called first: main() or paint() if my class extends JFrame?

    That was public static void main() I was referring to, thanks!

  4. #4
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: What method is called first: main() or paint() if my class extends JFrame?

    No, problem!
    My API:
    Java Code:
    cat > a.out || cat > main.class

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: What method is called first: main() or paint() if my class extends JFrame?

    Of course in 99.999% of cases you shouldn't be extending JFrame in the first place...
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: What method is called first: main() or paint() if my class extends JFrame?

    kammce, I (like most of the mods here) don't tend to answer PMs on Java issues, so I'll answer your question here, which seems a fair enough place to do it.

    Could you answer to me, why this is? I extend my JFrame some times. It depends on what I am doing. Why should extending JFrame be discouraged in Java programming. There seems to be quite a few Java programming practices that I do on a normal basis, that seem to be an discouraged on this forum even though I seem them used in Java Tutorials online and in the books that I have read in the past.

    Thank you for reading.
    A JFrame is a container. It's purpose is to hold stuff, generally a JPanel in which you then put everything else.
    Most people extend the JFrame just to put the code for building the JFrame in the new class:
    Java Code:
    class MYFrame extends JFrame {
        public MyFrame() {
            buildPanels();
        }
    }
    That there is not an extension of a JFrame.
    It adds no functionality to a JFrame, it simply holds the information for adding stuff to the JFrame.
    It's the equivalent of:
    Java Code:
    class MyArrayList extends ArrayList {
        public MyArrayList() {
            add(something);
            add(somethingElse);
        }
    }
    Which, hopefully, you'll agree is a silly thing to do.

    If you're not changing the functionality of the class (eg paintComponent on a JPanel, which is quite common) then you're not extending it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 08-23-2011, 11:04 PM
  2. Replies: 6
    Last Post: 04-27-2011, 05:27 AM
  3. Class that extends JFrame help
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 02:29 AM
  4. Replies: 5
    Last Post: 10-22-2010, 01:55 PM
  5. Why the paint() method is called two times ?
    By supremo in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 06:21 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
  •