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

Thread: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

  1. #1
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    Hello all. First off, I apologize if this is the wrong section for seeking this kind of help, I am a beginner programming student. I was assigned to create the gridlines for a Sudoku puzzle, which is basically a 3x3 grid, with a 3x3 grid within each of the nine box of the original grid. This puzzle does not have to function, I only have to create the grid (which is to be comprised of JTextFields) and some random, non-functioning buttons to the right of the puzzle. The original largest 3x3 grid must have a thick border (not just around the perimeter of the box, but the gridlines themselves), while the smaller 3x3 grids within must have thinner gridline borders.

    My first inclination was to create a nested for loop to house a 3x3 grid within each square of another 3x3 grid. So I attempted to do that but came up short in the following code:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class Sudoku1 extends JFrame {
    	public Sudoku1(){
    		// Create panel1 with GridLayout
    		JPanel panel1 = new JPanel();
    		JPanel panel3 = new JPanel();
    		Border lineBorder = new LineBorder(Color.BLACK, 3);
    		Border lineBorder2 = new LineBorder(Color.BLACK, 2);
     
    		for (int i = 1; i <= 9; i++){
    			panel1.setLayout(new GridLayout(3, 3));
    			panel1.setBorder(lineBorder);
    			panel1.add(new JTextField(1));
    			for (int j = 1; j <= 9; j++){
    				panel3.setLayout(new GridLayout(3, 3));
    				panel1.setBorder(lineBorder2);
    				panel1.add(new JTextField(1));
    			}
    		}
     
    		JPanel panel2 = new JPanel(new BorderLayout());
    		panel2.add(panel1, BorderLayout.CENTER);
     
    		add(panel2, BorderLayout.CENTER);
    		add(new JButton("Reset"), BorderLayout.EAST);
    	}
     
    	public static void main(String[] args){
    		Sudoku1 frame = new Sudoku1();
    		frame.setTitle("Sudoku Puzzle");
    		frame.setSize(400, 400);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }

    What it returns is this jumbled mess, essentially a 3x30 grid with untouched grid line borders:


    If anyone could tell me where I went wrong in the original code and get me pointed in the right direction, I would greatly appreciate it. I figure it has to have something to do with the nested loop I implemented, but I cannot figure out what that might be.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    Start simple,
    cut your code back to have just a 3x3 grid. get this to work before moving on.
    then figure out how to put something in each 1 of those 9 grids. say a button that has a unique word or number

    crude drawing
    1 / 2 / 3 / /
    ---------- /
    4 / 5 / 6 / reset /
    ---------- /
    7 / 8 / 9 / /

    finally, instead of these numbered buttons, place a new 3x3 grid using the same method.
    If this doesn't work first add a JPanel, then add your 3x3 grid to the JPanel.

    hope this helps,
    Jonathan
    Last edited by JonLane; February 21st, 2012 at 05:40 PM.

  3. The Following User Says Thank You to JonLane For This Useful Post:

    bulx0001 (February 21st, 2012)

  4. #3
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    Thank you for the response Jonathan. I am actually able to get the 3 by 3 grid just find. It is figuring out how to loop 9 more 3 by 3 grids within each grid square that has me stumped, unfortunately.

  5. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    This works.
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class Sudoku1 extends JFrame {
    	JPanel panel1 = new JPanel();
    	Border exteriorBorder = new LineBorder(Color.BLACK, 3);
    	Border dividerBorder = new LineBorder(Color.BLACK, 2);
     
    	//constructor
    	public Sudoku1(){
    		for (int i = 1; i <= 9; i++){
    			panel1.setLayout(new GridLayout(3, 3));
    			panel1.setBorder(exteriorBorder);
    			panel1.add(groupPanelMaker());
    		}
     
    		JPanel panel2 = new JPanel(new BorderLayout());
    		panel2.add(panel1, BorderLayout.CENTER);
     
    		add(panel1);
    		add(new JButton("Reset"), BorderLayout.EAST);
    	}
     
    	//added a method to make inner groups of 9.
    	public JPanel groupPanelMaker()
    	{
    		JPanel inner9 = new JPanel();
    		for (int i = 1; i <= 9; i++){
    			inner9.setLayout(new GridLayout(3, 3));
    			inner9.setBorder(dividerBorder);
    			inner9.add(new JTextField(1));
    		}
    		return inner9;
    	}
     
    	public static void main(String[] args){
    		Sudoku1 frame = new Sudoku1();
    		frame.setTitle("Sudoku Puzzle");
    		frame.setSize(400, 400);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }
    So I could see what you were doing, and played around with it for a while:
    I tried creating the panels in reverse order, I tried dumbing it down to add panel 2 exactly twice.
    but could only get your panel3 to show up once.
    I came to the conclusion that it for some reason would not let you add the same object(in this case panel3) more then once.
    So a wrote a method in your class to make an entire panel group, and return that panel group.
    each panel group was added and it works and looks good.

    I also renamed your border variables and since my method needed to use variables,
    I took them out of your constuctor method and made them class varaibles.

    You should post your understanding of how my code works step by step if you intend to cut and paste it for credit. (to not do so will only hurt your understanding in later classes)

    Hear from you soon,
    Jonathan

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

    bulx0001 (February 21st, 2012)

  7. #5
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    Thanks so much man, reading your explanation and looking at your code helps tremendously.
    You essentially eliminated my nested loop and replaced it with a method that performed almost the same thing I was shooting for?
    Do you know exactly why it wouldn't let me add the aforementioned panel3 object in the original code that I posted? Is it a logical error on my part of some sort?

  8. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    I can't explain it either. However an interesting test let me try to break my method to get the same results as before the fix.

  9. The Following User Says Thank You to JonLane For This Useful Post:

    bulx0001 (February 21st, 2012)

  10. #7
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Seeking help with Grid/BorderLayout regarding Sudoku Puzzle program

    I have isolated the funny behavior, but still don't understand why it behaves this way.
    None the less I was right about it not accepting the same object to add twice.
    Below is identical code with 1 difference.

    ADDs JPanel2 to JPanel1 twice.
    import java.awt.*;
    import javax.swing.*;
     
    public class Sudoku2 extends JFrame {
    	JPanel panel1 = new JPanel();
    	JPanel panel2 = new JPanel();
    	JPanel panel3 = new JPanel();
     
    	public Sudoku2(){
    		panel2.setLayout(new GridLayout(3, 3));
    		panel2.add(new JTextField(1));
     
    		panel3.setLayout(new GridLayout(3, 3));
    		panel3.add(new JTextField(1));
     
     
    		panel1.setLayout(new GridLayout(3, 3));
    		panel1.add(panel2);
    		panel1.add(panel2); // ONLY CHANGE
     
    		add(panel1);
    	}
     
    	public static void main(String[] args){
    		Sudoku2 frame = new Sudoku2();
    		frame.setSize(400, 400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }

    Adds JPanel2 and JPanel3 to JPanel1
    import java.awt.*;
    import javax.swing.*;
     
    public class Sudoku2 extends JFrame {
    	JPanel panel1 = new JPanel();
    	JPanel panel2 = new JPanel();
    	JPanel panel3 = new JPanel();
     
    	public Sudoku2(){
    		panel2.setLayout(new GridLayout(3, 3));
    		panel2.add(new JTextField(1));
     
    		panel3.setLayout(new GridLayout(3, 3));
    		panel3.add(new JTextField(1));
     
     
    		panel1.setLayout(new GridLayout(3, 3));
    		panel1.add(panel2);
    		panel1.add(panel3); //ONLY CHANGE
     
    		add(panel1);
    	}
     
    	public static void main(String[] args){
    		Sudoku2 frame = new Sudoku2();
    		frame.setSize(400, 400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }
    Compile them both and you will see the behavior, unfortunately I cant explain it. great question for the teacher!

  11. The Following User Says Thank You to JonLane For This Useful Post:

    bulx0001 (February 21st, 2012)

Similar Threads

  1. Creating a Sudoku Program Assignment
    By xion0374 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2012, 07:48 PM
  2. I'm creating a sudoku program that is somehow in an infinite loops.
    By Leiferson in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2011, 03:21 AM
  3. [SOLVED] Compile a set of java files for a sudoku program
    By kanishk.dudeja in forum Java Theory & Questions
    Replies: 7
    Last Post: June 16th, 2011, 09:54 AM
  4. confused and seeking advice
    By mrgreaper in forum Java Theory & Questions
    Replies: 11
    Last Post: January 1st, 2011, 07:10 AM
  5. BackgroundPanel.java cannot be placed in BorderLayout.NORTH
    By chopficaro in forum AWT / Java Swing
    Replies: 7
    Last Post: August 31st, 2010, 06:27 PM