1

There is some problem in Grouplayout of following code, Kindly help me to figure it out

I also have uploaded the output file, I am not sure if their is any problem with alignment. Kingly have a look and help me figure it out.

enter image description here

  package javaapplication1;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Progress extends JFrame implements ActionListener {

    static JFrame frame;
    public JLabel ClientIP;
    JTextField ip;
    JLabel ClientPassword;
    JTextField pass;
    JLabel Videoname;
    JTextField vname;
    JLabel perccomplete;
    JTextField percent;
    JLabel PacketsSent;
    JTextField pacsent;
    JLabel Connectiontype;
    JTextField conntype;
    JLabel noofvideossent;
    JTextField videosend;
    JButton disconnect;
    JButton refresh;
    JButton ok;

    public Progress() {

        ClientIP = new JLabel("Client's IP:");
        ClientPassword = new JLabel("Clients Password:");
        Videoname = new JLabel("Video being Transfered:");
        perccomplete = new JLabel("% of transfer Complete:");
        PacketsSent = new JLabel("No of Packets sent:");
        Connectiontype = new JLabel("Connection Type:");
        noofvideossent = new JLabel("No of Videos Sent:");


        ip = new JTextField(Ipad);
        ip.setColumns(20);
        pass = new JTextField(Ipass);
        pass.setColumns(20);
        vname = new JTextField(Iselvid);
        vname.setColumns(20);
        percent = new JTextField("10%");
        percent.setColumns(20);
        pacsent = new JTextField(String.valueOf(RTSPSeqNb));
        pacsent.setColumns(20);
        String c;
        if (clientConnected == true) {
            c = "TCP";
        } else {
            c = "not Connected";
        }
        conntype = new JTextField(c);
        conntype.setColumns(20);
        videosend = new JTextField(String.valueOf(videocount));
        videosend.setColumns(20);

        //Tell accessibility tools about label/textfield pairs.
        ClientIP.setLabelFor(ip);
        ClientPassword.setLabelFor(pass);
        Videoname.setLabelFor(vname);
        perccomplete.setLabelFor(percent);
        PacketsSent.setLabelFor(pacsent);
        Connectiontype.setLabelFor(conntype);
        noofvideossent.setLabelFor(videosend);

        //Lay out the labels in a panel.
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        // Turn on automatically creating gaps between components that touch
        // the edge of the container and the container.
        layout.setAutoCreateContainerGaps(true);

        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
                addComponent(ClientIP).addComponent(ClientPassword).addComponent(Videoname).addComponent(perccomplete).addComponent(PacketsSent).addComponent(Connectiontype).addComponent(noofvideossent));

        hGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
                addComponent(ip).addComponent(pass).addComponent(vname).addComponent(percent).
                addComponent(pacsent).addComponent(conntype).addComponent(videosend));

        layout.setHorizontalGroup(hGroup);


        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

        vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(ClientIP)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(ClientPassword)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(Videoname)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(perccomplete)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(PacketsSent)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(Connectiontype)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(noofvideossent));

        vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(ip)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(pass)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(vname)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(percent)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(pacsent)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(conntype)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(videosend));

        layout.setVerticalGroup(vGroup);




//        JPanel buttonPane = new JPanel(new GridLayout(0,1));
//        disconnect = new JButton("Disconnect Client");
//        disconnect.setActionCommand("Disconnect");
//        disconnect.addActionListener(this);
//        refresh = new JButton("Refresh Details");
//        refresh.setActionCommand("refresh");
//        refresh.addActionListener(this);
//        ok = new JButton("OK");
//        ok.setActionCommand("ok");
//        ok.addActionListener(this);
//        buttonPane.add(refresh);
//        buttonPane.add(disconnect);
//        buttonPane.add(ok);
//        add(buttonPane, BorderLayout.AFTER_LAST_LINE);
        setTitle("Find");
        pack();
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        //frame = new JFrame("Connected Client's Details");

        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        new Progress().setVisible(true);
        //Add contents to the window.
