Position of JComponents using Netbeans GUI Swing Builder - java

I'm developing a graphic interface using Java and Swing, and I'm having a hard time getting the JButtons to stay in their position while changing from one panel to another.
There are three buttons in a row aligned in the left bottom of each panel, all the panels the same size, but somehow they manage to change their position a little when I run the application (on the design preview they show up in the right place). It's getting a bit annoying. I'd appreciate any help

Are you trying to do tabs? If you are, a JTabbedPane will do this much better than a button.
Since you are using the Netbeans GUI Builder, look at the options in the Component panel on the left. It has Swing tabbed panes and AWT panes if you really want.

Related

Java button within graphics panel

I'm relatively new to developing GUI's within java so this may well be a stupid question or quite simply not possible to achieve but here we go.
I've created 1 single JPanel with no border layout set up or anything like that and I intended to paint a GUI on top of it using the graphics class. The JPanel is just plain black and then I've drawn a huge box over it leaving the black just as a border, and painted the whole GUI within this white box.
I want to add buttons within that white box GUI as well but I've no idea how. In fact they don't even have to be traditional buttons JButtons, if I could just draw a shape and have that act as a button then add an event handler to just that shape that would work also but I don't know how I'd do that either.
I have so much code for my whole program (it's a school coursework project) that I'm not sure which parts would even be worth sharing to assist with this question since there's so many GUI aspects I've already drawn so I've tried to just explain my issue in words.
Honestly I have no clue what I'm doing so any help would be appreciated.
EDIT: Here's a screenshot of my current GUI with a 'sketch' of how and where I'd like to be able to add buttons.
GUI Image
As with any suitably complex UI, you need to start by breaking it down into manageable chunks, focusing on areas of mutual interaction and functionality.
For example...
Says to me that you have two primary UI elements, the left and the right.
This could easily be established with a GridLayout, but, if the two sides are not equal in width, a GridBagLayout might be more appropriate
The right side to me says simply, JTable. You could place this within a container using a BorderLayout, allowing the table to occupy the CENTER position.
The key information would then a component laid out with either a GridLayout (top and bottom) or a GridBagLayout if the requirements are more complex. This component would then be added to the SOUTH position of the BorderLayout.
Again, this is pretty simple. The primary layout would probably be a BoderLayout, with the title in the NORTH position, the graph in the CENTER and the buttons wrapped in a component in the SOUTH.
You could use either a FlowLayout or GridBagLayout to layout the buttons depending on your how you want them to appear
Recommendations
Have a look at:
Laying Out Components Within a Container
How to Use Tables
And for the "border", I'd recommend you have a look at LineBorder. Take a look at How to use Borders more details

Why does the panel added inside Frame gets to a default position whenever maximized or minimized?

I am learning AWT. I have add a panel inside frame. I have set bounds to the panel such that it stays at the bottom. But when I maximize or minimize, the panel is reset to the center top. Is this a normal behavior? Is there any events for minimize or maximize?
I am learning AWT.
Why? It was replaced by Swing a decade ago. Some people would even argue the Swing has been replaced by JavaFX.
In any case you were given a link in you last question (that you have now deleted) to the Swing tutorial. Why have you not read the tutorial?
The basic concepts of Swing and AWT are the same, since Swing is built on top of AWT, so by reading the Swing tutorial you will learn many of the AWT concepts. In most cases the difference it the component class name.
the panel is reset to the center top. Is this a normal behavior?
Yes, Swing (and AWT) work with layout managers. The layout manager is responsible for determining the size and location of a component. Don't try to manually set the size or location of a component.
Is there any events for minimize or maximize?
Yes, you can use a WindowListener.
Read the Swing tutorial. You will find sections on:
Using Layout Managers
How to Write a WindowListener

JLabel keeps its position when jframe maximized

I have jLabel with an icon wanna put in right top corner , in Netbeans design view I place it to a desired position , After running program it is stay there fine ! But when I maximize the window it doesn't horizontally move to the corner and keep its position. It becomes like in the middle.
Thanks
Don't use a design tool to create the GUI. Its sounds like Netbeans is using a absolute layout which is not what your want.
Use a proper layout manager. Maybe a FlowLayout that is right aligned.
Read the section from the Swing tutorial on How to Use Flow Layout for more information and working examples.

How to make this GUI layout in Swing

For a project I have an almost working code, but I do not have the GUI. I want to make a screen that consists of clickable labels and has the following design:
I was thinking about first making the middle GridBagLayout with with a dimension of 6 by 2. Then 'wrapping' that up and adding the two buttons to the side, and then 'wrapping' that and adding the two buttons below.
I am inexperienced with Swing, and I have no idea how to start. I hope someone can give me some hints in the right direction.
Several approaches to a very similar layout using GridBagLayout and/or nesting are shown here. Consider using JButton for each clickable area, rather than JLabel. If you go with a nested layout,
Use BorderLayout for the enclosing panel.
Add buttons to EAST and WEST for the leftmost and rightmost areas.
Add a GridLayout(1, 2) of buttons to SOUTH for the bottom row.
Add a GridLayout(2, 6) of buttons to CENTER for the central twelve areas.
Addendum: A critical issue will be what you want the resize behavior to be.
as said in the comments above you could (should ?) use the WYSIWYG Window Builder plugin available for Eclipse; it's simple to use.
However, that doesn't answer your question, so to do so, here is how I would structure the UI if I were to make one like that :
http://www.hostingpics.net/viewer.php?id=902716gZHkK26.jpg
I basically use BoxLayout because that's the one I'm most familiar with. Every Rectangle is a JPanel. I think the image is pretty self eplanatory.

Netbeans IDE 6.91 "center horizontally" not clickable

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.
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!
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

Categories

Resources