Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Choosing LayoutManagers

    Hi Guys,

    This may sound a rather trivial question, but I need advice on choosing a LayoutManager. My scenario is as follows:

    I have a JFrame in which via panels are switched in and on based on user input.

    each of these panels is defined in their own class. One of the panels resembles the contentPane of the following:


    http://i.imgur.com/nn8XK.png

    I have tried using GridBagLayout, However the grid does not cover the panel no matter what I tried.

    I would really appreciate any advice possible.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Choosing LayoutManagers

    Looks like something that BoxLayout could handle.

    Can you post an MCVE showing what you've tried so far? This shouldn't be your whole project, just enough for us to copy into our own IDE and run.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Re: Choosing LayoutManagers

    Here is the code for the relevant pane. Don't no if it is MCVE.

    Java Code:
    public class MainMenu extends SizeablePanel {
        ActionListener parentFrame;
    
    
        public MainMenu(ActionListener l) {
    
            this.parentFrame = l;
    
            //Add title
            addTitle("\u03bbambda");
    
            //Add Border
            setBorder(new EmptyBorder(0, width / 4, height / 8, width / 4));
    
            //Create and add button pane:
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new GridLayout(0, 1,0,width/30));
    
            //Adding  buttons to the pane
            addButton("Calculator", buttonPane);
            addButton("Tutor", buttonPane);
            addButton("Marker", buttonPane);
            add(buttonPane, BorderLayout.CENTER);
    
    
            setVisible(true);
        }
    
    
        private void addButton(String text, Container container) {
            JButton button = new JButton(text);
            button.setFont(new Font("Serif", Font.PLAIN, 20));
            button.addActionListener(parentFrame);
            container.add(button);
    
    
        }
    
    
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Choosing LayoutManagers

    No, an MCVE should be runnable if we copy and paste it. I don't see a JFrame or a main() method here.

    Also, have you tried using a BoxLayout?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Re: Choosing LayoutManagers

    What would the benefit of using Boxlayout as opposed to GridLayout as above?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Choosing LayoutManagers

    GridLayout isn't working for you.

    You can keep trying to get GridLayout to work, and I'm sure it's possible, but I personally would use BoxLayout. It's up to you.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Re: Choosing LayoutManagers

    Despite the spam.

    I am in no doubt that you have that more experience working with Java GUI's and therefore I am inclined to trust your judgement.

    Therefore I have three follow-up questions:

    Are you Suggesting the I only use BoxLayout for the panel containing the buttons?

    How will I deal with the gaps between the buttons?


    BTW, why does GridBagLayout not cover the entire panel?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Choosing LayoutManagers

    Spam removed.

    I would personally use BoxLayout for the whole thing, at least judging from your picture. The reason for this is simplicity: you only have a single column of components, which makes any other layout overkill imho.

    You can add glue components to a BoxLayout to get those spaces.

    And different layouts respect sizes differently. I don't want to comment without an MCVE though, as we might be talking about different things.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  9. #9
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    19
    Rep Power
    0

    Default Re: Choosing LayoutManagers

    How, would I centre the title as per the picture?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Choosing LayoutManagers

    I really recommend trying something out and posting what you've got.

    The tutorial for BoxLayout tells you how to align components, as well as how to use spacing: How to Use BoxLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Choosing LayoutManagers

    BoxLayout really benefits from playing around.
    Create the basic layout you want then fiddle about with glue and struts to get things spaced the way you want.
    It can be a bit frustrating if you're attempting something complex (that is, lots of different horizontal and vertical layouts), but your picture looks like a fairly standard vertical box, as Kevin says.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Can you help me choosing a framework?
    By macaso in forum Web Frameworks
    Replies: 8
    Last Post: 11-16-2012, 09:25 AM
  2. Choosing db option
    By Levian in forum JDBC
    Replies: 6
    Last Post: 03-17-2012, 02:21 AM
  3. Help with LayoutManagers
    By BiteMuncher in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 12:52 PM
  4. Choosing a degree concentration
    By xcallmejudasx in forum Jobs Discussion
    Replies: 11
    Last Post: 08-14-2009, 11:30 AM
  5. Swing and LayoutManagers
    By gammaman in forum New To Java
    Replies: 2
    Last Post: 07-24-2009, 02:17 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
  •