• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

GridbagConstraints and Insets errors in GUI

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there, I wonder if you can help me with this please. I have to build the attached GUI as exercise (exercise.jpg), but I am getting a lot of errors on the console.
OK so the code is available on pastebin here:
http://pastebin.com/AFt4974J
http://pastebin.com/0qXG8xAL
An excerpt of errors is attached. I don't understand why I get them. Basically the compiler is picking on and
Any help is obviously more than welcome.
thanks
exercise.jpg
[Thumbnail for exercise.jpg]
error_output.jpg
[Thumbnail for error_output.jpg]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please PostTextNotScreenshots. I can't read a single word in that shot of your errors.
 
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please post the code; people prefer to read it than opening outside links.

I shall move your discussion because we usually discuss GUI layouts on a different forum.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As others have already mentioned, it would be better if you posted the code here in code tags (as you did with your prior posts), instead of linking them from elsewhere.

Anyway, based on the screenshot of compiler errors, I hazard to guess that the GridBagCostraints and Insets classes are not properly imported into your source file. Review your import statements to make sure you import these two classes.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with @Martin Vajsar in that you need to import the two classes: GridBagConstraints and Insets.

I can add that in your calls to the GridConstraints constructor that they need to have two more ints on the end.

I have read that the GridBagLayout and the GridBagConstraints together are powerful, but it seems to me that the NetBeans GUI editor has some advantages that I would like to try out.

Just today I found a bug in my code for one window that was using the altogether too complicated GridBagLayout. I replaced the code with the usage of a BorderLayout and a GridLayout which worked better in my case. Your case is different though, but it just makes me wonder what the GridBagLayout is good for.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry chaps, I thought that it would have been clearer if I linked to pastebin, apologies.
As far as I know the constraints have been imported correctly.
Strange you couldn't read the text in the screenshot, if you click on it the text is realy really clear.

Here is the code:

and to test the class:

And here is the transcription of the errors (please note that in copying and pasting the caret has moved so don't rely on that to spot the error):




 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Instead of only doing a static import as follows:

import static java.awt.GridBagConstraints.*;

You'll need to do a full import as follows:

import java.awt.GridBagConstraints;

2. You'll also need to import the Insets class as follows:

import java.awt.Insets;

3. Also, for each GridBagConstraint, when you call the constructor, you'll need to pass in two more parameters as follows:

selectionPanel.add( textArea1, new GridBagConstraints( 0, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ), 0, 0 ) );//set constraints on the element and add to jpanel

I believe that by making the above changes, you'll be able to compile.

4. Play around with the numbers to get things positioned on the screen.

I think that these 4 steps will get your project working.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK got it to work, sorry I have missed those. The problem now is though, that the layout is not as it should be, see screenshot.
Here is the code:
wrong_layout.jpg
[Thumbnail for wrong_layout.jpg]
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the layout is not as it should be, see screenshot



Can you use a GUI editor or do you have to use layout managers?
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Find out about Cay Horstmann's GBC class.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Find out about Cay Horstmann's GBC class



OK. I didn't know that, but doesn't this GBC class having a Gnu Public License mean that if you use it in your code, then your code is also open source code under the Gnu Public License?
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I never knew that.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry chaps, an IDE is not an option I need to learn to use layout managers
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, @Campbell, I figured that you would know about the Gnu Public License since you're a sherriff and have been a member for so long and since you have answered so many questions, but I just wanted @Jason to know that "open-source" doesn't mean "public domain". Maybe he didn't know that.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

an IDE is not an option I need to learn to use layout managers



As @Campbell has suggested, you could use Cay Hortsman's class called GBC. You can find it if you google for "cay hortsman's GBC class". It's not an IDE, it's a subclass of GridBagContraint. It looks like a way to better understand the GridBagConstraint.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaydell Leavitt wrote:Sorry, @Campbell, I figured that you would know about the Gnu Public License since you're a sherriff and have been a member for so long and since you have answered so many questions, but I just wanted @Jason to know that "open-source" doesn't mean "public domain". Maybe he didn't know that.


You were right to point it out, even to a sheriff. We do care about intellectual property here on the Ranch, and it is always useful to remind beginners everyone that not everything on the web is automatically at everyone's disposal.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason, you're using several different JPanels, but seem to use the coordinate system across all of them. For example, you set the coordinates of buttons to (6,0), (6,2), (6,4) and (6,6), but the buttonsPanel doesn't contain anything else. It isn't an error, but it is confusing. I'd suggest moving creation of each panel into a separate method - it will be then clearly visible that every method/panel should use its own coordinate system.

Also note that if you don't put any component into some cells, these cells won't take up any space at all. So, even when you've left "empty" columns between your buttons, the buttons aren't separated. If you want to ensure spacing between components, I'd suggest using non-zero Insets for that.

My suggestion would be to put only a few components onto your form, so you can more easily see what's happening when you set different properties of the constraints. When you get part of your layout right, add a few more components. Trying to quell too many components on the form at once is quite challenging.

You also seem not to set any weight to any component at all. Make sure you assign weights to components that should be resizable - otherwise they'll all just lump together in the middle.
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote: . . . You were right to point it out, . . .

And we don't know everything. None of us.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for shedding some light on it guys. @Martin Vajsar, thanks I think you're right, I will start with less components, that should be easier to see. However, buttonsPanel is not empty at all:


thanks
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:However, buttonsPanel is not empty at all ...


I haven't stated it is empty. I said it contains only the buttons, and therefore it doesn't make sense to start placing them at column six. Consider:

This little program creates two windows which look exactly the same - the button fills he entire contents of the form, even though the buttons are put to different cells in the GridBagLayout. The reason is that empty rows and columns do not take any space whatsoever, so there isn't any place to the left or top of the button placed at the location (6,6).

You've placed the buttons apart in the GridBagLayout, but this doesn't mean that there will be any space between them. To reserve some space around a component, use non-zero insets in the GridBagConstraint of that component.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic