Results 1 to 3 of 3
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default Require Help with Layout (GridBag)

    Hello All,

    I need help with GridBagLayout. I get stuck until this stage many times and would not know how to proceed further. I need help on laying out the components according to the Layout as attached. WIth the solution given this time I will look into it more deeply and analyse where I went wrong. Please help.

    Code is Shown Below

    Java Code:
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class XCurrencyNumberFormatDemo {
    
    	static JPanel jplMain = new JPanel();
    	static JLabel jlbCurrencyIn = new JLabel();
    	static JLabel jlbLocale = new JLabel();
    	static JTextField jtfCurrencyIn = new JTextField();
    	static JTextField jtfLocale = new JTextField();
    	static JLabel jlbCurrencyOut = new JLabel();
    	static JTextField jtfCurrencyOut = new JTextField();
    	static JButton jbnCancel = new JButton("Cancel");
    	static JButton jbnConvert = new JButton("Convert");
    
    	public static void main(String args[]) {
    
    		createGUI();
    
    	}
    
    	public static void createGUI() {
    		JFrame frame = new JFrame("XNumberFormat Demo");
    
    		frame.setLayout(new GridBagLayout());
    		frame.setContentPane(jplMain);
    		jplMain.setLayout(new GridBagLayout());
    		GridBagConstraints gc = new GridBagConstraints();
    		gc.gridx = 0;
    		gc.gridy = 0;
    		gc.fill = GridBagConstraints.BOTH;
    		gc.weightx = 0.3;
    		gc.weighty = 0.3;
    		jplMain.add(jlbCurrencyIn, gc);
    
    		gc.gridx = 1;
    		gc.gridy = 0;
    		jplMain.add(jtfCurrencyIn, gc);
    
    		gc.gridx = 0;
    		gc.gridy = 1;
    		jplMain.add(jlbLocale, gc);
    
    		gc.gridx = 1;
    		gc.gridy = 1;
    		jplMain.add(jtfLocale, gc);
    
    		gc.gridx = 2;
    		gc.gridy = 1;
    		jplMain.add(jbnConvert, gc);
    		
    		gc.gridx = 0;
    		gc.gridy = 2;
    		jplMain.add(jlbCurrencyOut, gc);
    
    		gc.gridx = 1;
    		gc.gridy = 2;
    		jplMain.add(jtfCurrencyOut, gc);
    		jtfCurrencyOut.setEnabled(false);
    
    		gc.gridx = 2;
    		gc.gridy = 2;
    		jplMain.add(jbnCancel, gc);
    
    		frame.setVisible(true);
    		frame.setSize(300, 400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    }
    Regards,
    Hemanth
    Attached Thumbnails Attached Thumbnails Require Help with Layout (GridBag)-mylayout.gif  
    Last edited by hemanthjava; 10-22-2008 at 11:30 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default

    Java Code:
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class XCurrencyNumberFormatDemo {
    
       static JPanel jplMain = new JPanel();
       //static JLabel jlbCurrencyIn = new JLabel();
       static JLabel jlbCurrencyIn = new JLabel("In Currency");
       //static JLabel jlbLocale = new JLabel();
       static JLabel jlbLocale = new JLabel("In Locale");
       //static JTextField jtfCurrencyIn = new JTextField();
       static JTextField jtfCurrencyIn = new JTextField(10);
       //static JTextField jtfLocale = new JTextField();
       static JTextField jtfLocale = new JTextField(10);
       //static JLabel jlbCurrencyOut = new JLabel();
       static JLabel jlbCurrencyOut = new JLabel("Out Currency");
       //static JTextField jtfCurrencyOut = new JTextField();
       static JTextField jtfCurrencyOut = new JTextField(10);
       static JButton jbnConvert = new JButton("Convert");
       static JButton jbnCancel = new JButton("Cancel");
    
       public static void main(String args[]) {
          SwingUtilities.invokeLater(new Runnable() {
    
             public void run() {
                new XCurrencyNumberFormatDemo().createGUI();
             }
          });
       }
    
       public void createGUI() {
          jbnCancel.setPreferredSize(jbnConvert.getPreferredSize());
          
          JFrame frame = new JFrame("XNumberFormat Demo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(400, 400);
          
          //frame.setLayout(new GridBagLayout());
          jplMain.setLayout(new GridBagLayout());
          frame.setContentPane(jplMain);
          
          GridBagConstraints gbc = new GridBagConstraints();
          // Added
          Insets insets = new Insets(0, 10, 0, 10);
          gbc.insets = insets;
          //
          //gc.gridx = 0;
          //gc.gridy = 0;
          gbc.weightx = 0.3;
          gbc.weighty = 0.3;
          //gc.fill = GridBagConstraints.BOTH;
          
          jplMain.add(jlbCurrencyIn, gbc);
    
          gbc.gridx = 1;
          //gc.gridy = 0;
          jplMain.add(jtfCurrencyIn, gbc);
    
          gbc.gridx = 0;
          gbc.gridy = 1;
          jplMain.add(jlbLocale, gbc);
    
          gbc.gridx = 1;
          //gc.gridy = 1;
          jplMain.add(jtfLocale, gbc);
    
          gbc.gridx = 2;
          //gc.gridy = 1;
          jplMain.add(jbnConvert, gbc);
    
          gbc.gridx = 0;
          gbc.gridy = 2;
          jplMain.add(jlbCurrencyOut, gbc);
    
          gbc.gridx = 1;
          //gc.gridy = 2;
          jplMain.add(jtfCurrencyOut, gbc);
          jtfCurrencyOut.setEnabled(false);
    
          gbc.gridx = 2;
          //gc.gridy = 2;
          jplMain.add(jbnCancel, gbc);
    
          frame.setVisible(true);
       }
    }
    db

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My recommendation is to skip the gridbag whenever possible and do it with other nested layouts:
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class XCur2
    {
      private static final String[] LABELS = {"In Currency", "In Locale", "Out Currency"};
      private static final String CONVERT = "CONVERT";
      private static final String CANCEL = "CANCEL";
      private static final String[] BTN_STRINGS = {CONVERT, CANCEL};
      private static final Dimension MAIN_PANEL_SIZE = new Dimension(450, 160);
      private JPanel mainPanel = new JPanel();
      private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();
    
      public XCur2()
      {
        JPanel labelPanel = createLabelPanel();
        JPanel textPanel = createTextPanel();
        JPanel buttonPanel = createButtonPanel();
        mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS));
        mainPanel.add(labelPanel);
        mainPanel.add(textPanel);
        mainPanel.add(buttonPanel);
      }
      
      private JPanel createLabelPanel()
      {
        JPanel labelPanel2 = new JPanel(new GridLayout(3, 1));
        labelPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
        int leftBorder = 10;
        for (String labelString : LABELS)
        {
          JLabel label = new JLabel(labelString);
          label.setBorder(BorderFactory.createCompoundBorder(
              BorderFactory.createLineBorder(Color.black), 
              BorderFactory.createEmptyBorder(0, leftBorder, 0, 0)));
          labelPanel2.add(label);
        }
        return labelPanel2;
      }
      
      private JPanel createTextPanel()
      {
        JPanel textPanel2 = new JPanel(new GridLayout(3, 1));
        textPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
        int textareaSize = 12;
        for (String labelString : LABELS)
        {
          JTextField textfield = new JTextField(textareaSize);
          textFieldMap.put(labelString, textfield);
          JPanel tfpane = new JPanel(new GridBagLayout());
          tfpane.add(textfield, new GridBagConstraints());
          tfpane.setBorder(BorderFactory.createLineBorder(Color.black));
          textPanel2.add(tfpane);
        }
        return textPanel2;
      }
      
      private JPanel createButtonPanel()
      {
        Dimension btnSize = new Dimension(100, 28);
        JPanel bp = new JPanel(new GridLayout(3, 1));
        bp.setBorder(BorderFactory.createLineBorder(Color.black));
        JPanel emptyPanel = new JPanel();
        emptyPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        bp.add(emptyPanel);
        for (String btnString : BTN_STRINGS)
        {
          JButton btn = new JButton(btnString);
          btn.setPreferredSize(btnSize);
          JPanel btnPane = new JPanel(new GridBagLayout());
          btnPane.add(btn, new GridBagConstraints());
          btnPane.setBorder(BorderFactory.createLineBorder(Color.black));
          bp.add(btnPane);
        }
        return bp;
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("XNumber Format Demo");
        frame.getContentPane().add(new XCur2().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

Similar Threads

  1. Require some insight!
    By Shaolin in forum JavaServer Pages (JSP) and JSTL
    Replies: 13
    Last Post: 07-15-2008, 11:36 PM
  2. Gridbag Layout Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 12-04-2007, 05:12 PM
  3. Require Links for free swing component
    By Gajesh Tripathi in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2007, 10:24 PM
  4. MiG Layout Manager 2.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-16-2007, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •