tags:

views:

652

answers:

6

Hey stackoverflow.

I am currently writing a sudoku solver in java. What i need now is some kind of swing gui to input the already known numbers. I created a jframe but manually adding 81 textfields from the toolbox seems like a bad solution. I am also able to add the with something like this:

    this.setLayout(new GridLayout(9, 9));

    for (int i = 0; i < 81; i++)
    {


        this.add(new JTextField("Field"));
    }

However I do not know how to address these textfields afterwards so I can collect the user input into a 2dimensional array.

All Help is very much appreciated.

+3  A: 

A different solution would be to use a JTable. You could allow for the TableModel to maintain the full data solution, as well as a copy of the user's attempts. The CellRenderers and CellEditors could handle the user experience. See this tutorial.

akf
thx, i will look into this.
ev00l
+1  A: 

JTable is your friend. Use a DefaultTableModel with editable String values.

String[] columnNames = new String[9];
for(int i=0; i<9; i++){columnNames[i]="";}
String[][] data = new String[9][9]; 
JTable tab = new JTable(columnNames,data);

When they fill it in, check that each string is an appropriate number and prompt for error.

BobMcGee
+2  A: 

Struggled a bit with this for my own sudoku solver, but ended up going for painting on a JPanel, and adding a mouse listener to that.. Than determine the current field using mouse position with his function:

 addMouseListener(new MouseAdapter() {
  private int t(int z) {
   return Math.min(z / factor, 8);
  };

  @Override
  public void mouseMoved(MouseEvent e) {
   setToolTipPossibilities(t(e.getX()), t(e.getY()));
  }

  @Override
  public void mousePressed(MouseEvent e) {
   clickColumn = t(e.getX());
   clickRow = t(e.getY());
  }
 });
Tim
I would add that with this solution you can use any nine images for your symbols.
Bill the Lizard
And as a further addition: I also used it to show some details about the solving algorithm; displaying the cursor and attempted values (and tooltips as you can see).
Tim
+2  A: 

First you need to declare array of JTextFields.

So just like your array to store user input you do:

private JTextField[] textFields;

After that you can use some math to map your one-dimensional array to your two dimensions. something like this should work:

floor(index / 9), index % 9

for x,y

Jan Gressmann
+1  A: 

1st way:

You could put the text fields into an array that mirrors the array that your cell values are in.

The problem with this method tho is that when you need to bind a mouseListener or ActionListener to the TextField you will have a hard time figuring out which cell number it corrisponds to.

2nd way:

You could extend the JTextField into a custom class with new instance variables that store cell number in it.

Using this method you can also implement MouseListener or ActionListener on the extended class too and get whatever information about the field you need, without searching through your array. And combining with the first to put them into an array organizes them for quick access.

Victor
Thanks alot for your input.
ev00l
+1  A: 

Just want to post a little update.

I added an array of textfields as a field on my form:

private JTextField[] fields;

Initialized it:

fields = new JTextField[81];

And finally I am adding the fields to my form like this:

    private void addComponents()
{
    this.setLayout(new GridLayout(9, 9));

    for (int i = 0; i < fields.length; i++)
    {
        fields[i] = new JTextField("" + i);
        this.add(fields[i]);
    }
}

The result as of now can be seen here: Image of my textfields

ev00l