0

Here I want to add buttons as per 4-3-3 formation in football but i am getting 3-3-3. how can i put 4 buttons in one row?? I have been referral to this site: https://weblogs.java.net/blog/tpavek/archive/2006/02/getting_to_know_2.html

Code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import static javax.swing.GroupLayout.Alignment.*;

class Abc extends JFrame
{
JButton b[];

Abc()
{
b=new JButton[11];
JPanel jp=new JPanel();
for(int i=0;i<b.length;i++)
{
b[i]=new JButton();
}
GroupLayout layout=new GroupLayout(jp);
jp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b[0])
.addComponent(b[1])
    .addGroup(layout.createSequentialGroup())
    .addComponent(b[2])
    .addComponent(b[3]))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b[4])
.addComponent(b[5])
    .addGroup(layout.createSequentialGroup())
    .addComponent(b[6]))
.addComponent(b[7])
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b[8])
.addComponent(b[9])
    .addGroup(layout.createSequentialGroup())
    .addComponent(b[10]))
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b[0])
.addComponent(b[4])
.addComponent(b[8]))

.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b[1])
.addComponent(b[5])
.addComponent(b[9]))
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b[2])
.addComponent(b[3])
.addComponent(b[6])
.addComponent(b[10]))

.addComponent(b[7])
);
setTitle("kuvh b");
setSize(1000,1000);
for(int i=0;i<11;i++)
{
add(b[i]);
}
add(jp);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                                  "javax.swing.plaf.metal.MetalLookAndFeel");
                                //  "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                                //UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception ex) {
                    ex.printStackTrace();

           }
                new Abc().setVisible(true);
            }
        });
    }
}

I want to make a structure of 4-3-3 formation with this. Please help me The code is resulting in 3-3-3 formation. there are 3 buttons on third row but i want 4 how can i do this please help

See the output: [1]:

  1. https://i.stack.imgur.com/1NnDz.jpg

i hope i will find my solution as far as possible

  • Your desired output is not clear. You say you want "4-3-3 as in football", but I don't know what that means. You don't say whether you want the 4 to be a row or a column, or on which end. You say you are getting 3-3-3, but you actually have 10 buttons on the output. You don't say what you've tried, or which buttons you want where. Most of us don't want to just poke around hoping we accidentally land on the answer that you want, so look at your question from the standpoint of someone who does not already know what you're after, and describe it. – arcy Jan 06 '15 at 15:43
  • i want a output that consists of 4 rows and 3 columns – Kapil Arora Jan 06 '15 at 16:11
  • first row consists of 3 buttons Second row is same as first one and third row contains 4 buttons and at last 4th row contains one button in center – Kapil Arora Jan 06 '15 at 16:12
  • Ok, I was going to get further into this, but I've run out of time to spend on it now. First figure out what you want your 4-button-row to look like -- are the first three buttons lined up with the three buttons on the first two rows, or do you want all four buttons spread out and centered on their own? That will tell you whether to put the first three buttons of those rows in the same vertical groupings as the buttons in the first two rows. Each grouping corresponds to either a vertical or horizontal row. Try posting a picture of what you want. – arcy Jan 06 '15 at 16:34
  • http://newsimg.bbc.co.uk/media/images/41705000/gif/_41705434_4_3_3_416.gif – Kapil Arora Jan 07 '15 at 03:20

1 Answers1

0

I've found two ways to do something like you want; centering the buttons turns out to be something that Swing does not make as easy as it might, but then centering components is probably not as common as other alignments.

You can center components in a FlowLayout; the disadvantage in a FlowLayout is that, if the user shrinks the window to the point the components no longer fit, the layout wraps the components. This is very useful for some things, but not for your football players. I've wrapped my example in a scrollpane so this won't happen.

The other way to center components is with GroupLayout, but GroupLayout is not good for the overall layout you are trying to achieve. GroupLayout is intended for use where you have overall rows and columns in which to line things up, and your four lines of football players are not lined up that way vertically, only horizontally. But you can use the centering characteristic of GroupLayout to do the horizontal centering, and make a separate GroupLayout for each line.

