Write a 2d array to JTextArea?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6809 Views - Last Post: 31 October 2010 - 09:45 PM Rate Topic: -----

#1 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Write a 2d array to JTextArea?

Posted 31 October 2010 - 05:37 PM

Is it possible to send the output to a variable of type 2d array to a JTextArea or other swing functions?
Is This A Good Question/Topic? 0
  • +

Replies To: Write a 2d array to JTextArea?

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 05:57 PM

Yes.

:)

See JTextArea
Was This Post Helpful? 0
  • +
  • -

#3 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 06:03 PM

Ahh hello again :) what would I call to print it out to the area.
I have the grid in a separate class from where I add all buttons so im guessing I would
private Sudoku grid[][]; at the top to reference it?

EDIT- in the class I also call a print() method which prints it to the terminal, so I'm guessing a viable option would be to just call that method and output it to the JTextArea?

This post has been edited by Wolfy200222: 31 October 2010 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 06:33 PM

Quote

what would I call to print it out to the area.


Use the append() method write lines of text to the text area.

RE: edit: yes,assuming the text area is in scope there.
Was This Post Helpful? 0
  • +
  • -

#5 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 06:43 PM

Uhh I'm a bit confused, the append method only works with strings I'm guessing where, the grid is a 2d array. I have all methods in a class called Sudoku, and call them from a class called Functions where I have all my buttons and gui things and call methods from the Sudoku class, I have tried the append method before but not sure if I'm using it right. I've created JTextArea big enough to hold the grid but everytime I try to use the append method I get errors or it doesnt print out to the JTextArea, it prints to the terminal.

Im not sure if this is right but I reference the grid in the other class using; private Sudoku grid[][];

then under the actionperformed method for open include; text.append(grid[][])

This post has been edited by Wolfy200222: 31 October 2010 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 07:40 PM

sorry to push but I really want this done soon because Ive spent so much time on it and not got very far. How would I append a print method from another class to the textarea?

EDIT - is append the right method seeming its a 2d array of ints?

This post has been edited by Wolfy200222: 31 October 2010 - 07:40 PM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 07:49 PM

Beats me. :)

post your code.
Was This Post Helpful? 0
  • +
  • -

#8 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 07:54 PM

Sudoku

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

//import javax.swing.JFileChooser;
import javax.swing.JFrame;



public class Sudoku extends JFrame{

	private int[][] grid;	
	private int[] options;
	
	
	public Sudoku () {		
        grid = new int[9][9];
}    
	 public void load(String fileName)  throws IOException
	    {  try {
            File myFile = new File(fileName);
            FileReader fileReader = new FileReader(myFile);
            BufferedReader reader = new BufferedReader(fileReader);
            
            String line = " "; 
            
            int j = 0;            
            
            while  ((line = reader.readLine()) != null) {
                    for (int i = 0; i<9 && j< 9; ++i) {
                            if (line.charAt(i) == ' ') {
                            grid[j][i] = 0;
                            } else {
                            grid[j][i] = line.charAt(i)-'0';
                            }System.out.print("|");
                            System.out.print(grid[j][i]);
                           
                    }
                    j++;
                    System.out.println();
                    
            }
            reader.close();
            
    } catch (IOException e) {
		System.out.println("Failed");
    }
    }
	public void solve() {
         int complete = 0;
         do {
                 complete = 0;
                 for(int i = 0; i<3; ++i){
                         for(int j = 0; j<3; ++j){  
                                 options = new int[9];
                                 int x = 0;
                                 int valuesLength;
                                 for(int l = i*3; l<(i+1)*3; ++l){
                                         for(int m = j*3; m<(j+1)*3; ++m){
                                                 if (grid[l][m] != 0)
                                                         options[x++] = grid[l][m];
                                         }
                                 }
                                 valuesLength = x;
                                 
                                 x = 0;
                                 int[] candidates = new int[9];
                                 int candidatesLength = 0;
                                 for(int o=1; o<10; ++o){
                                         boolean flag = false;
                                         for(int op=0; op<valuesLength; op++){
                                                 if (o == options[op]) {
                                                         flag = true;
                                                         break;
                                                 }
                                         }
                                         if (flag == false)
                                                 candidates[x++] = o;
                                         
                                 }
                                 
                                 candidatesLength = x;
                                 
                                 for (x = 0; x < candidatesLength; x++) {
                                         int curr_candidate = candidates[x];
                                         int count = 0;
                                         int lx = 0, ly = 0;
                                         
                                         for(int l = i*3; l<(i+1)*3; ++l){
                                                 for(int m = j*3; m<(j+1)*3; ++m){                                                        
                                                         if (grid[l][m] == 0)  {
                                                                                                                         
                                                                 boolean flag = true;
                                                                 //check in row
                                                                 for (int k = 0; k < 9 && flag; k++) {
                                                                         if (grid[l][k] == curr_candidate) {
                                                                                 flag = false;
                                                                         }
                                                                 }
                                                                 // not in row,
                                                                 //check in col
                                                                 for (int k = 0; k < 9 && flag; k++) {
                                                                         if (grid[k][m] == curr_candidate) {
                                                                                 flag = false;
                                                                         }
                                                                 }
                                                                 
                                                                 if (flag) {
                                                                         ++count;
                                                                         lx = l;
                                                                         ly = m;
                                                                 }
                                                         }
                                                                 
                                                         
                                                 }
                                         }
                                         if (count == 1) {
                                                 grid[lx][ly] =  curr_candidate;
                                                 ++complete;
                                         }
                                 }
                         }        
                 }
        
    
        }  while(complete != 0);      
}

									 
                                
