Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2008, 07:13 PM
Member
 
Join Date: Dec 2007
Posts: 14
hemanthjava is on a distinguished road
Java Swing GridBagLayout Problem
Hello All,

I have a problem with the GridBagLayout when it comes to laying out the components. I tried the gridbaglayout and ended up with a lot of spaces in between components. What I actually was looking for is as shown in the second pic.

Could you please help me. I have included the code also. I tried changing the ipadx, ipady values but it did not help. I am not sure how weightx, weighty work. I never really understand them

Code:
import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.JTextField; public class GridBagLayoutDemo { private static JLabel jlbName; private static JLabel jlbAge; private static JLabel jlbSex; private static JLabel jlbAddress; private static JLabel jlbCity; private static JLabel jlbState; private static JTextField jtfName; private static JTextField jtfAge; private static JTextArea jtaAddress; private static JTextField jtfState; private static JTextField jtfCity; private static JRadioButton jrbMale; private static JRadioButton jrbFemale; private static Container pane = null; private static JButton jbtSubmit; public static void addComponentsToPane(JFrame frame) { pane = frame.getContentPane(); ButtonGroup group = new ButtonGroup(); group.add(jrbMale); group.add(jrbFemale); GridBagConstraints gBC = new GridBagConstraints(); gBC.fill = GridBagConstraints.HORIZONTAL; gBC.insets = new Insets(5, 5, 0, 5); jlbName = new JLabel("Name : "); // gBC.weightx = 0.5; gBC.gridx = 0; gBC.gridy = 0; pane.add(jlbName, gBC); jtfName = new JTextField(20); gBC.gridx = 1; gBC.gridy = 0; pane.add(jtfName, gBC); jlbAge = new JLabel("Age : "); gBC.gridx = 0; gBC.gridy = 1; pane.add(jlbAge, gBC); jtfAge = new JTextField(5); gBC.gridx = 1; gBC.gridy = 1; gBC.fill = GridBagConstraints.NONE; pane.add(jtfAge, gBC); gBC.fill = GridBagConstraints.HORIZONTAL; jlbSex = new JLabel("Sex : "); gBC.gridx = 0; gBC.gridy = 2; pane.add(jlbSex, gBC); jrbMale = new JRadioButton("Male"); gBC.gridx = 1; gBC.gridy = 2; pane.add(jrbMale, gBC); jrbFemale = new JRadioButton("Female"); gBC.gridx = 2; gBC.gridy = 2; pane.add(jrbFemale, gBC); jlbAddress = new JLabel("Address : "); gBC.gridx = 0; gBC.gridy = 4; pane.add(jlbAddress, gBC); jtaAddress = new JTextArea(5, 10); gBC.gridx = 1; gBC.gridy = 4; pane.add(jtaAddress, gBC); jlbCity = new JLabel("City : "); gBC.gridx = 0; gBC.gridy = 5; pane.add(jlbCity, gBC); jtfCity = new JTextField(10); gBC.gridx = 1; gBC.gridy = 5; pane.add(jtfCity, gBC); jlbState = new JLabel("State : "); gBC.gridx = 2; gBC.gridy = 5; pane.add(jlbState, gBC); jtfState = new JTextField(10); gBC.gridx = 3; gBC.gridy = 5; pane.add(jtfState, gBC); jbtSubmit = new JButton("Submit"); gBC.gridx = 3; gBC.gridy = 6; pane.add(jbtSubmit, gBC); } private static void createAndShowGUI() { JFrame frame = new JFrame("GridBagLayout Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridBagLayout()); // Set up the content pane. addComponentsToPane(frame); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Regards,
Hemanth
Attached Images
File Type: jpg gridbagLayout.jpg (57.5 KB, 0 views)
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-29-2008, 09:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
You don't need to change weight or ipad constraints for this layout.
Works okay now.
Code:
import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.JTextField; public class GblRx { private JTextField jtfName; private JTextField jtfAge; private JTextArea jtaAddress; private JTextField jtfState; private JTextField jtfCity; private JRadioButton jrbMale; private JRadioButton jrbFemale; private JButton jbtSubmit; public void addComponentsToPane(JFrame frame) { Container pane = frame.getContentPane(); ButtonGroup group = new ButtonGroup(); group.add(jrbMale); group.add(jrbFemale); GridBagConstraints gBC = new GridBagConstraints(); gBC.fill = GridBagConstraints.HORIZONTAL; gBC.insets = new Insets(5, 5, 0, 5); JLabel jlbName = new JLabel("Name : "); gBC.gridx = 0; gBC.gridy = 0; pane.add(jlbName, gBC); jtfName = new JTextField(20); gBC.gridx = 1; gBC.gridy = 0; gBC.gridwidth = 2; pane.add(jtfName, gBC); JLabel jlbAge = new JLabel("Age : "); gBC.gridx = 0; gBC.gridy = 1; gBC.gridwidth = 1; pane.add(jlbAge, gBC); jtfAge = new JTextField(5); gBC.gridx = 1; gBC.gridy = 1; gBC.fill = GridBagConstraints.NONE; gBC.anchor = GridBagConstraints.WEST; gBC.gridwidth = 2; pane.add(jtfAge, gBC); gBC.fill = GridBagConstraints.HORIZONTAL; JLabel jlbSex = new JLabel("Sex : "); gBC.gridx = 0; gBC.gridy = 2; gBC.gridwidth = 1; pane.add(jlbSex, gBC); jrbMale = new JRadioButton("Male"); gBC.gridx = 1; gBC.gridy = 2; pane.add(jrbMale, gBC); jrbFemale = new JRadioButton("Female"); gBC.gridx = 2; gBC.gridy = 2; pane.add(jrbFemale, gBC); JLabel jlbAddress = new JLabel("Address : "); gBC.gridx = 0; gBC.gridy = 3;//4; pane.add(jlbAddress, gBC); jtaAddress = new JTextArea(5, 10); gBC.gridx = 1; gBC.gridy = 3;//4; gBC.gridwidth = 2; pane.add(jtaAddress, gBC); JLabel jlbCity = new JLabel("City : "); gBC.gridx = 0; gBC.gridy = 4;//5; gBC.gridwidth = 1; pane.add(jlbCity, gBC); jtfCity = new JTextField(10); gBC.gridx = 1; gBC.gridy = 4;//5; gBC.gridwidth = 2; pane.add(jtfCity, gBC); gBC.gridwidth = 1; JLabel jlbState = new JLabel("State : "); gBC.gridx = 3;//2; gBC.gridy = 4;//5; gBC.insets.right = 0; pane.add(jlbState, gBC); gBC.insets.right = 5; jtfState = new JTextField(10); gBC.gridx = 4;//3; gBC.gridy = 4;//5; pane.add(jtfState, gBC); jbtSubmit = new JButton("Submit"); gBC.gridx = 4;//3; gBC.gridy = 5;//6; pane.add(jbtSubmit, gBC); } public static void main(String[] args) { GblRx test = new GblRx(); JFrame frame = new JFrame("GridBagLayout Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridBagLayout()); test.addComponentsToPane(frame); frame.pack(); frame.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SWT & Swing Bridge problem roshithmca AWT / Swing 0 03-26-2008 02:23 PM
GridBagLayout newtojava7 New To Java 2 03-07-2008 01:16 AM
gridbaglayout newtojava7 New To Java 4 01-27-2008 09:03 PM
jdbc/ swing code problem sami Advanced Java 1 08-13-2007 03:26 PM
Problem with GridBagLayout Daniel SWT / JFace 2 07-01-2007 07:57 PM


All times are GMT +3. The time now is 08:37 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org