5

I am trying to arrange two set of buttons for a calculator GUI. Each one uses a GroupLayout to make them. One set is the numbers (and "."), the other is for operation buttons. This basically works but if one of the buttons has double length (for example the equals button on my operations set) it throws the other buttons out of line. I will use the operations set as an example. There are two columns and four rows of buttons. The final row only has one button - the equals. I want to make this double length stretching across both columns. At the moment it simply pushes the second column along to the end of it when I want the second column to sit on top of it.

Here's the code for the layout - operLayout is the name of the layout for the operations, left and right brackets on the first row, + and - on the second, * and / on the third and equals on the last row. Each button has a minimumSize set elsewhere (they are all the same except equals is twice as long).

operLayout.setAutoCreateGaps(true);
operLayout.setAutoCreateContainerGaps(true);
operLayout.setVerticalGroup(operLayout
        .createSequentialGroup()
        .addGroup(
operLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(leftBracket)
                            .addComponent(rightBracket))
            .addGroup(operLayout.createParallelGroup().addComponent(add).addComponent(subtract))
            .addGroup(operLayout.createParallelGroup().addComponent(multiply).addComponent(divide))
            .addGroup(operLayout.createParallelGroup().addComponent(equals)));
    operLayout.setHorizontalGroup(operLayout
            .createSequentialGroup()
            .addGroup(
                    operLayout.createParallelGroup().addComponent(leftBracket).addComponent(add)
                            .addComponent(multiply).addComponent(equals))
            .addGroup(
                    operLayout.createParallelGroup().addComponent(rightBracket).addComponent(subtract)
                            .addComponent(divide)));

I understand why this is happening but I'm not sure how to sort it out. Is there a simple way? Or should I change the way I'm doing it? Thanks

mKorbel
  • 109,525
  • 20
  • 134
  • 319
PElshen
  • 135
  • 1
  • 1
  • 10
  • I applaud you for your choice of `GroupLayout` for this task. I believe this layout is the most powerful for laying out standard forms. It is easily overlooked because it is complicated to use. After learning how it works, I use it almost exclusively. – Erick Robertson Dec 19 '12 at 18:47
  • can reformat your code? Current;y it's rather unreadable. – Sled Dec 19 '12 at 18:50
  • yes, please do, although someone may have beat you to it. Thank you @ErickRobertson, I know what you mean, it seems complicated but once you understand it , it's really useful! – PElshen Dec 19 '12 at 19:18

1 Answers1

3

Put the equals component in its own parallel horizontal group.

First of all, make sure you use consistent indentation when you're using GroupLayout. I have found this absolutely vital in keeping track of what's going on.

The reason you're seeing the behavior you report is because the equals sign is part of the same horizontal parallel group as the first column of buttons. So when you make it double wide, it pushes the second column of buttons to the right. This is exactly what you're telling it to do because you're telling it to stay in the first parallel group (column).

In order to get the behavior you want, you have to layout that button separately, in parallel to the other buttons. You do this by putting it in its own parallel group. You probably want to put an alignment on this group also in order to get the best behavior. I think GroupLayout.Alignment.CENTER is what you want.

Also note that you don't need to create a new group if it's only going to have one component in it. Just add that component instead.

operLayout.setVerticalGroup(operLayout.createSequentialGroup()
    .addGroup(operLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        .addComponent(leftBracket)
        .addComponent(rightBracket))
    .addGroup(operLayout.createParallelGroup()
        .addComponent(add)
        .addComponent(subtract))
    .addGroup(operLayout.createParallelGroup()
        .addComponent(multiply)
        .addComponent(divide))
    .addComponent(equals));
operLayout.setHorizontalGroup(operLayout.createParallelGroup()
    .addGroup(operLayout.createSequentialGroup()
        .addGroup(operLayout.createParallelGroup()
            .addComponent(leftBracket)
            .addComponent(add)
            .addComponent(multiply))
        .addGroup(operLayout.createParallelGroup()
           .addComponent(rightBracket)
           .addComponent(subtract)
           .addComponent(divide)))
    .addComponent(equals));
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • thank you for your help! yes, I realise that was what I was telling it to, the result I got didn't surprise me, I just wasn't sure how to rearrange to get it right. I don't quite understand what you are saying to do though as you say to put it in it's own parallel group (horizontal or vertical? I would assume vertical or maybe both?) but then you say that if it's one component it doesn't need a group. I've tried the code you've given as well as variations on what you said with groups/just component, using center alignment but there's no change to what I had. – PElshen Dec 19 '12 at 23:11
  • okay sorry just realised you said horizontal to start off with but doing that doesn't solve the problem. – PElshen Dec 20 '12 at 01:02
  • I don't know why this isn't solving the problem for you. Make sure your indentations line up with your parentheses so that you're adding the component to the proper group. It should be added in parallel to the sequential group that contains the two columns within the horizontal group. This works, so I'm not sure what you're doing differently. But I wouldn't be surprised if it's in the wrong place based on the way the original code was organized. – Erick Robertson Jan 02 '13 at 17:17
  • I tidied up my code a lot and I'm (almost) certain I did it right but you're probably right. Since then I thought of another button to add and no longer need the double-length one. If I need it in the future I will refer back to this - thank you for your help! – PElshen Jan 02 '13 at 20:50
  • No problem. I commend you for using `GroupLayout`. It's extra work to break through the understanding barrier, but it's worth it. – Erick Robertson Jan 02 '13 at 22:56