   /*     To implement later
	public void fileChooser(){
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.showOpenDialog(null);
	    fileChooser.setDialogTitle("Choose a file");
	    this.getContentPane().add(fileChooser);
	    fileChooser.setVisible(true);
	  }
	*/
	 public void printSudoku() {
         
         int i, j;
         
         System.out.println("\n");
         for (i = 0; i<9; ++i) {System.out.print("|");
                 for (j = 0; j<9; ++j) {
                	 
                         System.out.print(grid[i][j] + "|");
                 }
                 System.out.println();        
         }
         
 }
	
}



Function


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;



public class Functions extends JFrame implements ActionListener{

	private JButton open, solve;
	private Sudoku sudoku;
	private JTextArea text;

	
	public Functions() {
		super("Sudoku Solver by dng9");
		sudoku = new Sudoku();
        FlowLayout flowLay = new FlowLayout();
        this.setLayout(flowLay);
              
        JTextArea text = new JTextArea();
        text.setPreferredSize(new Dimension(100, 150));
        text.setEditable(false);
        add(text);
		open = new JButton("Open");
		add(open);
		open.addActionListener(this);
		solve = new JButton("Solve");
		add(solve);
		solve.addActionListener(this);

		
	}
	
	
	



	public void actionPerformed(ActionEvent event) {
		
		String actionCommand = event.getActionCommand();
		
		if (actionCommand.equals("Open")) {
		
			
					
					try {
						sudoku.load("web.sud");
						
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 
		}
		
		if (actionCommand.equals("Solve")){
			sudoku.solve();
			sudoku.printSudoku();
		}	
	}
}



Main

import java.io.IOException;

import javax.swing.JFrame;


class MainModel extends JFrame{
	
