Search code examples
javaswinggrid-layoutjlist

insert JList into GridLayout


I have a problem related to JList. I'm getting the following layout.

JTextField | Button 1 | Button 2
Button 3   | Button 4 | Button 5

This is the layout I'm getting:

JTextField | Button 1 | Button 2
Button 3   | Button 4 | Button 5

As you can see, I do not have JList in between. I did try inserting a list component there but when I did it, the GridLayout would push the list beside button 2. I only want the list to be my itself in between those.

package ManyThings;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
javax.swing.JTextField;

public class test {

public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 3));

    JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,1));
JTextField t1;
t1=new JTextField();
p1.add(new JTextField());             
p1.add(new JButton("Search"));
p1.add(new JButton("Clear"));
frame.add(p1);

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,3));
p2.add(new JButton("Add"));
p2.add(new JButton("Detail"));
p2.add(new JButton("Remove"));
frame.add(p2);

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

Expecting the following layout.

JTextField | Button 1 | Button 2
JList
Button 3   | Button 4 | Button 5

Solution

  • Compound layouts

    Make use of multiple containers, using different layout managers (as required)

    Compound layouts

    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    public class Test {
    
        public static void main(String args[]) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridLayout(3, 1));
    
                JPanel topRow = new JPanel(new GridLayout(1, 3));
                topRow.add(new JTextField(10));
                topRow.add(new JButton("Search"));
                topRow.add(new JButton("Clear"));
    
                add(topRow);
                add(new JScrollPane(new JList()));
    
                JPanel bottomRow = new JPanel(new GridLayout(1, 3));
                bottomRow.add(new JButton("Add"));
                bottomRow.add(new JButton("Detail"));
                bottomRow.add(new JButton("Remove"));
    
                add(bottomRow);
            }
    
        }
    }
    

    Different layout managers

    Use different layout managers to better meet the requirements of the individual components

    Different layouts

    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    public class Test {
    
        public static void main(String args[]) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
    
                JPanel topRow = new JPanel(new GridLayout(1, 3));
                topRow.add(new JTextField(10));
                topRow.add(new JButton("Search"));
                topRow.add(new JButton("Clear"));
    
                JPanel bottomRow = new JPanel(new GridLayout(1, 3));
                bottomRow.add(new JButton("Add"));
                bottomRow.add(new JButton("Detail"));
                bottomRow.add(new JButton("Remove"));
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.gridx = 0;
                gbc.gridy = 0;
    
                add(topRow, gbc);
                gbc.gridy = 2;
                add(bottomRow, gbc);
    
                gbc.gridy = 1;
                gbc.weighty = 1;
                gbc.fill = GridBagConstraints.BOTH;
                add(new JScrollPane(new JList()), gbc);
    
            }
    
        }
    }
    

    See Laying Out Components Within a Container for more details