Results 1 to 6 of 6
- 11-07-2009, 04:30 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
GridBagLayout and multiple JPanel's on a single JFrame
Hi!
I have a form (frame) with a few panels in JTabbedPane on it. Frame has a BorderLayout as tabbed pane will occupy whole form. But each panel should have GridBagLayout as it will contain labels, text fields and text areas.
Here is the current code I use to set up layouts on all panels
The question is should I create one global (for the current frame that is) GridBagLayout variable and use it with "new GridBagLayout()" operator for all panels or use the current approach?Java Code:// adds a component to a panel private void addComponent(JPanel panel, Component component, int row, int column, int width, int height, GridBagLayout gbl, GridBagConstraints gbc) { gbc.gridx = column; gbc.gridy = row; gbc.gridwidth = width; gbc.gridheight = height; gbl.setConstraints(component, gbc); panel.add(component); } private void addPanel1Components() { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); pSoft.setLayout(gbl); addComponent(Panel1, lblName, 0, 0, 1, 2, gbl, gbc); ... // It is followed by other 10+ similar calls for all of the components to be placed on this panel }
As having a procedure with 8 parameters is kind of not good looking code and probably not the optimal one either.
NOTE: this question is also posted here:
forums.sun.com/thread.jspa?threadID=5415147
NOTE: ANSWEREDLast edited by Cybex; 11-09-2009 at 11:02 AM.
-
Thanks for the link. I have replied in the Sun forum.
- 11-09-2009, 05:55 AM #3
The question is should I create one global (for the current frame that is) GridBagLayout variable and use it with "new GridBagLayout()" operator for all panels or use the current approach?
As having a procedure with 8 parameters is kind of not good looking code and probably not the optimal one either.
It' really up to you.
Some considerations:
1 — if it works and you are satisfied with it, it's okay
2 — if others will have to read/edit your code, will it be easy enough for them to read and understand? Also, how will it look and read if you come back to it in a few months?
3 — if you will want to query and alter any of these layouts in the future you will want a separate instance of GridBagLayout for each container. You can access (and cast) the layout from the container.
4 — you can use a single instance of both GridBagLayout and GridBagConstraints for everything if you like.
- 11-09-2009, 11:00 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
Thanks for advices!
I I'll stick to single layout for each panel variant. Will try to rewrite the code a bit for it so it would not be too bulky.
-
Also seriously look at MigLayout which Google can help you find. It's a powerful and easier alternative to GridBagLayout.
- 11-09-2009, 06:12 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
displaying multiple tabels information in single window
By mailsatya in forum AWT / SwingReplies: 3Last Post: 10-12-2009, 02:03 PM -
Switch JPanels in a single JFrame
By atom86 in forum AWT / SwingReplies: 8Last Post: 09-23-2009, 09:30 AM -
multiple users with single connection
By nishi.kishore in forum Java ServletReplies: 3Last Post: 09-06-2009, 03:15 PM -
[SOLVED] Multiple client single server problem(it hangs halfway)
By kellaw in forum Advanced JavaReplies: 1Last Post: 10-02-2008, 07:03 PM -
Adding Multiple Panels and Single Scroll bar on the JFrame
By SANDY_INDIA in forum AWT / SwingReplies: 6Last Post: 07-28-2008, 06:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks