views:

457

answers:

4

Hi,

The other day this code was working. I changed some things and re-ran it and now it doesn't work as intended. Obviously something I changed altered the behaviour, but I have gone back and reverted all of those changes and it still doesn't work. Disregarding that bit of information (to start), why the hell does this code not place a 15x15 grid of JLabels inside of a JPanel?

gameBoard.setLayout(new FlowLayout());

for (int i = 0; i < 15; i++)
{
    for (int j = 0; j < 15; j++)
    {
        JLabel tile = new JLabel("");      
     tile.setHorizontalAlignment(SwingConstants.CENTER);
        tile.setPreferredSize(new Dimension(27, 27));
        tile.setBorder(new EtchedBorder());
        tile.setEnabled(false);

        gameBoard.add(tile);
    }
}

gameBoard is a JPanel defined through NetBeans' GUI Builder. It has a preferred size, a maximum size (same as preferred). The horizontal/vertical resizable options are disabled, yet when this code runs every button extends horizontally in a row without ever breaking.

If I understand correctly, FlowLayout is supposed to wrap elements when they reach the end of their containing element. This was happening before today, I don't really know why this behaviour stopped?

+4  A: 

It would be much better to use a GridLayout.

GridLayout gridLayout = new GridLayout(15, 15);

The flow layout may have changed due to using a new look and feel, different fonts or some Java VM upgrade. Defining a flow layout for this use is not recommended as you're reliant on underlying sizes that can vary across virtual machines or look and feels.

Pool
I don't want an alternative, rather a reason why the components are never wrapping.
Logan Serman
You're welcome.
Pool
I don't understand how this answer could be ranked up? It doesn't answer the question, provides an alternate solution and doubtful guidance!
Jeach
@Jeach: I feel this answer is a good attempt at answering the underlying question (how do I make it work). It is concise, clear and explains the motivations behind this choice. However, as you say, Jeach, it doesn't exactly answer the OP's question. For these reasons, I don't think this answer deserves to be chosen as the accepted answer by the OP, but I do think it's worth being voted up, as it may very well help someone else stuck in a similar situation.
Shawn
A: 

I've found great success & control using TableLayout and would recommend it to anyone. You can define the columns & rows using specific number of pixels, or you can just use TableLayout.PREFERRED for each column and/or row. Very nice & flexible Layout. We use it everywhere.

discgolfer
+2  A: 

In the interests of teaching a man how to fish:

Revision control systems are great. Even your system is making a back up of your source at frequent intervals.

A useful debugging technique is to take away things until it does work. Does setting a border cause you code to break. Going the other way, can you write a trivial program that demonstrates the problem.

Tom Hawtin - tackline
A: 

I figured it out. Apparently I had to set the preferredSize within the code itself. Just setting it in the Netbeans properties window is not enough. Strange.

Logan Serman