	public MainModel() {

		Functions func = new Functions();
	    func.setSize(275,200);
	    func.setVisible(true);
	}
	
public static void main(String args[]) throws IOException{
    MainModel app=new MainModel();
 

}

}



I would like to get the output of the load()method into the JTextArea, then either print alongide by making the JTextArea bigger or over-ride the text there with the output of the solve() method

This post has been edited by Wolfy200222: 31 October 2010 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 08:04 PM

Assumning your JTextArea text and your grid[][] are accessible in the same method
text.clear();
for(int i = 0; i < grid.length; i++) {
   for(int j = 0; j < grid[i].length; j++) {
     text.append(" " + grid[i][j];
   }
   text.append("\n");
}



or have a look at my multiple Sudoku tutorial starting by this one
http://www.dreaminco...-i-basic-tools/
Was This Post Helpful? 1
  • +
  • -

#10 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 08:06 PM

I think one option would be to convert your output into a 2d array of strings using the toString() method for each element of grid, and using the same for loop I imagine you could simply append to the JTextArea for each new String element. 2D arrays are a real pain the butt..

(I can't say with 100% confidence, as I've never played sudoku so I'm not entirely sure what it is your output is supposed to look like)

This post has been edited by Deviara: 31 October 2010 - 08:10 PM

Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 08:11 PM

View PostDeviara, on 31 October 2010 - 09:06 PM, said:

I think one option would be to convert your output into a 2d array of strings using the toString() method for each element of grid, and using the same for loop I imagine you could simply append to the JTextArea for each new String element. 2D arrays are a real pain the butt..

text.append("" + intValue); :)
Was This Post Helpful? 1
  • +
  • -

#12 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 08:18 PM

thanks to both for replying, I tried implementing your code pbl into my classes but without success (though I think that might be how Im implementing it myself) I have checked out your sudoku tutorials and have found them helpful and an interesting and informative method of doing so.

To clear confusion am I meant to implement your code section in my functions class where my textarea is or make a textarea reference in my Sudoku class and call the method under an actionperformed button?

EDIT - I'm also asked to add a cast to the text.clear() bit that takes type Object.

This post has been edited by Wolfy200222: 31 October 2010 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#13 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 09:06 PM

Ok I've modified the printSudoku() method to use a StringBuilder to construct an output string. I print it and return it to the caller:


public String printSudoku() {

    StringBuilder sb = new StringBuilder();

    int i, j;

    sb.append("\n");

    for (i = 0; i < 9; ++i) {
        sb.append("|");
        for (j = 0; j < 9; ++j) {
            sb.append(grid[i][j]);
            sb.append("|");
        }
        sb.append("\n");
    }
    System.out.println(sb.toString());

    return sb.toString();
}



The caller is an ActionListener:

...
solve = new JButton("Solve");
add(solve);
solve.addActionListener(new solveSudoku(text));
...



And solveSudoku() is defined as:

class solveSudoku implements ActionListener {
    JTextArea myTextArea;

    public solveSudoku(JTextArea ta) {
        myTextArea = ta;
    }

    public void actionPerformed(ActionEvent e) {
        String text;

        sudoku.solve();

        text = sudoku.printSudoku();

        myTextArea.append(text);
    }
}



Output to console:

Quote

run:
|0|6|0|1|0|4|0|5|0
|0|0|8|3|0|5|6|0|0
|2|0|0|0|0|0|0|0|1
|8|0|0|4|0|7|0|0|6
|0|0|6|0|0|0|3|0|0
|7|0|0|9|0|1|0|0|4
|5|0|0|0|0|0|0|0|2
|0|0|7|2|0|6|9|0|0
|0|4|0|5|0|8|0|7|0


|9|6|3|1|7|4|2|5|8|
|1|7|8|3|2|5|6|4|9|
|2|5|4|6|8|9|7|3|1|
|8|2|1|4|3|7|5|9|6|
|4|9|6|8|5|2|3|1|7|
|7|3|5|9|6|1|8|2|4|
|5|8|9|7|1|3|4|6|2|
|3|1|7|2|4|6|9|8|5|
|6|4|2|5|9|8|1|7|3|

BUILD STOPPED (total time: 8 seconds)

Was This Post Helpful? 2
  • +
  • -

#14 Wolfy200222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 24-October 10

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 09:24 PM

Thanks for your help! Really appreciated, but I get a Null pointer around the code of
sudoku.solve();
in the solveSudoku() class you provided =/

Edit - Nevermind I've got it to compile and run and solve, however it still gives the error after solve and doesnt print to JTextArea - Null pointer around myTextArea.append(text);

This post has been edited by Wolfy200222: 31 October 2010 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#15 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Write a 2d array to JTextArea?

Posted 31 October 2010 - 09:41 PM

I made no changes to the Sudoku class other than the print routine I previously posted. I made no changes to MainModel.

Here's my functions class:

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class Functions extends JFrame implements ActionListener {

    private JButton open, solve;
    private Sudoku sudoku;
    private JTextArea text;

    public Functions() {
        super("Sudoku Solver by dng9");
        sudoku = new Sudoku();
        FlowLayout flowLay = new FlowLayout();
        this.setLayout(flowLay);

        JTextArea text = new JTextArea();
        text.setPreferredSize(new Dimension(100, 150));
        text.setEditable(false);
        add(text);
        open = new JButton("Open");
        add(open);
        open.addActionListener(this);
        solve = new JButton("Solve");
        add(solve);
        solve.addActionListener(new solveSudoku(text));
    }

    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();

        if (actionCommand.equals("Open")) {
            try {
                sudoku.load("web.sud");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    class solveSudoku implements ActionListener {
        JTextArea myTextArea;

        public solveSudoku(JTextArea ta) {
            myTextArea = ta;
        }

        public void actionPerformed(ActionEvent e) {
            String text;

            sudoku.solve();

            text = sudoku.printSudoku();

            myTextArea.append(text);
        }
    }
}


This post has been edited by n8wxs: 31 October 2010 - 09:44 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2