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 8 of 8

Thread: Not really Understanding JPanel..

  1. #1
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Not really Understanding JPanel..

    I don't get it... Why do we have to use JPanel??? Why cannot we just set the location of each button or whatever and be done with it?? It's making me so frustrated right now. Like how can I skip a panel from re-arranging my stuff urghh, why so complicated?


    And how did you learn how to master arranging buttons. Links, anything. Thanks.


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Not really Understanding JPanel..

    Using JPanel to make it easy to arrage every component which we want to put into the frame. Once you used the JPanel, you need to set a layout for it to make it easy for you to arrange all your components. Different layout have different ways of arrangement. For easy understanding, you just need to think that the JPanel is just an empty box and you need to decide how you going to place your stuff into the box. If you really feel that using JPanel bring you a lots of problem, you may use Netbean which provide drag and drop feature for you. Wish that the words I said might help you...

  3. The Following User Says Thank You to lunchong For This Useful Post:

    Emperor_Xyn (December 31st, 2011)

  4. #3
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Not really Understanding JPanel..

    Yeah I guess. I i'll get by and learn it. Just found out how to use GridLayout, so I think I know how to use all the other ones now.. Just a pain in the butt =p

    How do I set a text area a int? It only accepts string, do I have to parse int?? Does that make a int usuable as a string representing the number?

  5. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    270
    My Mood
    Bored
    Thanks
    39
    Thanked 75 Times in 63 Posts
    Blog Entries
    4

    Default Re: Not really Understanding JPanel..

    Yeah it's bit of a pain huh. Personally, I hate working on Java GUI's. Much rather work with VB.NET for graphical interfaces. Think of JPanel as a top level container, something that holds other things. JFrame is just the actual Window, title bar, etc.

    Read up on it these at the Using Swing Components tutorial. In order to get the buttons placing in the right place you need to get familiar with Layout Managers. The place to do this is at: A visual guide to layout managers.

    Personally, my favorite is the GroupLayout. It's one of the more complicated layout managers but it is the most flexible. I believe the netbeans GUI builder creates a GroupLayout. Speaking of which, it may be worth while checking out netbeans GUI builder. Many programmers look down there noses at it (for good reason) but it works and tweaking with the code it outputs taught me a lot about GroupLayouts.

    As for why you can't just add buttons to a JFrame and tell it the X/Y position? Don't know. Could be that doing it that way is a restrictive design pattern, preventing more advanced graphical interfaces like layered panes and child windows.

  6. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Emperor_Xyn (December 31st, 2011)

  7. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    270
    My Mood
    Bored
    Thanks
    39
    Thanked 75 Times in 63 Posts
    Blog Entries
    4

    Default Re: Not really Understanding JPanel..

    Quote Originally Posted by Emperor_Xyn View Post
    How do I set a text area a int? It only accepts string, do I have to parse int?? Does that make a int usuable as a string representing the number?
    StringBuffer b = new StringBuffer();
    b.append(myInteger);
    JTextArea myTextArea = new JTextArea(b.toString());

    There are of course many, MANY ways you could this. I like stringbuffers. You will notice that the integer is not modified/destroyed in the process. You can continue to use it later on but it will not be updated in the text area until you tell it to.

  8. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Emperor_Xyn (December 31st, 2011)

  9. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Not really Understanding JPanel..

    @Christopher

    StringBuffer is not as readable (to me) as
    String str = myInteger+""; //The compiler will convert this to new StringBuilder().append(myInteger).append("").toString(), so unless your doing heavy string manipulation your fine.
    //I haven't tried this in a while, but I can't see why they would change it
    JTextArea area = new JTextArea(str);
    and also isn't actually as efficient as you might think, because, as in the example, it converts the string addition into StringBuilder.append. So this:
    String str = "Example "+"of what "+"I believe the compiler does";
    is the same as
    String str = new StringBuilder().append("Example").append("of what ").append("I believe the compiler does").toString();

    I found this out when I used a decompiler on one of my .class's because I lost my source file somehow.

    I find "String" + "String" much more readable, but I suppose it comes down to personal preferrence. It's just that I have noticed a few people didn't realize the
    compiler did this. (I didn't for a while), and a myth (Or maybe it didn't do this in older versions?) came about that everything had to be StringBuilder to be efficient.

    Links (Saying something like this requires these ):
    http://java.sun.com/docs/books/jls/t...html#15.18.1.2, which has an interesting explanation on how it evaluates string concat, and is also linked here
    Really small type, but otherwise good explanation:
    http://willcode4beer.blogspot.com/20...revisited.html
    Last edited by Tjstretch; January 1st, 2012 at 12:32 AM.

  10. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Not really Understanding JPanel..

    Quote Originally Posted by ChristopherLowe View Post
    As for why you can't just add buttons to a JFrame and tell it the X/Y position? Don't know. Could be that doing it that way is a restrictive design pattern, preventing more advanced graphical interfaces like layered panes and child windows.
    You can: Doing wihtout a layout manager

    However, I wouldn't recommend it because it's a lower-level control and usually means you're going to end up doing more work for something that's not quite as nice as using a layout manager.

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    ChristopherLowe (January 1st, 2012)

  12. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    270
    My Mood
    Bored
    Thanks
    39
    Thanked 75 Times in 63 Posts
    Blog Entries
    4

    Default Re: Not really Understanding JPanel..

    @HelloWorld922 - Oh cool! So there you have it, it can be done. I did actually try this out before posting but didn't realize you needed to setBounds() for it to work. Good spot.

    @Tjstretch - Java strings are immutable, so like you said, the compiler needs to do some extra work behind the scenes to get String concatenation with '+' working. Normally, this extra workload is not really an issue but failing to recognize what this shortcut does to your complied code will bite you on the ass eventually.

    For example, what if this integer was your health for a the HUD of a game. Using '+' you will be creating a tonne of extra objects at run-time, in a loop that instantly gets garbage collected. Why not just use the one object (StringBuffer or StringBuilder) that won't get GC'ed until it falls out of scope, will only get instantiated once and can be reused however you like. The biggest reason I like StringBuffer as opposed to StringBuilder is because you can tell it when to increases the buffer, (so if I know what kind of data I am dealing with I can fine tune it even more efficiently).

    That is not to say I never use '+'. I do, just not often, the only time I do is for things like error messages or System.out.println() debugs; places where it is convenient to pack the code into one line and is not used for any computations or loops.

    I completely agree that the '+' is more readable than StringBuffer but for the reasons we have both stated, it is not as good a solution. This being said, I did say there are plenty of ways to achieve this. Here is another one which may be easier to read.

    String str = new Integer(myInteger).toString();
    JTextArea area = new JTextArea(str);

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, 02:35 PM
  2. Need Help Understanding Where I went wrong
    By basedoverlord12 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 1st, 2011, 05:40 PM
  3. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 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, 01: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, 08:02 AM