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

Thread: GridLayout and size problems...

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GridLayout and size problems...

    Hey there,

    So i need to create a matching pairs game. So far i've managed to create the table and all the cards. I though using a GridLayout would be suitable so it looks like the cards have been dealt across the table. The only problem is for some reason the cards are no longer the size i specified setSize(150, 200). So the button is no longer the same size as the image contained within in. I'm going to assume it's something to do with GridLayout? As it was working fine before this, but despite my best efforts I can't seem to find what i need to do to fix it. Any help would be much appretiated. Here's my code.

    import javax.swing.*;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
     
    public class Cards implements  ItemListener{
     
        // Definition of global values and items that are part of the GUI.
     
        JToggleButton one, two, three, four, oneDup, twoDup, threeDup, fourDup;
        JPanel table;
     
        ImageIcon backImg = new ImageIcon("back.jpg");
        ImageIcon oneImg = new ImageIcon("one.jpg");
        ImageIcon twoImg = new ImageIcon("two.jpg");
        ImageIcon threeImg = new ImageIcon("three.jpg");
        ImageIcon fourImg = new ImageIcon("four.jpg");
     
     
        public JPanel createContentPane (){
     
            // JPanel to place everything on.
            table = new JPanel();
            table.setBackground(Color.green);
            GridLayout dealer = new GridLayout(4, 2, 10, 10);
            table.setLayout(dealer);
     
            one = new JToggleButton(backImg);        // Create cards (JToggleButtons)
            one.setSize(150,200);
            one.addItemListener(this);
            table.add(one);
     
            two = new JToggleButton(backImg);       
            two.setSize(150,200);
            two.addItemListener(this);
            table.add(two);
     
            three = new JToggleButton(backImg);      
            three.setSize(150,200);
            three.addItemListener(this);
            table.add(three);
     
            four = new JToggleButton(backImg);
            four.setSize(150,200);
            four.addItemListener(this);
            table.add(four);
     
            oneDup = new JToggleButton(backImg);      
            oneDup.setSize(150,200);
            oneDup.addItemListener(this);
            table.add(oneDup);
     
            twoDup = new JToggleButton(backImg);      
            twoDup.setSize(150,200);
            twoDup.addItemListener(this);
            table.add(twoDup);
     
            threeDup = new JToggleButton(backImg);
            threeDup.setSize(150,200);
            threeDup.addItemListener(this);
            table.add(threeDup);
     
            fourDup = new JToggleButton(backImg);
            fourDup.setSize(150,200);
            fourDup.addItemListener(this);
            table.add(fourDup);
     
            table.setOpaque(true);
            return table;
        }
     
     
     
        public void itemStateChanged(ItemEvent e) {               // Flip card over when pressed
            if(e.getStateChange() == ItemEvent.SELECTED)
            {
                one.setSelectedIcon(oneImg);
                two.setSelectedIcon(twoImg);
                three.setSelectedIcon(threeImg);
                four.setSelectedIcon(fourImg);
                oneDup.setSelectedIcon(oneImg);
                twoDup.setSelectedIcon(twoImg);
                threeDup.setSelectedIcon(threeImg);
                fourDup.setSelectedIcon(fourImg);
            }
            else
            {
                one.setSelectedIcon(backImg);
            } 
        }
     
        private static void createAndShowGUI() {      // Set up JFrame , will be removed when other JPanels added
     
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame frame = new JFrame("[=] JToggleButton [=]");
     
            //Create and set up the content pane.
            Cards demo = new Cards();
            frame.setContentPane(demo.createContentPane());
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(1400, 700);
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: GridLayout and size problems...

    Use setPreferredSize() method, instead of setSize()

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:56 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GridLayout and size problems...

    I put this line into each of the JToggleButtons

    one.setPreferredSize(new Dimension(150,200));

    but it seems to do nothing.

Similar Threads

  1. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 01:15 AM
  2. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 07:39 PM
  3. Moving Icon in gridlayout SWING
    By Loodistobilo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 24th, 2010, 07:59 PM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 05:28 PM
  5. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 08:21 AM