.
.
.
.

 button1.setIcon(pic1);
         button1.setToolTipText("Open File");
         toolBar.add(button1);   
      
         button2.setIcon(pic2);
         button2.setToolTipText("Save File");
         toolBar.add(button2);   
       
         button3.setIcon(pic3);
         button3.setToolTipText("Help File");
         toolBar.add(button3);   
         
         gbc1 = new GridBagConstraints(); 
                 // c.insets = new Insets(2, 2, 2, 2);;
         gbc1.weighty = 1.0;
         gbc1.weightx = 1.0;
         gbc1.gridx = 0;
         gbc1.gridy = 0;
         // gbc.weightx = 30.0;
         // gbc.weighty = 30.0;
         // c.gridheight = 0;
      
         gbc1.anchor = GridBagConstraints.NORTHWEST ;    
         // gbl.setConstraints(toolBar, c);
         
         contentPane.add(toolBar,gbc1);
.
.
.
.
 /* @@@@@@@@@@@@@@@@ creating panels @@@@@@@@@@@@@@@@ */
         JLabel locationLabel;
         JLabel divingTypeLabel;
         JLabel diveLogLabel;
         JLabel EquipmentLabel;
         JLabel fBuddyLabel;
         JLabel sBuddyLabel;
         
         JLabel firstNameLabel;
         JPanel mainPane = new JPanel();
         JPanel diverPane = new JPanel();   
         JTabbedPane tabs = new JTabbedPane();
         mainPane.setBorder(new LineBorder(Color.black, 5));
         Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
         Border borderTitle = BorderFactory.createTitledBorder(loweredetched, "Detail");  
         
      	    
         locationLabel=new JLabel("Location: ");
         JTextField l = new JTextField(15);  
         JTextField a = new JTextField(15);  
         l.setBorder(BorderFactory.createEtchedBorder());
         // c.fill = GridBagConstraints.BOTH;
         gbc2 = new GridBagConstraints();
         gbc2.gridx = 0;
         gbc2.gridy = 0;
         mainPane.add(locationLabel,gbc2);  
         gbc2.gridx = 1;
         gbc2.gridy = 0;
         mainPane.add(l, gbc2);
         
         divingTypeLabel=new JLabel("Diving Type: ");
         a.setBorder(BorderFactory.createEtchedBorder());
         gbc2.gridx=0;
         gbc2.gridy=1;
         mainPane.add(divingTypeLabel, gbc2);
         gbc2.gridx=1;
         gbc2.gridy=1;
         mainPane.add(a, gbc2);
       
                              
         gbc1.gridx = 0;
         gbc1.gridy = 1;
         // c.fill = GridBagConstraints.HORIZONTAL;
         // gbc.anchor = GridBagConstraints.NORTHWEST;
         gbc1.gridwidth = 2; 
         // c.anchor = GridBagConstraints.WEST; 
         gbc1.weightx = 30.0;
         gbc1.weighty = 30.0;
         gbc1.gridwidth = GridBagConstraints.REMAINDER; 
         mainPane.setBorder(borderTitle); 
         tabs.addTab("  Main  ", mainPane);  
         tabs.addTab(" Diver ", diverPane);  
         contentPane.add(tabs,gbc1);

my component seem not in correct position, how to place my Diving Type and its JTextField underneath the "location" ? The picture that i've uploaded is the result of this code

Dani AI

Generated

As pointed out, the reason your labels and fields did not stack was that the panel kept its default layout manager (FlowLayout), so the GridBagConstraints you supplied had no effect. The fix is to give that panel a GridBagLayout (so the gridx/gridy coordinates matter) and then tune the constraints for labels vs fields.

Practical tips that follow the forum code thread but avoid repeating it:

  • Use one constraints template for form rows (set sensible insets for spacing). For labels keep them anchored to the left or right so they sit next to the field; for text fields set the fill to allow horizontal growth and give them a positive weight in the x direction so they absorb extra width.
  • Increment the row index for each new label/field pair. Either create a fresh GridBagConstraints for each add, or clone/reset the fields you change (gridx, gridy, weightx, fill, anchor) before each add — GridBagConstraints is mutable and leftover settings are a common source of odd layouts.
  • If the panel is inside a larger GridBagLayout (you already use constraints to place the toolbar and tabs), make sure the central/tabs region has nonzero vertical weight so it expands; conversely keep the toolbar weight small so it stays compact.
  • For simple two-column forms consider a simpler manager (GridLayout with two columns, or a BoxLayout with nested panels) or a small helper library (JGoodies FormLayout) if you want less boilerplate.

For reference and examples see the official Java tutorial and the GridBagConstraints API:
GridBagLayout tutorial
GridBagConstraints API

As noted, applying the layout change fixed the problem; the above suggestions help make the form behave predictably when the window is resized.

Recommended Answers

All 2 Replies

After you create the "mainPane", you need to set it's layout to GridBagLayout like so

mainPane.setLayout(new GridBagLayout());

thanks a lot, Ezzaral, you have help me out from this problem, what a careless mistake i've made

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.