2

I have a Swing Form that contains a JScrollPane(activityScrollPane) for a JPanel(activityPanel). The panel contains a JTextField and a JButton (that is used to add more fields to the Panel). Now the problem is that the elements start from the center of the panel as in the image below (with the borders marking the activityScrollPane boundary)

enter image description here

Following is the code I am currently using to make the scroll pane and associated components.

 //part of the code for creating the ScrollPane

        final JPanel activityPanel = new JPanel(new GridBagLayout());
        gbc.gridx=0;
        gbc.gridy=0;
        JScrollPane activityScrollPane = new JScrollPane(activityPanel); 
        //adding activity fields
        activityFields = new ArrayList<JTextField>();
        fieldIndex = 0;
        activityFields.add(new JTextField(30));

        final GridBagConstraints activityGBC = new GridBagConstraints();
        activityGBC.gridx=0;
        activityGBC.gridy=0;
        activityGBC.anchor = GridBagConstraints.FIRST_LINE_START;
        activityPanel.add(activityFields.get(fieldIndex),activityGBC);

        fieldIndex++;
        JButton btn_more = (new JButton("more"));
        activityGBC.gridx=1;
        activityPanel.add(btn_more,activityGBC);

How can I make the JTextField and the JButton or for that matter any component to appear on the top left corner of the JScrollPane. I have already tried using

activityConstraints.anchor = GridBagConstraints.NORTHWEST;

as pointed in the SO post, but it does not at all seem to work.

Community
  • 1
  • 1
Ankit
  • 6,772
  • 11
  • 48
  • 84

5 Answers5

3

You simply forgot to provide any weightx/weighty values, atleast one having a non-zero value will do. have a look at this code example :

import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo
{
    private JTextField tfield1;
    private JButton button1;

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        tfield1 = new JTextField(10);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;  
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;       
        contentPane.add(tfield1, gbc);

        button1 = new JButton("More");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        contentPane.add(button1, gbc);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutDemo().displayGUI();
            }
        });
    }
}

Latest EDIT : No spacing along Y-Axis

import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo
{
    private JTextField tfield1;
    private JButton button1;
    private JTextField tfield2;
    private JButton button2;

    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        tfield1 = new JTextField(10);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
        gbc.weightx = 1.0;
        //gbc.weighty = 0.2;    
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;       
        contentPane.add(tfield1, gbc);

        button1 = new JButton("More");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        contentPane.add(button1, gbc);

        tfield2 = new JTextField(10);
        gbc.weightx = 1.0;
        gbc.weighty = 0.2;  
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;       
        contentPane.add(tfield2, gbc);

        button2 = new JButton("More");
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        contentPane.add(button2, gbc);

        JScrollPane scroller = new JScrollPane(contentPane);

        frame.add(scroller);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridBagLayoutDemo().displayGUI();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • it works for the first field. But when I try to add more `JTextField`s, they get equally distributed over the entire height. Is there a way to prevent that as well. – Ankit May 22 '12 at 12:38
  • @Ankit : Too simple, just comment this line `gbc.weighty = 1.0`, though keep the `weightx` value as is :-), Please do revert back, if you want something else, that I couldn't understood :-) – nIcE cOw May 22 '12 at 12:52
  • the first part of making it start from the top is fine. But the thing is that if I use `weighty`, then it starts at top but as I add more fields they do not start just below the previous field. Instead it starts in the center. I guess I am using the wrong layout. – Ankit May 23 '12 at 01:00
  • Simply use `weightx`, don't use `weighty`, if you don't want spacing along Y-axis between the components, try this latest edit and see is this what you wanted :-) – nIcE cOw May 23 '12 at 01:52
2

try it with BorderLayout: controls.setLayout(new BorderLayout()); and then apply it for your JPanel controls.add(yourPanel, BorderLayout.PAGE_START);

I also have problems with GridBagLayout so i solved it with BorderLayout and it works so fine.


So i wrote for your little example:

private void initComponents() {

        controls = new Container();
        controls = getContentPane();
        controls.setLayout(new BorderLayout());

        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        field = new JTextField(20);
        c.gridx = 0;
        c.gridy = 0;
        panel.add(field, c);

        one = new JButton("Go!");
        c.gridx = 1;
        c.gridy = 0;
        panel.add(one, c);

        controls.add(panel, BorderLayout.PAGE_START);

    }  

Hope it helps!

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
2

Sorry as my answer me be on the off-side of what you have asked, but why dont you use GroupLayout instead of GridBag Layout, thats much more easier to handle

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
2

I think this could be simple and possible, you can

to this JPanel

  • put JPanels contains JComponent to the GridLayout (notice about scrolling, you have to change scrolling increment)

or use most complex JComponents as

  • put JPanels contains JComponent as Item to the JList

  • put JPanels contains JComponent as row to the JTable (with only one Column, with or without TableHeader)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Add one panel at the right and one at the bottom.

Right Panel: Set Weight X to 1.0. Set Fill to horizontal.

Bottom Panel: Set Weight Y to 1.0. Set Fill to vertical

There may be better ways to that, but this one worked for me.

wAy
  • 188
  • 6