Question

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);


 }
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

This is an older post but i just stumbled across this, while searching the same issue. The use of frame.pack() is used to move everything around to fit in to the given panel size. So if you increase the size of one thing, other things shrink and move around to fit in the panel.

source: http://www.macs.hw.ac.uk/guidebook/?name=JFrame&page=2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top