Results 1 to 2 of 2
  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default GroupLayout: JLabel auto-added to vertical group?

    Hi

    My JLabel "Title" is automatically being added to my vertical group in the method Init();. If I add it to the vertical group, it says that it is not attached to a horizontal group, thus it is adding a second one.

    Java Code:
    package platinum;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.awt.Color.*;
    import static javax.swing.GroupLayout.Alignment.*;
    public class MainScreen {
        int nGap = 20;
        JFrame frame = new JFrame();
        GridLayout grdlayout = new GridLayout(6, 1);
        JPanel Buttonlayout = new JPanel(grdlayout), TopPanel = new JPanel(),
                BottomPanel = new JPanel(), LeftPanel = new JPanel(),
                RightPanel = new JPanel();
        GroupLayout grplayout = new GroupLayout(frame.getContentPane()),
                grplayoutlabel = new GroupLayout(TopPanel);
        JButton[] ButtonList = new JButton[6];
        JLabel Title = new JLabel("Gooey Application");
        String[] ButtonNames = {"Home", "Getting Started Checklist", "Contacts",
        "To Do List", "Appointments", "Messenger"}, URLs = {"google.ca", 
        "onemanga.com", "java-forums.org", "onepieceofbleach.com",
        "hidden-street.net", "sing365.com"};
        public MainScreen(){
            Init();
            InitLayout();
            CreateGUI();
        }
        public void Init(){
            for (int i = 0; i < 6; i++){
                final int x = i;
                ButtonList[i] = new JButton();
                ButtonList[i].setText(ButtonNames[i]);
                ButtonList[i].setActionCommand(ButtonNames[i]);
                ButtonList[i].setBackground(Color.white);
                Buttonlayout.add(ButtonList[i]);
                ButtonList[i].addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent ev){
                        try{
                            java.awt.Desktop.getDesktop().browse(new URI("http://www." + URLs[x]));
                        }
                        catch (Exception e){
                            System.out.println(e.getMessage());
                        }
                    }
                });
            }
            Dimension buttonSize = ButtonList[1].getPreferredSize();
            grplayoutlabel.setHorizontalGroup(
                grplayoutlabel.createSequentialGroup()
                    .addGap((int)(buttonSize.getWidth()/2) + nGap)
                    .addComponent(Title)
            );
            grplayoutlabel.setVerticalGroup(
                grplayout.createSequentialGroup()
                    .addGap(nGap)
                    //.addComponent(Title)
            );
    
        }
        public void InitLayout(){
            Dimension buttonSize = ButtonList[1].getPreferredSize();
            Sizes(buttonSize);
            Colors();
            Buttonlayout.setPreferredSize(new Dimension((int)(buttonSize.getWidth()),
                    (int)(buttonSize.getHeight() * 6)));
            grplayout.setHorizontalGroup(
                grplayout.createParallelGroup(LEADING)
                    .addComponent(TopPanel)
                    .addComponent(BottomPanel)
                    .addGroup(grplayout.createSequentialGroup()
                        .addComponent(LeftPanel)
                        .addComponent(Buttonlayout)
                        .addComponent(RightPanel)
            ));
            grplayout.setVerticalGroup(
                grplayout.createSequentialGroup()
                    .addComponent(TopPanel)
                    .addGroup(grplayout.createParallelGroup()
                        .addComponent(LeftPanel)
                        .addComponent(RightPanel)
                        .addComponent(Buttonlayout))
                    .addComponent(BottomPanel)
            );
        }
        public void Sizes(Dimension bs){
            LeftPanel.setPreferredSize(new Dimension(nGap, (int)(bs.getHeight()*6)));
            RightPanel.setPreferredSize(new Dimension(nGap, (int)(bs.getHeight()*6)));
            BottomPanel.setPreferredSize(new Dimension(nGap*2 + (int)(bs.getWidth()), 50));
            TopPanel.setPreferredSize(new Dimension(nGap*2 + (int)(bs.getWidth()), 50));
        }
        public void Colors(){
            LeftPanel.setBackground(Color.green);
            RightPanel.setBackground(Color.red);
            BottomPanel.setBackground(Color.black);
            TopPanel.setBackground(Color.black);
        }
        public void CreateGUI(){
            //frame.setUndecorated(true);
            frame.getContentPane().setLayout(grplayout);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
        public static void main(String args[]){
            new MainScreen();
        }
    }
    I need to add it to the vertical group so that I can add a gap. Thanks for any help!

    Jamie

  2. #2
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Nevermind. I'm stupid. I added the sequential group to grplayout rather than grplayoutlabel. Sorry!

    Now the gap that I am adding to the vertical sequential gap isn't working.
    Last edited by dunafrothint; 03-02-2010 at 07:46 PM.

Similar Threads

  1. Auto contrast and auto brightness
    By oxxxis in forum Java 2D
    Replies: 0
    Last Post: 01-21-2010, 09:32 PM
  2. Doubt with GroupLayout
    By emylyano3 in forum AWT / Swing
    Replies: 1
    Last Post: 06-28-2009, 03:16 AM
  3. help about class GroupLayout
    By MaHa in forum NetBeans
    Replies: 2
    Last Post: 09-08-2008, 12:25 AM
  4. Auto-complete/Auto-fix for custom statement
    By dark_cybernetics in forum Eclipse
    Replies: 0
    Last Post: 08-19-2008, 12:19 PM
  5. Replies: 6
    Last Post: 07-10-2008, 08:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •