views:

72

answers:

2

hello everyone,

i am decent with java programming, but very new to GUI development. i wanted to make a console blackjack game i made years ago into one where people can play via a GUI i build using netbeans. i think that without a GUI, most people won't take the game seriously when i add it to my working portfolio

i created a JFRAME with the size set to 1000X700 pixels and on top of that is a JPanel with a background of dark green to simulate a blackjack table.

on top of the the dark green JPanel I have a small 60X93 JLabel with the icon set to be the back of a playing card. this represents the dealer and i wanted to CENTER it horizontally and vertically on top of the JPanel on which it rests.

however, in the netbeans tool bar for the particular GUI the "center horizontally" and "center vertically" options are greyed out and not clickable even when i select the JLabel which is the dealer. i even tried shift clicking to select both the JLabel AND the JPanel on which it rests but still the "center horizontally" and "center vertically" options are greyed out.

can someone please help me or offer guidance please? thank you... i have spent 2 days googling and i don't understand how others have not run into this same problem.

A: 

Interesting question. I've been working with NetBeans for years, and to be honest I've never even noticed those icons before until you just pointed them out. I'm unable to make them do anything either.

However, if you're new to GUI development in Java... you should read about layout managers, and spend some time with the GridBagLayout tutorial. GridBagLayout is by far the most popular layout manager in Swing development. You could get by using it exclusively if you wanted, as there are really only a handful of real-word situations in which it makes more sense to use another layout manager.

For your immediate purposes: In your Inspector view, right-click on the JPanel and select "Set Layout -> Grid Bag Layout". You'll now see your layout manager in the Inspector tree view right below your JPanel. Right-click on it and select "Customize". A window will pop-up, and there you can click on any component you're interested in and adjust all kinds of settings (e.g. margins, padding, etc). The setting in which you are interested is "Anchor", and the value you want is "Center". With your JLabel selected, you can adjust this setting either from the pull-down menu at the top-left... or graphically at the top-bottom.

One way or the other, if you're going to do any kind of Java GUI development beyond the most trivial of "Hello World" examples... you're going to be using GridBagLayout very soon. So you might as well dive in!

Steve Perkins
hmmmm this gridbaglayout seems tricky... do i have to add all the components i want to the canvas BEFORE setting the Panel as GridBagLayout? OR can i just have the one label at first... set the GridBagLayout and then add more components and then keep tweaking the GridBag settings?
Martin
this thing seems like a nightmare to learn but with all things worthwhile i guess im gonna have to put some blood, sweat, and tears into this thing to finally get it. too bad there arent specific tutorials walking you through something as the Help section is only definitions of values... ehhh i will get this down and learn it even if it kills me.
Martin
@Martin: It's much less complicated when you use NetBeans rather than writing the code by hand. It doesn't really matter if you declare the components first and the layout manager second, or the other way around... NetBeans will simply sort it out. But if I correctly follow your first comment above, the answer is the latter. Using GridBagLayout is very similar to working with tables in HTML... as you add new elements (i.e. rows and columns), you may have to adjust the existing rows and columns a bit.
Steve Perkins
incredible... since i have a decent knowledge of HTML i know exactly what you are talking about.
Martin
thank you-a thousand times over
Martin
@Martin: You can always accept the answer if it's helpful! :)
Steve Perkins
A: 

Let me provide some explanation about the tool button options "Center Horizontally" and "Center Vertically"

All the six toolbar buttons provided on the Matisse Designer are for alignment of the components related to each other. Those buttons only get activated when you select more that one component on the designer.

When we select two or more components on the designer window, may it be JPanel or JFrame, and click the "Center Horizontally" button all the selected controls (components) are aligned one below the other such that the center points of all the components are in one vertical line. That means they are moved in the horizontal direction to make them align exactly one below the other.

The "Center Vertically" button does this alignment in the vertical direction. So to answer your question the tools you are using for centering a component in JFrame are incorrect tools. They are for aligning components relatively to each other and not the container.

If you select a JPanel and a component inside the JPanel the tool buttons get disabled. The alignment setting buttons are available only when components are selected in the same container, in other words the components who are under same container.

To arrange a component in the center of the JFrame you may have to add some custom code or use some other layout manager other that Group Layout which is used by default by the designer.

with regards
Tushar Joshi, Nagpur

Tushar Joshi
good man... very well explained. thank you
Martin
@Martin your questions was about "center horizontally" and if my explanation has answered your questions then please accept it as answer.
Tushar Joshi