Results 1 to 9 of 9
  1. #1
    grapeShot is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Not understanding GridLayout

    Hello,

    I cannot figure out how to implement GridLayout.

    In my applet, I want to make a grid of 2 rows and 2 columns. In each grid I want to add a Label and a TextField. I want the background to be red.

    so my code would be?

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;

    public class GridLayoutApplet extends Applet implements ActionListener{

    // construct components
    Label fNameLabel = new Label("First Name");
    TextField fNameField = new TextField(20);
    Label lNameLabel = new Label("Last Name");
    TextField lNameField = new TextField(20);
    Label addressLabel = new Label("Address");
    TextField addressField = new TextField(20);
    Label cityLabel = new Label("City");
    TextField cityField = new TextField(20);

    public void init(){
    setBackground(Color.red);
    setLayout(new GridLayout(2,2);

    // add components to GridLayout
    add.fNameLabel;
    add.fNameField;
    add.lNameLabel;
    add.lNameField;
    add.addressLabel;
    add.addressField;
    add.cityLabel;
    add.cityField;

    }
    }

    I have read about panels and frames but, it is all confusing to me. How can you add a label and a TextField to one square of the grid?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Not understanding GridLayout

    You should check out the Oracle tutorials in my signature. They show examples on frames, panels, and layout managers.
    Also,you have some basic syntax errors in your code above.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    grapeShot is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: Not understanding GridLayout

    Thanks but no thanks Jim :(

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Not understanding GridLayout

    Quote Originally Posted by grapeShot View Post
    Thanks but no thanks Jim :(
    So if you don't put in the effort, why would we?

    One of the basic syntax errors Jim is pointing to is this: it's add(myComponent), and not add.myComponent.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Not understanding GridLayout

    Quote Originally Posted by grapeShot View Post
    Thanks but no thanks Jim :(
    Quit now, programming is not for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    grapeShot is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: Not understanding GridLayout

    Quote Originally Posted by SurfMan View Post
    So if you don't put in the effort, why would we?

    One of the basic syntax errors Jim is pointing to is this: it's add(myComponent), and not add.myComponent.
    Thank you SurfMan. But presupposing that I put in no effort is wrong. I had consulted many textbooks and online tutorials. None of them were giving me the specific answers I was seeking. I think I am like many new to Java programming that are looking for a friendly yet helpful place to get answers and to be directed back to the same resources that have as yet failed me. Your explanation of my syntax errors is very helpful and just the sort of thing I was looking for. If I can see what is correct and compare that to mine, I can make the connection that is missing and learn which is why I assume this forum is online.

    I have since worked out my problems with the code and my understanding of what is happening with each piece of code, but that is not through the help of this forum.

  7. #7
    grapeShot is offline Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    Default Re: Not understanding GridLayout

    Quote Originally Posted by gimbal2 View Post
    Quit now, programming is not for you.
    Way to go!! What a vote of confidence for someone new to programming ;) I hope others are reading this helpful post!

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: Not understanding GridLayout

    If you want to add a label AND a textfield to an area in a GridLayout, you have to add it to another JPanel, then add that new panel to the GridLayout. A single area in a GridLayout can only contain one component.

    Generally speaking, if people are redirecting you to tutorials and Google, 9 times out of 10, your question was too vague. There are hundreds of posts here of students trying to cheat their way through CS by getting all their work handed on a silver platter here. (Here's an example: Need help with excercise). We don't hand out freebies like that. That's why we ask for effort of the OP themselves. Sorry if that was misdirected. If you stick around a bit, you'll see the cheaters come by, then you'll understand what I mean :)

    Please don't stop visiting here. If you look at our post history, you can see that we always help people that ask good, solid questions, and show effort.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Not understanding GridLayout

    I'm not apologizing, its your own fault for passing yourself off as the average loser that doesn't want to make any effort. The next time you may want to consider actually explaining why you say "no thank you" when someone points you to research material.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Making a GridLayout?
    By umzz in forum AWT / Swing
    Replies: 1
    Last Post: 03-19-2012, 11:32 PM
  2. help! Gridlayout
    By eiramae in forum Java Applets
    Replies: 3
    Last Post: 02-19-2011, 06:11 AM
  3. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  4. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 11:16 PM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 12:48 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
  •