//        frame.add(new Progress());
//        //Display the window.
//        frame.pack();
//        frame.setVisible(true);
    }

    public static void main(String args[]) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        if ("Disconnect".equalsIgnoreCase(e.getActionCommand())) {
            ClientIPAddr = null;
            JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
        }
        if ("refresh".equalsIgnoreCase(e.getActionCommand())) {
            String Ipad = null, Ipass, Iselvid;
            if (ClientIPAddr == null || ClientIPAddr.equals("")) {
                Ipad = "Not Connected";
            } else {
                Ipad = ClientIPAddr.toString();
            }
            if (vFilePassword == null || vFilePassword.equals("")) {
                Ipass = "No Password";
            } else {
                Ipass = vFilePassword;
            }
            if (selected_video == null || selected_video.equals("")) {
                Iselvid = "Not Selected";
            } else {
                Iselvid = selected_video;
            }
            ip.setText(Ipad);
            pass.setText(Ipass);
            vname.setText(Iselvid);
            percent.setText("10%");
            pacsent.setText(String.valueOf(RTSPSeqNb));
            String c;
            if (clientConnected == true) {
                c = "TCP";
            } else {
                c = "not Connected";
            }
            conntype.setText(c);
            videosend.setText(String.valueOf(videocount));
        }
        if ("ok".equalsIgnoreCase(e.getActionCommand())) {
            frame.dispose();
        }

    }
}
Learner
  • 425
  • 5
  • 14

3 Answers3

3

GroupLayout is a constraints-based layout. The base principle is to determine positionning constraints between the components, on the x and y axis independently.

There are only two constraints : either the components are in sequence, or they are in parallel.

In your case, your layout is simple :

  • on the x axis, there are all the labels in parallel, and the all the textfields in parallel.
  • on the y axis, there are groups of [label and then textfield], all in parallel

The code for that is the following :

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Progress extends JPanel {

    private JLabel ClientIP;
    private JTextField ip;
    private JLabel ClientPassword;
    private JTextField pass;
    private JLabel Videoname;
    private JTextField vname;
    private JLabel perccomplete;
    private JTextField percent;
    private JLabel PacketsSent;
    private JTextField pacsent;
    private JLabel Connectiontype;
    private JTextField conntype;
    private JLabel noofvideossent;
    private JTextField videosend;

    public Progress() {

        ClientIP = new JLabel("Client's IP:");
        ClientPassword = new JLabel("Clients Password:");
        Videoname = new JLabel("Video being Transfered:");
        perccomplete = new JLabel("% of transfer Complete:");
        PacketsSent = new JLabel("No of Packets sent:");
        Connectiontype = new JLabel("Connection Type:");
        noofvideossent = new JLabel("No of Videos Sent:");


        ip = new JTextField();
        pass = new JTextField();
        vname = new JTextField();
        percent = new JTextField();
        pacsent = new JTextField();
        conntype = new JTextField();
        videosend = new JTextField();

        ip.setColumns(20);
        pass.setColumns(20);
        vname.setColumns(20);
        percent.setColumns(20);
        pacsent.setColumns(20);
        conntype.setColumns(20);
        videosend.setColumns(20);

        //Tell accessibility tools about label/textfield pairs.
        ClientIP.setLabelFor(ip);
        ClientPassword.setLabelFor(pass);
        Videoname.setLabelFor(vname);
        perccomplete.setLabelFor(percent);
        PacketsSent.setLabelFor(pacsent);
        Connectiontype.setLabelFor(conntype);
        noofvideossent.setLabelFor(videosend);

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(Alignment.LEADING)
                        .addComponent(ClientIP)
                        .addComponent(ClientPassword)
                        .addComponent(Videoname)
                        .addComponent(perccomplete)
                        .addComponent(PacketsSent)
                        .addComponent(Connectiontype)
                        .addComponent(noofvideossent))
                    .addGroup(layout.createParallelGroup(Alignment.LEADING)
                        .addComponent(ip)
                        .addComponent(pass)
                        .addComponent(vname)
                        .addComponent(percent)
                        .addComponent(pacsent)
                        .addComponent(conntype)
                        .addComponent(videosend)));

        layout.setVerticalGroup(
                layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(ClientIP)
                        .addComponent(ip))
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(ClientPassword)
                        .addComponent(pass))
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(Videoname)
                        .addComponent(vname))
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(perccomplete)
                        .addComponent(percent))
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(PacketsSent)
                        .addComponent(pacsent))
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(Connectiontype)
                        .addComponent(conntype))
                    .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(noofvideossent)
                        .addComponent(videosend)));
    }


    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("swing.boldMetal", Boolean.FALSE);
                JFrame f = new JFrame("test frame");
                f.setContentPane(new Progress());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setTitle("Find");
                f.pack();
                f.setVisible(true);
            }
        });
    }

}