My example uses FlowLayout for the first line, and GroupLayout for the second, just to show how it could be done. I did not address the problem of the gap that appears between the players' lines when the window is made large enough. Especially for examples, I do not use the style of tacking method invocations onto other method invocations and constructors; I think the deeply nested parentheses and non-straightforward logic of this style makes it more difficult to figure out (or keep track of) what's going on.

You can use GridBagLayout to center things, also, but I don't use it at all if anything else will do what I need.

I hope this answers your question.

package grouplayout;

import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main2 extends JFrame
{
    public static void main(String ... arguments)
    {
        Main2 main2 = new Main2();
        main2.createUI();
        main2.setVisible(true);
    }

    public void createUI()
    {
        JPanel wingPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setHgap(35);
        wingPanel.setLayout(flowLayout);

        JButton btnone = new JButton("Lwing");
        JButton btntwo = new JButton("center");
        JButton btnthr = new JButton("Rwing");
        wingPanel.add(btnone);
        wingPanel.add(btntwo);
        wingPanel.add(btnthr);

        // -------------------------------------------

        JButton mid1 = new JButton("mid1");
        JButton mid2 = new JButton("mid2");
        JButton mid3 = new JButton("mid3");
        JButton mid4 = new JButton("mid4");
        JPanel midfieldPanel = new JPanel();
        GroupLayout groupLayout = new GroupLayout(midfieldPanel);

        GroupLayout.SequentialGroup horizontalGroup = groupLayout.createSequentialGroup();
        groupLayout.setHorizontalGroup(horizontalGroup);
        horizontalGroup.addComponent(mid1);
        horizontalGroup.addComponent(mid2);
        horizontalGroup.addComponent(mid3);
        horizontalGroup.addComponent(mid4);

        GroupLayout.SequentialGroup verticalGroup = groupLayout.createSequentialGroup();
        groupLayout.setVerticalGroup(verticalGroup);

        GroupLayout.ParallelGroup midButtonGroup = groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER);
        midButtonGroup.addComponent(mid1);
        midButtonGroup.addComponent(mid2);
        midButtonGroup.addComponent(mid3);
        midButtonGroup.addComponent(mid4);

        verticalGroup.addGroup(midButtonGroup);

        JPanel teamPanel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(teamPanel, BoxLayout.PAGE_AXIS);
        teamPanel.setLayout(boxLayout);

        teamPanel.add(wingPanel);
        teamPanel.add(midfieldPanel);

        JScrollPane scrollPane = new JScrollPane(teamPanel);
        getContentPane().add(scrollPane);

        pack();

    }
}

EDIT: as requested, the below does the same thing with only GroupLayout.

There is no interaction between the two groups, because GroupLayout aligns things in columns, and your players are not in columns.

And yes, I suppose it is difficult -- GroupLayout, as I understand it, was really intended for use by GUI builder tools, not really for building UIs by hand. I personally have a supporter class or two that allows GroupLayout UIs to be built with slightly simpler logic. But in any event, I think you need to understand the building blocks:

GroupLayout allows - and requires - that you place each component in both horizontal and vertical row/column position independently; this is useful since so many UIs require rows and columns of mixed components and variable extra components.

A sequential group of components in dimension X is arranged sequentially in dimension X; a parallel group in dimension X is also arranged sequentially, but perpendicular to dimension X.

The layout maintains preferred sizes of components; row width and column height are set at the maximum preferred size of the constituent components.

The overall GroupLayout object has one vertical and one horizontal grouping; within that, sequential and parallel groups are created to create the overall layout desired.

I know the examples in the tutorial(s) I've read do not create separate variables to hold the internal sequential and parallel groups, preferring to use forms like new X().addComponent().addGroup() etc. But I think that makes it harder to understand what the code is actually doing, not easier; and the nested parentheses become their own maintenance problem. So I think this is a better way to do things, especially for those just getting started with this layout.

package grouplayout;

