Java grid layout and JPanel

17,196

Solution 1

That's what layout managers do; they allocate enough space for every component. You could try embedding HTML into the JLabel instance in order for it to wrap. Otherwise, you're just going to have to live with it.

It's also important to note that a JPanel instance is opaque by default.

Solution 2

Well... first things first. You're declaring a GridLayout of 2 rows, 0 columns. Is this exactly what you want? I am thinking not.

The next part is where you're adding content. The grid is typically held at a dynamic size. It will shrink the cells to the smallest possible value they can. But when some contents are getting bigger (large jPanels of text) it will expand the cell.

Depending on the contents of the cell they'll either remain static or attempt to fill in all available space.

If I'm reading your question properly, what you'll want to do is set a static size for your JButtons by using the functions for JComponents See JavaDoc link here. You'll probably want to use setMaximumSize(new Dimension(width,height)); for each button.

Makes sense?

On a side note. If you want more control over your layout look towards using a GrabBagLayout. More work, but more control.

Share:
17,196
arled
Author by

arled

A React Native developer.

Updated on June 04, 2022

Comments

  • arled
    arled almost 2 years

    I have a question.

    I have the following program and I'm having trouble with one thing. I have created a grid layout, and I'm using JPanel to enter buttons and text with in the grid, but everything seems to change size if my text is very long. Can someone help me with how I can prevent this from happening?

    This is my code:

    package JFrameTester;
    
    import java.awt.*;
    import javax.swing.*;
    
    
    
    public class JFrameTester {
    
    
    public JPanel createContentPane (){
    
            JPanel panel = new JPanel();
            JButton button1,button2,button3,button4;
    
            JPanel mainPanel = new JPanel(new GridLayout(2, 0, 40, 10));
    
            //JPanel red = createSquareJPanel(Color.red, 50);
            button1 =  new JButton ("button1");
            button2 =  new JButton ("button2");
            button3 =  new JButton ("button3");
            button4 =  new JButton ("button4");
            Label one   = new Label("Rohihtjthjhtjghjghmgfjgjghjghj");
            //add(button1);
    
    
            mainPanel.add(button1);
            mainPanel.add(one);
            mainPanel.add(button2);
            mainPanel.add(button3);
            mainPanel.add(button4);
    
    
            panel.add(mainPanel);
            panel.setOpaque(true);
            return panel;
        }
    
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame("GridLayout");
            JFrameTester Display = new JFrameTester();
            frame.setContentPane(Display.createContentPane());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
    
    
     }
    }
    
  • mre
    mre over 12 years
    If @Led K sets the maximum size for the JLabel instance (which is the component giving him grief), some of it's text will be missing.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    Shouldn't that be GridBagLayout?