A GroupLayout is relatively easy to understand, when the code is properly indented. I replaced the Alignement.LEADING by Alignement.BASELINE, in order to align on the baseline (the text of the labels and of the textfields are on the same line).

barjak
  • 10,842
  • 3
  • 33
  • 47
  • +1 for readability; see also this [example](http://stackoverflow.com/a/14858272/230513) that adds labeled fields in a loop. – trashgod Apr 18 '13 at 10:41
  • barjak's [sscce](http://sscce.org/) compiles and executes correctly; your update does not compile. I'd suggest backing out the changes until it works again, and focus on the first change to break your next update. – trashgod Apr 19 '13 at 11:29
0

In my experience, GroupLayout is best used with a GUI builder such as the one from NetBeans as it was build especially for this purpose. So just fix the layout with your GUI builder.

If you want to manipulate the layout "manually", there are easier Layout Managers for this task, IMHO.

Puce
  • 37,247
  • 13
  • 80
  • 152
  • and very similair notification in Oracle tutorial too – mKorbel Apr 18 '13 at 10:15
  • Well, it is true that `GroupLayout` is well suited for a GUI builder's needs. But really, `GroupLayout` is perfectly usable when written by hand. The code you write by hand is generally easy to write, compact, and not that hard to maintain afterwards. Also, there are a lot of constructs that you can express with the `GroupLayout` API, that is just impossible to express using a GUI builder. – barjak Apr 18 '13 at 10:20
  • @barjak complex layoutManagers often are a matter of taste - and GroupLayout isn't what I personnally would consider tasteful :-) As to maintenance: assume you want to add another label/text pair. This would require to _change_ the layout code at three locations (adding the comps individually to the parallelGroup under the horizontalGroup plus adding a new group containing both under the verticalGroup). My expectation to a easy-to-maintain layout would be to not touch to layout at all, but simply add the components to the panel and be done (like you would in a MigLayout, f.i.) – kleopatra Apr 18 '13 at 10:35
  • 1
    @kleopatra's point is well-taken; this (counter-)[example](http://stackoverflow.com/a/14858272/230513) adds labeled fields to a `GroupLayout` in a loop, which may be easily adapted to data-driven use. – trashgod Apr 18 '13 at 10:47
0

Thanks a lot for you help. I modified the code as follows and it worked.. The alignment was fine but their was problem in vertical axis..

GroupLayout layout = new GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);

         // Turn on automatically creating gaps between components that touch
         // the edge of the container and the container.
         layout.setAutoCreateContainerGaps(true);

         GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
         hGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
                 addComponent(ClientIP).addComponent(ClientPassword).addComponent(Videoname).addComponent(perccomplete).addComponent(PacketsSent).addComponent(Connectiontype).addComponent(noofvideossent));

         hGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).
                 addComponent(ip).addComponent(pass).addComponent(vname).addComponent(percent).
                 addComponent(pacsent).addComponent(conntype).addComponent(videosend).addComponent(disconnect).addComponent(refresh).addComponent(ok));
         layout.setHorizontalGroup(hGroup);


         GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

         vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(ClientIP).addComponent(ip)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(ClientPassword).addComponent(pass)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(Videoname).addComponent(vname)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(perccomplete).addComponent(percent)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(PacketsSent).addComponent(pacsent)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(Connectiontype).addComponent(conntype)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(noofvideossent).addComponent(videosend));

         vGroup.addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(disconnect)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(refresh)).addGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(ok));
         layout.setVerticalGroup(vGroup);
Learner
  • 425
  • 5
  • 14
  • Please update your question with new code; an [*sscce*](http://sscce.org/) and [*screenshot*](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) are usually well received. – trashgod Apr 18 '13 at 11:44
  • This non-answer can be deleted now. – trashgod Apr 19 '13 at 11:30