Results 1 to 7 of 7

Thread: SudokuGrid

  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default SudokuGrid

    Hey. So I've made the program to solve a sudoku puzzle and now I wan to make it into a GUI. I've been reading a little bit of Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Anyhow, I was thinking of making every cell in a sudoku puzzle a JTextField. The problem is, the JTextField looks extremely small with a width of 1 in columns. I tried making the size of JTextField to 20 in width and height with one character visible at a time. Turns out, it doesn't work.

    Then I looked at JFormattedTextField, but I can't seem to find how to change its width and height so it looks like a bigger square. I would also like to change the font size of the number so it looks bigger.

    Thanks in advance.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    try changing the font on your JTextField

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    But could I stop a user from typing more than 1 number in a text field? When I used JFormattedText, I used a MaskFormatter to prevent that, but when I change the font in JFormattedText, the text area looks like a straight vertical line. What should I do?
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Use a Document Filter.

    e.g.,
    Java Code:
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.PlainDocument;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Interfata extends JFrame {
      private static final int CELL_POINTS = 20;
      private static final int SIDE = (int)((double)CELL_POINTS * 5 / 2);
      private static final Dimension CELL_SIZE = new Dimension(SIDE, SIDE);
      private static final Font CELL_FONT = new Font(Font.DIALOG, Font.BOLD, CELL_POINTS);
      public JTextField c[][] = new JTextField[9][];
      JButton solve = new JButton("solve");
      JButton clear = new JButton("clear");
      JButton exit = new JButton("exit");
      public int mat[][] = new int[9][9];
    
      public Interfata() {
        this.setTitle("Sudoku");
        exit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            System.exit(0);
          }
        });
    
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        solve.setSize(50, 50);
        clear.setSize(50, 50);
        exit.setSize(50, 50);
        JPanel panou_centru = new JPanel();
        JPanel panou_sud = new JPanel();
        Container panou = this.getContentPane();
        panou.setLayout(new BorderLayout(0, 10));
        int gap = 5;
        panou_centru.setLayout(new GridLayout(3, 3, gap, gap));
        panou_centru.setBorder(new EmptyBorder(gap, gap, gap, gap));
        panou_centru.setBackground(Color.black);
        
        JPanel[][] innerPanels = new JPanel[3][3];
        for (int i = 0; i < innerPanels.length; i++) {
          for (int j = 0; j < innerPanels[i].length; j++) {
            innerPanels[i][j] = new JPanel(new GridLayout(3, 3));
            panou_centru.add(innerPanels[i][j]);
          }
        }
            
        DocumentSizeFilter sizeFilter = new DocumentSizeFilter(1);
        for (int i = 0; i < 9; i++) {
          c[i] = new JTextField[9];
          for (int j = 0; j < 9; j++) {
            c[i][j] = new JTextField("", 3);
            ((PlainDocument)c[i][j].getDocument()).setDocumentFilter(sizeFilter);
            //panou_centru.add(c[i][j]);
            c[i][j].setPreferredSize(CELL_SIZE);
            innerPanels[i/3][j/3].add(c[i][j]);
            
            // TODO:  For testing purposes only.  Delete later
            c[i][j].setText("" + ((i + j) % 9 + 1)); // TODO: delete this
            c[i][j].setHorizontalAlignment(SwingConstants.CENTER);
            c[i][j].setFont(CELL_FONT);
          }
        }
        add("Center", panou_centru);
        panou_sud.setLayout(new FlowLayout());
        panou_sud.add(solve);
        panou_sud.add(clear);
        panou_sud.add(exit);
        add("South", panou_sud);
      }
    
      private static void createAndShowUI() {
        JFrame frame = new Interfata();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    
    class DocumentSizeFilter extends DocumentFilter {
      private static final String ALLOWED = "123456789";
      int maxCharacters;
      boolean DEBUG = false;
    
      public DocumentSizeFilter(int maxChars) {
          maxCharacters = maxChars;
      }
    
      public void insertString(FilterBypass fb, int offs,
                               String str, AttributeSet a)
          throws BadLocationException {
          if (DEBUG) {
              System.out.println("in DocumentSizeFilter's insertString method");
          }
    
          if ((fb.getDocument().getLength() + str.length()) <= maxCharacters && ALLOWED.contains(str))
              super.insertString(fb, offs, str, a);
          else
              Toolkit.getDefaultToolkit().beep();
      }
      
      public void replace(FilterBypass fb, int offs,
                          int length, 
                          String str, AttributeSet a)
          throws BadLocationException {
          if (DEBUG) {
              System.out.println("in DocumentSizeFilter's replace method");
          }
    
          if ((fb.getDocument().getLength() + str.length()
               - length) <= maxCharacters && ALLOWED.contains(str))
              super.replace(fb, offs, length, str, a);
          else
              Toolkit.getDefaultToolkit().beep();
      }
    
    }

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    lol wow that's amazing. just copied and pasted the code. Gonna try doing it myself without reading ur code cuz that will ruin the fun. (no offense)
    Last edited by Lil_Aziz1; 05-28-2010 at 04:26 AM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  7. #7
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    I started reading about Borders and I have couple of questions:

    Is inset the space needed for a border? Also, I started playing with your code a bit and when I changed panou_centru.setBorder(new EmptyBorder(gap, gap, gap, gap)); to panou_centru.setBorder(new EmptyBorder(gap, gap, gap+20, gap)); I notice the gaps inside the grid is not changed. Why?

    Also, I replaced the new EmptyBorder(..) to BorderFactory.createEmptyBorder(gap, gap, gap, gap). Is there any implicit difference between them two because I noticed nothing different. Is there convention to which one I should use?
    Last edited by Lil_Aziz1; 05-28-2010 at 10:23 PM.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •