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 4 of 4

Thread: GridLayout not working the way i want it

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default GridLayout not working the way i want it

    Is there a way to keep the GridLayout one size so it doesnt change when you make a window(frame) bigger or smaller Ill post 2 images so you can see for yourself.
    GridBagLayout is this possible? i want to keep 8*8 squares 64 for the chessboard.

    chessboard.jpg

    chessboard2.jpg

    code

     
    package view;
     
    import java.awt.*;
     
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
     
    import controller.Controller;
     
    public class ChessBoardView extends JPanel
    {
        private final static ImageIcon DARK_BROWN = new ImageIcon 
        (ChessBoardView.class.getResource("assets/sqb.gif"));
        private final static ImageIcon LIGHT_BROWN = new ImageIcon 
        (ChessBoardView.class.getResource("assets/sqw.gif"));;
        private JLabel sqb;
        private JLabel sqw;
     
    	public ChessBoardView (Controller controller)
        {
     
            Dimension boardSize = new Dimension(200, 200);
     
            setLayout( new GridLayout(8, 8) );
            setPreferredSize( boardSize );
            setBounds(10, 10, boardSize.width, boardSize.height);
            setBorder(BorderFactory.createLineBorder(Color.BLACK));
     
     
     
            for (int i = 0; i < 64; i++) {
                JPanel square = new JPanel( new BorderLayout() );
                super.add( square );
     
                int row = (i / 8) % 2;
                if (row == 0)
     
                square.add( i % 2 == 0 ? new JLabel (LIGHT_BROWN) : new JLabel (DARK_BROWN) );
                else
                square.add( i % 2 == 0 ? new JLabel (DARK_BROWN) : new JLabel (LIGHT_BROWN) );
            }
     
        }  
    }


  2. #2
    Member
    Join Date
    Aug 2013
    Location
    Netherlands
    Posts
    38
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default GridBagLayout not works the way i want. Looking for alternatives or a solution

    The following link takes you to my thread but i posted it in the wrong categorie
    thanks in advance

    http://www.javaprogrammingforums.com...ay-i-want.html

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: GridLayout not working the way i want it

    Hello.
    For each component you are adding using the GridLayout set the attributes "width" and "height" to fixed values.

    Syed.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GridLayout not working the way i want it

    Syed,

    gokuball has become Wolverine89, and this topic has been continued and/or overcome by many other topics contained in the thread Chess Program.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    jps (August 16th, 2013)

Similar Threads

  1. Problem with GridLayout arrangement.
    By oror84 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 6th, 2012, 10:46 AM
  2. [SOLVED] JPanel GridLayout isn't working
    By mzamboen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 14th, 2012, 02:10 PM
  3. GridLayout help
    By sambar89 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2011, 11:00 PM
  4. GridLayout and size problems...
    By sambar89 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 6th, 2011, 01:06 PM
  5. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM