Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By PhHein

Thread: Swing GUI programming question

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

    Default Swing GUI programming question

    I find myself asking these two questions because I see them as relating. First question is; I always write
    Java Code:
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    (where f is a JFrame object)
    to set the close for the JFrame. What I don't get about this is what is going on in the parenthesis. I looked in the Java Documentation, and it says an int goes inside. In that case, I don't really get what the word JFrame is doing there. Overall, please explain what is inside the parenthesis of that line and why it has to be there.

    The second question is a generic question. I notice a lot of times an object will be created, and as its parameter, you will have to instantiate an object.
    an example would be
    Java Code:
    Class f = new Class(new Object)
    What does it mean when an object gets created inside of a new object? Why is putting
    Java Code:
    new Object
    ever necessary when concerning the two parenthesis?

    Sorry for all the mumbo-jumbo, trying to clear everything up

    As always, thanks for any and all answers

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Swing GUI programming question

    Hi there,
    have a look at the source code for JFrame and you'll see
    Java Code:
    public static final int EXIT_ON_CLOSE = 3;
    For your second question: there are a lot of classes having parameterized constructors. One case is this one:
    Java Code:
    Scanner s = new Scanner(new File("mytext.txt"));
    It's even a triple. This constructor of Scanner needs a File object. The File constructor needs a String, containing the path to the file.
    sehudson likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default Re: Swing GUI programming question

    To answer your first question:

    Java Code:
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    The setDefaultCloseOperation is the name of a method being called on the JFrame (called f in this case).
    This method sets the operation that will happen by default when a close is initiated on the JFrame(i.e., the user hits the 'X').
    The method requires as a parameter an int that represents the operation. The JFrame class has a static int defined called EXIT_ON_CLOSE, and
    that int is what is being passed to the method. Since the method takes an int. EXIT_ON_CLOSE is the exit application default window close operation.


    Your 2nd question has multiple parts.

    What does it mean when an object gets created inside of a new object?
    Some classes have a constructor that requires that an instance of an object be passed in.
    So, if you use the below code as an example:

    Java Code:
    public class Car {
      private Engine engine;
    
      public Car(Engine engine) {
        this.engine = engine;
     }
    }
    The constructor for the Car class requires that an Engine be passed to it. So in order to create a Car, I could do the following:

    Java Code:
    Car honda = new Car(new Engine());
    Or, we could do the following:

    Java Code:
    Engine hondaEngine = new Engine();
    Car honda = new Car(hondaEngine);
    Either way is valid.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,689
    Rep Power
    5

    Default Re: Swing GUI programming question

    No need to look at source code to see what static members a class has by the way, its in the javadocs:

    JFrame (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Swing GUI programming question

    The first one requires an int to choose which kind of close operation to perform. In the JFrame class there are several public static final ints, and EXIT_ON_CLOSE is one of them.

    Also, the reason you need to instantiate a new Class object in some constructors is because that class needs to use certain things that the other class contains.

Similar Threads

  1. new to programming, need help, simple question.
    By vanzi in forum New To Java
    Replies: 1
    Last Post: 10-17-2013, 12:20 PM
  2. Java programming question, please help
    By DolphinsNum1 in forum New To Java
    Replies: 1
    Last Post: 02-17-2013, 03:03 AM
  3. Replies: 6
    Last Post: 05-14-2012, 12:59 PM
  4. Replies: 1
    Last Post: 10-31-2010, 07:41 PM
  5. Framework for swing programming
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 02-28-2009, 08:02 AM

Posting Permissions

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