• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Gridbag layout not showing

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I'll try to keep this as short and sweet as possible. I've created a window and now I want to add buttons, text fields, password field and labels to a frame but when I try to use grid bag layout for some reason it's not working. I've tried going to afew tutorials but they don't really help and I can't seem to understand where i'm going wrong. Can you guys take a look and let me know where i'm going wrong.


 
Marshal
Posts: 79962
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't combine setSize and a layout. Use the fill fields of the cnstraints instead.
Don't use grid bag constraints if you can use something like Cay Horstmann's GBC class. Have a look at the tutorial he links to
Don't add things after the setVisible call. Move the setVisible call to the last line. What is happening is that you are making the GUI visible before you add the button, so the version with the button never appears.
 
Bartender
Posts: 5560
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at least as important: don't forget to add the panel to the frame.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I've tried going to afew tutorials but they don't really help



Show us the links to the tutorials that you are using and point out the sections from the tutorial that you don't understand.

If you don't understand the tutorial, why do you think you will understand what we suggest? We can't guess what you find confusing.

So the idea of using a tutorial is to start with working code and then make changes. When something stops working you know what you just changed and then you can ask a specific question about the change you just made.

 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my redo let out as logically as I can but once again the panel with the labels and fields isn't showing up



 
Piet Souris
Bartender
Posts: 5560
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mitchell,

the problem is: the devil is in the details.

You seem to know the concepts involved: creating a frame, and a JPanel with a GridbagLayout, and setting your constraints. The problem is: how do you use all these things in the correct way?

For instance: you write (lines 42 and 43):

But that code does not add a panel to the frame at all.

Then you add many components to your panel, okay, but then these components are again added (see lines 46-49 and 57 and farther). To what are you adding these components (from line 57)?

So, create your JPanel, with GridBagLayout, add all your components to it, with the correct constraints, then create your frame and add that panel to it, with
frame.add(panel).

Getting this contraints exactly as they should be can be a bit of a hussle, but then again, experiment as much as you can, with the API at hand.

Keep to this simple scheme. Succes!
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah.... I see what I did....



This is what I have so far now, don't tell me where the mistakes are but point out what is wrong so I can find it. I'm starting to get hooked on programming and I want to know everything.




 
Piet Souris
Bartender
Posts: 5560
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using an IDE, then you'll see that the IDE reports many errors.
Why do you set a GridbagLayout for your frame?
you are (at the end of the program) adding your components like this:  component.add(panel). Does that seem right?

And, as Campbeel wrote (although it does not matter much): first make your frame ok, and only then make it visible.

Yiou see: many details that need to be correct. So, code as much as you can, but with the API's at hand, and give the Oracle tutorials a thorough read! There's no other way. But don't worry: it is quite a pleasent way!
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't that the correct way to add components to the panel?

component.add(panel);

?

Also do you set the gridbaglayout to the panel or to the frame?
 
Piet Souris
Bartender
Posts: 5560
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, have you corrected the errors and run your program? What output did you get?
Can you answer your own questions now?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pay attention to what others have told you. Make sure the frame.setVisible(true) call is made AFTER you add the panel to the frame.
Don't set a size on the button - let it be its preferred size.

And you are calling  
That is backward. you want to add the textField to the panel, not add the panel to the textField.
 
mitchell bat
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, just wanted to say I got it!

I read through it all and figured out where I was going wrong.
1. As Fred Kleinschmidt said I was adding the panel to the components and not the component to the panel
2. I set the visibility to true in the wrong part of the code
3. Gridbag layout was wrong

Here is my revised code but now it's time to play around with the gridbaglayout so I can master it!

 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic