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: Help With Sudoku GUI

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help With Sudoku GUI

    So here is what i have so far for my sudoku game. Now my grid only displays textboxes where a user can input numbers but nothing will happen yet. I want to know how to make it so that the user can only input one character and only numbers. i would also like to know how to make some textboxes jdialogs displaying uneditable numbers that im taking from a sudoku website for the puzzle. Any help would be appreciated. Thanks.


    Main Class
    import javax.swing.JOptionPane; 
     
    public class Game{ 
     
        public static void main(String[] args) { 
     
            JOptionPane.showMessageDialog(null, "Hello. Welcome to a game of Sudoku by Ezra Zike."); 
     
            Level select = new Level();  
     
        }    
    }
    Difficulty Select Class
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class Level extends JFrame {
     
        private JLabel tag1;
        private JButton butt1;
        private JButton butt2;
        private JButton butt3;
        private JButton butt4;
     
        public Level(){
     
            super("Difficulty");
            setLayout(new FlowLayout());
            setSize(250,130);
            setVisible(true);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            tag1 = new JLabel("Please Select Your Difficulty.");
            add(tag1);
     
            butt1 = new JButton("Easy.");
            butt1.setToolTipText("For players new to Sudoku.");
            add(butt1);
     
            butt2 = new JButton("Medium.");
            butt2.setToolTipText("Your abilites will be tested.");
            add(butt2);
     
            butt3 = new JButton("Hard.");
            butt3.setToolTipText("Your skills will be strained.");
            add(butt3);
     
            butt4 = new JButton("Evil!");
            butt4.setToolTipText("You will not survive.");
            add(butt4);
     
            thehandler handler = new thehandler();
            butt1.addActionListener(handler);
            butt2.addActionListener(handler);
            butt3.addActionListener(handler);
            butt4.addActionListener(handler);
     
        }
     
        private class thehandler implements ActionListener{
     
            public void actionPerformed(ActionEvent event){
     
                String string = "";
     
                if(event.getSource()==butt1){
                    dispose();
                    string = "Have fun!";
                    SudokuPanel Squares = new SudokuPanel();
                    }
                else if(event.getSource()==butt2){
                    dispose();
                    string = "Good luck!";
                    SudokuPanel Squares = new SudokuPanel();
                    }
                else if(event.getSource()==butt3){
                    dispose();
                    string = "Try to keep your head from exploding...";
                    SudokuPanel Squares = new SudokuPanel();
                    }
                else if(event.getSource()==butt4){
                    dispose();
                    string = "Please refrain from smashing any keyboards.";
                    SudokuPanel Squares = new SudokuPanel();
                    }
     
                JOptionPane.showMessageDialog(null, string);
            }
     
        }
     
    }

    Sudoku Panel Class
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
     
    public class SudokuPanel extends JFrame {
     
        public final int SQUARE_COUNT = 9;
        public Squares [] squares = new Squares[SQUARE_COUNT];
     
        public SudokuPanel(){
     
            super("Sudoku");
            setSize(600,600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
            JPanel panel = new JPanel(new GridLayout(3,3));
            for(int i=0; i<SQUARE_COUNT; i++){
                squares[i] = new Squares();
                panel.add(squares[i]);
            }
     
            JPanel panel2 = new JPanel();
            JButton startTime = new JButton();
            JButton stop = new JButton();
            JButton resetTimer = new JButton();
            MyTimer timer = new MyTimer();
     
            startTime = new JButton("Start Timer");
            stop = new JButton("Stop Timer");
            final JLabel timerLabel = new JLabel("   ...Waiting...   ");
            resetTimer = new JButton("Reset");
     
            JMenuBar menuBar = new JMenuBar();
            setJMenuBar(menuBar);
     
            JMenu menu = new JMenu("Menu");
            menuBar.add(menu);
     
            JMenuItem newDifficulty = new JMenuItem("Select New Difficulty");
            menu.add(newDifficulty);
     
            JMenuItem reset = new JMenuItem("Reset Puzzle");
            menu.add(reset);
     
            JMenuItem exit = new JMenuItem("Exit");
            menu.add(exit);
     
            exitaction e = new exitaction();
            newDifficultyaction d = new newDifficultyaction();
            resetaction f = new resetaction();
     
            reset.addActionListener(f);
            exit.addActionListener(e);
            newDifficulty.addActionListener(d);
     
            panel2.add(timer);
     
            add(panel, BorderLayout.CENTER);
            add(panel2, BorderLayout.PAGE_END);
     
            setVisible(true);
            setLocationRelativeTo(null);
     
        }
     
        public class exitaction implements ActionListener{
            public void actionPerformed (ActionEvent e){
                System.exit(0);
            }
        }
     
        public class newDifficultyaction implements ActionListener{
            public void actionPerformed (ActionEvent e){
                dispose();
                Level select = new Level(); 
            }
        }
     
        public class resetaction implements ActionListener{
            public void actionPerformed (ActionEvent e){
                dispose();
                SudokuPanel Squares = new SudokuPanel();
            }
        }
     
    }

    Squares/Cells class
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
     
    public class Squares extends JPanel {
     
        public final int CELL_COUNT = 9;
        public Cell [] cells = new Cell[CELL_COUNT];
     
        public Squares(){
            this.setLayout(new GridLayout(3,3));
            this.setBorder(new LineBorder(Color.BLACK,2));
            for(int i =0; i<CELL_COUNT; i++){
                cells[i] = new Cell();
                this.add(cells[i]);
            }
        }
     
        public class Cell extends JTextField{
     
            private int number;
            public Cell(){
     
            }
             public void setNumber(int number){
                 this.number = number;
                 this.setText("1");
             }
            public int getNumber(){
     
                return number;
            }
     
        }
     
    }
    Timer Class
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class MyTimer extends Panel {
     
     
        private JLabel timeDisplay;
        private JButton resetButton;
        private JButton startButton;
        private JButton stopButton;
        Timer timer;
     
        public MyTimer(){
     
     
            startButton = new JButton("Start Timer");
            stopButton = new JButton("Stop Timer");
            timeDisplay = new JLabel("...Waiting...");
     
     
     
            this.add(startButton);
            this.add(stopButton);
            this.add(timeDisplay);
     
            event e = new event();
            startButton.addActionListener(e);
     
            event1 c = new event1();
            stopButton.addActionListener(c);
     
     
     
        }
    Last edited by Ezra Zike; April 25th, 2014 at 10:30 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Sudoku GUI

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Ezra Zike (April 25th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Sudoku GUI

    Anyone?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Sudoku GUI

    some textboxes jdialogs displaying uneditable numbers
    text fields can be made uneditable by calling one of its methods.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Sudoku GUI

    i would like to know how to put numbers in certain boxes like Web Sudoku - Billions of Free Sudoku Puzzles to Play Online has, thats the part that has me really stumped right now

  7. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help With Setting Specific Numbers in Specific JTextFields

    Say i wanted to put a 2 in the upper left box in the upper left corner of that box like this http://tinypic.com/r/vrzpfq/8, how would i go about it. btw the numbers in the pictures are the ones i plan on using to fill my grid.


    Code for individual 3x3 cells
    import java.awt.*;
     
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.PlainDocument;
     
     
    public class Squares extends JPanel {
     
    	public final int CELL_COUNT = 9;
    	public Cell [] cells = new Cell[CELL_COUNT];
     
    	public Squares(){
    		this.setLayout(new GridLayout(3,3));
    		this.setBorder(new LineBorder(Color.BLACK,2));
    		for(int i = 0; i<CELL_COUNT; i++){
    			cells[i] = new Cell();
    			cells[i].setDocument(new NumericalDocument());
    			this.add(cells[i]);
    		}
    	}
     
    	public class Cell extends JTextField{
     
    		private int number;
    		public Cell(){
     
    		}
    		public void setNumber(int number){
    			this.number = number;
    			this.setText("5");
    		}
    		public int getNumber(){
     
    			return number;
    		}
     
    	}
     
    	public static class NumericalDocument extends PlainDocument{
    		String numbers = "0123456789";
    		public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
    			if (getLength() == 0 && str.length() == 1 && numbers.contains(str)) {
    				super.insertString(offs, str, a);
    			}
    			else {
    				Toolkit.getDefaultToolkit().beep();
    			}
    		}
    	}
    }

    Code for putting the 3x3 squares into a 3x3 grid making a 9x9 grid jtextfields

    import java.awt.GridLayout;
     
    import javax.swing.JPanel;
     
     
    public class Grid extends JPanel{
     
    	public final int SQUARE_COUNT = 9;
    	public Squares [] squares = new Squares[SQUARE_COUNT];
     
    	public Grid(){
    		for(int i=0; i<SQUARE_COUNT; i++){
    			squares[i] = new Squares();
    			this.add(squares[i]);
    		}
    	}
    }


    --- Update ---

    Please anyone? This is one of the last few things i need to be done, its had me stuck for a while and i only have one day left to turn this in.
    Last edited by Ezra Zike; April 26th, 2014 at 01:03 AM.

  8. #7
    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: Help With Sudoku GUI

    You can draw them on the object's graphics context using Graphics.drawString(), OR you can use the component's label (if it has one) to display the desired number. There may be other ways, but those are the obvious ones.

    Threads merged.

    Please don't post duplicate threads. It causes confusion and usually doesn't help.

Similar Threads

  1. Sudoku
    By Puleng in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2014, 03:50 AM
  2. Sudoku
    By davasile in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2013, 12:18 AM
  3. Help with turning Sudoku into Magic Sudoku
    By Murlio in forum Java Theory & Questions
    Replies: 1
    Last Post: November 4th, 2012, 02:49 PM
  4. Sudoku coding help...please
    By sketch_flygirl in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 5th, 2009, 10:07 PM
  5. Sudoku
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2009, 10:09 PM