How to remove the space between a JComponent and its Grid border while using GridBagLayout?
2 Answer(s)      5 years and 7 months ago
Posted in : Vurduk mu Şamarı Oynar Gotünün Damar

Hi, I had try to design a UI using GridBagLayout.I had a problem in that ie,the name of the Label is small(for example."Hai"),ane the gridwidth=1;when I try to add a JTextBox ater the Label,there I find huge space between the Label text and the TextBox.Could anyone help me to reduce the space between those Components?

Thanks for ur help in advance.


View Answers

August 4, 2011 at 3:12 PM

import java.awt.*;
import javax.swing.*;

public class GridBagLayoutExample {

  public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); 
    c.gridx = 0; 
    c.gridy = 0;
    c.ipadx = 5;
    c.ipady = 5; 
    p.add(new JLabel("Java"), c); 
    c.gridx = 1; 
    c.ipadx = 0; 
    c.ipady = 0;
    p.add(new JTextField(20), c);
    f.setSize(300, 200);

August 4, 2011 at 3:44 PM

What I exactly want is......1 JLabel name "Hai" another Label name is "Welcome" then the textBox next to Hai and welcome starts from same X axis position.I want the textbox after Hai to start before the welcome TB position

Related Tutorials/Questions & Answers: