Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: JPanel new FlowLayout

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanel new FlowLayout

    I know how to use layouts and everything, but my questions is, why do I have to do a new on the FlowLayout?
    myPane.setLayout(new FlowLayout());

    I really just need an explanation on why it isn't.

    myPane.setLayout(FlowLayout());

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,829
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: JPanel new FlowLayout

    You use new to create a new instance of a class. Using FlowLayout() without new looks like a call to a method not a call to make a new instance of a class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel new FlowLayout

    Hey Norm, thanks for the response. Im still a little confused then. According to the code, setLayout is a method right? How does a method take a class an know what to do with it? I mean would the method setLayout look something like this?

    public void setLayout(object layout)
    {
    layout newlayout = new layout();
    (other instructions)
    }

    Now im not even sure that would work, but its the only way I can think of with my very very very very small experience with java, to access a class from a method inside another class, called from the another class.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,829
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: JPanel new FlowLayout

    How does a method take a class and know what to do with it?
    That would be what the programmer that wrote the method would define and take care of. By class I assume you mean an instance of a class.

    would the method setLayout look something like this?
    public void setLayout(object layout)

    That defines a method named setLayout that takes a parameter of type object named layout. Somewhere the class named object would have to be defined.

    The next line is confusing because it has a classname the same as the parameter's name:
    layout newlayout = new layout();
    The syntax of that line says: Define a variable of type layout with a name of newlayout and assign it the value of a new layout object.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel new FlowLayout

    Alright thanks for the reply. I tried the scenario out that I suggested to no success. It said that object layout could not be found because the class did not exist. The only thing I could think of now is if the object passed into a method that handled every layout there was, without being lazy. Ive tried some more test and it seems that Ive gotten down using a new for classes, even when I'm not making a new object. The only thing I need to figure out now is how they did it. How they were able to get access to another class from a class instantiated from a class. Could figure it out if I could figure out how to compare to of the same classes.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,829
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: JPanel new FlowLayout

    using a new for classes, even when I'm not making a new object.
    You use new to create an instance of a class. What you just said does not make sense.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel new FlowLayout

    I'm sorry if I didn't make sense. What I meant is I am use to doing stuff like

    MyClass newClass = new MyClass();

    not doing a new inside a parenthesis like this

    newClass.getSomething(new Whatever());

    I just can't figure out what to do with the (new Whatever()), or how to write a method in MyClass, that knows how to take another class(Whatever()) and actually tell
    1: What class it is.
    2:What to do with the class now that is knows what it is.

    I guess Ill just copy and paste some of my code.

    public class Test
    {


    public static void main(String[] args)
    {
    TestSubject subjective = new TestSubject();
    subjective.setTestSubject(new Subject());
    }

    }
    public class TestSubject
    {
    public void setTestSubject(Object newClass)
    {
    System.out.println(newClass.toString());
    Object newSubject;
    newSubject = new Subject();
    System.out.println(newSubject.toString());
    if (newClass.equals( new Subject()));
    {
    System.out.print("didnt work");

    }
    }
    }

    Have another class called subject that's just for test. It actually contains no code.
    Last edited by johnvasgird; April 26th, 2012 at 04:21 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,829
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: JPanel new FlowLayout

    The first way assigns a reference to a variable that can be used elsewhere. The second way does not. They both create an instance of the class.
    newClass.getSomething(new Whatever()); // Added needed ()s
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel new FlowLayout

    Wow okay, I figured out I was doing everything right. I used a conditional statement to compare the classes, and if the classes were the same, it would print something out. I used print() instead of println() so I couldn't tell that is was actually working. Feel like an idiot now. Thanks for the help though.

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. img flowlayout
    By Fermen in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2011, 09:39 PM
  3. Help using Flowlayout
    By PaulPeter in forum AWT / Java Swing
    Replies: 0
    Last Post: April 5th, 2010, 03:30 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 09:02 AM