This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java GridLayout - 2 columns  RSS feed

 
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm trying to create a basic GUI for booking classes in a gym. I'm not very familiar with the grid layout and struggling with creating a 2 column grid.

Here's what I'm trying to achieve:


And here's my grid code so far:



Any guidance would be much appreciated. Many thanks.
 
Sheriff
Posts: 23507
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This creates a layout with 4 rows and 1 column. Since you want 2 columns then the second parameter ought to be 2, not 1. And the first parameter ought to be however many rows you want.

I notice that your code declared 4 panels but it only puts components into 2 of them. The other 2 panels are left empty. That must be only a copy-and-paste error, or something like that. Or maybe you haven't finished with the code which adds the components, because it looks like you want a JTextField with a scroll bar and you haven't coded that yet.

Anyway, fixing the declaration of the GridLayout ought to improve things a bit. Don't hesitate to post back if you need more help.
 
Paul Clapham
Sheriff
Posts: 23507
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Marshal
Posts: 59115
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Moving to our GUIs forum.

I don't think you can achieve that layout with Grid Layout, not unless you subdivide the panel into several smaller panels. The idea of grid layout is that all the components are the same size. You may have to resort to grid bag. Find a copy of Cay Horstmann's Core Java II (volume I) and look for the GBC class, which makes using grid bag much easier. There is a siplified version of that page here. The tutorial (‍) linked to in the first link has been moved; it is now on YouTube.
I think the scrolling box on the right wants to be a location 1, 0, spanning 6 vertically, and make the five components on the left:-
  • 1: JLabel: location 0, 1
  • 2: JTextField (I think) Location 0, 2
  • 3: JLabel: location 0, 4
  • 4: JTextField (I think) Location 0, 5
  • 5: JButton. Location 0, 6
  • All have spans 1, 1.
     
    Paul Clapham
    Sheriff
    Posts: 23507
    47
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In the picture posted, the top part is a 2-column grid. In the left column are Member ID, Course name, and Confirm Booking, and in the right column is an unlabelled text component. So that could be done using a GridLayout(1, 2). Then the first JPanel in that layout would need its own layout to arrange its 5 components -- I wouldn't use a GridLayout(5, 1) for that because it would space all 5 components uniformly, and that isn't what the picture shows. Perhaps a BoxLayout with some spacing to make it come out... but we don't know whether that's in the scope of Lyle's learning so far.

    And the whole thing looks like a FlowLayout with that GridLayout on top and another JPanel below to contain the two "View" buttons. That JPanel would need its own layout to centre the two buttons like in the picture.
     
    Author
    Ranch Hand
    Posts: 964
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Paul Clapham wrote:This creates a layout with 4 rows and 1 column. Since you want 2 columns then the second parameter ought to be 2, not 1. And the first parameter ought to be however many rows you want.


    At the risk of being irrelevant to the original poster, allow me to elaborate on this a bit.

    If you look at how GridLayout is actually implemented, then you will see that new GridLayout(4, 1) will actually lay out with 4 rows and with as many columns as necessary. The cols argument is essentially ignored. (If you doubt, the javadocs for the GridLayout.setColumns() method explains this. Or just run a few quick examples and you'll see.)

    Because of this, when I use GridLayout I always specify zero either for rows or for cols, just to remind myself how GridLayout works. So I would write either new GridLayout(n, 0) or new GridLayout(0, n). In this case, in which a two-column grid seems to be desired, I would use new GridLayout(0, 2).
     
    Men call me Jim. Women look past me to this tiny ad:
    Rocket Oven Kickstarter - from the trailboss
    https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!