import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main3 extends JFrame
{
    public static void main(String ... arguments)
    {
        Main3 main2 = new Main3();
        main2.createUI();
        main2.setVisible(true);
    }

    public void createUI()
    {
        JButton btnone = new JButton("Lwing");
        JButton btntwo = new JButton("center");
        JButton btnthr = new JButton("Rwing");

        JPanel wingPanel = new JPanel();
        GroupLayout wingGroupLayout = new GroupLayout(wingPanel);

        GroupLayout.SequentialGroup wingHorizontalGroup = wingGroupLayout.createSequentialGroup();
        wingGroupLayout.setHorizontalGroup(wingHorizontalGroup);
        wingHorizontalGroup.addComponent(btnone);
        wingHorizontalGroup.addComponent(btntwo);
        wingHorizontalGroup.addComponent(btnthr);

        GroupLayout.SequentialGroup wingVerticalGroup = wingGroupLayout.createSequentialGroup();
        wingGroupLayout.setVerticalGroup(wingVerticalGroup);

        GroupLayout.ParallelGroup wingButtonGroup = wingGroupLayout.createParallelGroup();
        wingButtonGroup.addComponent(btnone);
        wingButtonGroup.addComponent(btntwo);
        wingButtonGroup.addComponent(btnthr);

        wingVerticalGroup.addGroup(wingButtonGroup);

        // -------------------------------------------

        JButton mid1 = new JButton("mid1");
        JButton mid2 = new JButton("mid2");
        JButton mid3 = new JButton("mid3");
        JButton mid4 = new JButton("mid4");
        JPanel midfieldPanel = new JPanel();
        GroupLayout groupLayout = new GroupLayout(midfieldPanel);

        GroupLayout.SequentialGroup horizontalGroup = groupLayout.createSequentialGroup();
        groupLayout.setHorizontalGroup(horizontalGroup);
        horizontalGroup.addComponent(mid1);
        horizontalGroup.addComponent(mid2);
        horizontalGroup.addComponent(mid3);
        horizontalGroup.addComponent(mid4);

        GroupLayout.SequentialGroup verticalGroup = groupLayout.createSequentialGroup();
        groupLayout.setVerticalGroup(verticalGroup);

        GroupLayout.ParallelGroup midButtonGroup = groupLayout.createParallelGroup(GroupLayout.Alignment.CENTER);
        midButtonGroup.addComponent(mid1);
        midButtonGroup.addComponent(mid2);
        midButtonGroup.addComponent(mid3);
        midButtonGroup.addComponent(mid4);

        verticalGroup.addGroup(midButtonGroup);

        JPanel teamPanel = new JPanel();
        BoxLayout boxLayout = new BoxLayout(teamPanel, BoxLayout.PAGE_AXIS);
        teamPanel.setLayout(boxLayout);

        teamPanel.add(wingPanel);
        teamPanel.add(midfieldPanel);

        JScrollPane scrollPane = new JScrollPane(teamPanel);
        getContentPane().add(scrollPane);

        pack();

    }
}
arcy
  • 12,845
  • 12
  • 58
  • 103
  • Its little bit difficult for me. Although it seems like its fine. Can you give me the same result with only GroupLayout. – Kapil Arora Jan 08 '15 at 10:20
  • I am not as good as you are in java. Sir, please give me a easy method to solve my problem – Kapil Arora Jan 09 '15 at 06:55
  • is there any solution by which i could merge different layouts and gets the required result??? – Kapil Arora Jan 09 '15 at 06:57
  • I think I've done enough programming for you. There may be a simpler way of doing this, but I don't know of one offhand. If this is beyond your current programming capabilities, the answer is not necessarily easier programming. A standard layout manager may not be the right tool for your job; their purpose is easier creation of standard UIs -- windows and dialogs with buttons, text fields, drop-downs, etc. -- and it appears you don't have that. Also be aware that SO is great at providing answers to specific technical questions, but that you seem to be expecting something else. – arcy Jan 09 '15 at 13:11