Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to fill available space with FlowLayout

  1. #1
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default How to fill available space with FlowLayout

    I am trying to make a gui, with a frame, 2 panels which are half of the height of the frame, and in the bottom panel 7 buttons next to each other using FlowLayout.
    this works, but it does not fill up the available space, so if I say the preferred size of the buttons is screenWidth / 7 to make room for all 7, it places 6 buttons on one row, puts blank space on both sides, and places the last button which could fit without the blank space a row below.

    Posted full code so you can copy paste and run for yourself,
    but the main question is how to fill up the remaining space with that 7th button using FlowLayout.

    In any case, Thank you for your time!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.Container;
    public class newGui
    {
        public newGui()
        {
            initUI();
     
        }
        public void addComponents(Container container)
        {
     
            JPanel display = new JPanel(); 
            JPanel interact = new JPanel();
            //sets Layout for containers
            container.setLayout(new BorderLayout());
            display.setLayout(new FlowLayout());
            interact.setLayout(new FlowLayout());
     
            display.setBackground(Color.BLACK);
     
            //creates icons for buttons
            //to run change with own jpg's
            ImageIcon[] icons = new ImageIcon[7];
            icons[0] = new ImageIcon(("hearts1.jpg"));
            icons[1] = new ImageIcon(("hearts2.jpg"));
            icons[2] = new ImageIcon(("hearts3.jpg"));
            icons[3] = new ImageIcon(("hearts4.jpg"));
            icons[4] = new ImageIcon(("hearts5.jpg"));
            icons[5] = new ImageIcon(("hearts6.jpg"));
            icons[6] = new ImageIcon(("hearts7.jpg"));
     
            //makes buttons
            JButton button1 = new JButton("derp");
            JButton button2 = new JButton("derp");
            JButton button3 = new JButton("derp");
            JButton button4 = new JButton("derp");
            JButton button5 = new JButton("derp");
            JButton button6 = new JButton("derp");
            JButton button7 = new JButton("derp");
     
            //sets icons to buttons
            button1.setIcon(icons[0]);
            button2.setIcon(icons[1]);
            button3.setIcon(icons[2]);
            button4.setIcon(icons[3]);
            button5.setIcon(icons[4]);
            button6.setIcon(icons[5]);
            button7.setIcon(icons[6]);
     
            //gets screen size and sets up dimension to use for button
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int screenHeight = screenSize.height;
            int screenWidth = screenSize.width;
            display.setPreferredSize(new Dimension(screenWidth, screenHeight/2));  
            Dimension buttonDimension = new Dimension(screenWidth / 7, screenHeight / 2 -40 );
     
            button1.setPreferredSize(buttonDimension);
            button2.setPreferredSize(buttonDimension);
            button3.setPreferredSize(buttonDimension);
            button4.setPreferredSize(buttonDimension);
            button5.setPreferredSize(buttonDimension);
            button6.setPreferredSize(buttonDimension);
            button7.setPreferredSize(buttonDimension);
     
            //adds buttons to bottom panel
            FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
     
            interact.add(button1,flow);
            interact.add(button2,flow);
            interact.add(button3,flow);
            interact.add(button4,flow);
            interact.add(button5,flow);
            interact.add(button6,flow);
            interact.add(button7,flow);
     
            //adds panels to container
            container.add(display,BorderLayout.NORTH);
            container.add(interact,BorderLayout.SOUTH);
     
        }
        //sets up frame and adds the components
        private void initUI()
        {
     
            JFrame frame = new JFrame("My App");
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     
            frame.setSize(screenSize);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
     
            addComponents(frame.getContentPane());
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
     
        }
     
        public static void main(String[] args)
        {
     
            javax.swing.SwingUtilities.invokeLater(new Runnable(){
     
                public void run()
                {
     
                    newGui gui = new newGui();
                }
            });
     
        }
    }
    English is not my native language (Typo alert).


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to fill available space with FlowLayout

    Have you tried using BoxLayout instead?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: How to fill available space with FlowLayout

    I can try that, I just figured there must be a way of doing this with flowlayout too
    English is not my native language (Typo alert).

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to fill available space with FlowLayout

    FlowLayout respects each component's preferredSize. BoxLayout will stretch or shrink components so they all fill the parent container. Switching to BoxLayout is the first thing I would try if I were you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to fill available space with FlowLayout

    Its usually discouraged to set preferred sizes on components. This is a job the layout and the component are supposed to do. In general my first go is the MigLayout (which is a third party layout). Its oftentimes exactly what you want because even the most basic code makes good looking GUI's.

  6. The Following User Says Thank You to Cornix For This Useful Post:

    Time4Java (September 18th, 2014)

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to fill available space with FlowLayout

    You could also consider GridLayout, if you want all of your components to be the same size.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: How to fill available space with FlowLayout

    Thank you all for the response, I'm using miglayout and it all seems to work
    But I do have one more question, when using swing, why should I repaint(or overwrite paintComponent)?
    So far I'm just swapping the corresponding icon on the JButtons and that seems to work without a hitch for now.

    When I read this it seems the components used repaint themselves when necessary, so why repaint?

    Thank you!

    "
    Swing components generally repaint themselves whenever necessary. When you invoke the setText method on a component, for example, the component automatically repaints itself and, if appropriate, resizes itself. "
    English is not my native language (Typo alert).

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to fill available space with FlowLayout

    Repainting is more of a concern when you're using a custom component. You aren't doing that, so it doesn't really matter.

    More info here: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Time4Java (September 18th, 2014)

  11. #9
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: How to fill available space with FlowLayout

    Thank you
    I'll close this now
    English is not my native language (Typo alert).

Similar Threads

  1. Fill seatpanel
    By MustTry in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 13th, 2014, 08:26 AM
  2. Fill
    By bejoykodiyan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 9th, 2012, 02:28 AM
  3. JPanel new FlowLayout
    By johnvasgird in forum Java Theory & Questions
    Replies: 8
    Last Post: April 26th, 2012, 03:18 PM
  4. img flowlayout
    By Fermen in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2011, 08:39 PM
  5. Help using Flowlayout
    By PaulPeter in forum AWT / Java Swing
    Replies: 0
    Last Post: April 5th, 2010, 02:30 PM

Tags for this Thread