Results 1 to 9 of 9
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Why wont my Gui update?

    im making a sudoku game and i am trying to use a swing worker to update the values in the grid. when i run my application can call the update method the Text in my Jlabel doesn't change at all.

    relevant code;

    Solve method isnt posted because it doesnt set any values, it used to calculate candidate values. Yes my check for definiates and set values work correctly



    board[i][j] is a 2d array of JLabel Objects which have a value assigned to them

    Java Code:
    public void run() {
    
    		class Solver extends SwingWorker<SudukoCell[][] , SudukoCell[][]> {
    
    			public SudukoCell[][] doInBackground() {
    				int x = 0;
    				int y = 0;
    				while(!solved){
    					solve();
    					sb.updateBoard();
    					//publish(sb.getBoard());
    					solved = true;
    
    				}
    				return null;
    			}
    			@Override
    			protected void process(List<SudukoCell[][]> board) {
    
    				sb.updateBoard();
    
    
    
    			}
    
    		}
    
    
    		Solver worker = new Solver();
    		worker.execute();
    	}



    Java Code:
    	public void solve() {
    
    
    		setCandiates();
    		checkForDef();
    		
    
    
    
    	}



    Java Code:
    public void checkForDefinates() {
    		if(candidates.size() == 1){
    			this.value = candidates.get(0);
    			candidates.remove(0);
    
    		}
    
    	}

    Java Code:
    public void updateBoard() {
    		int x = 0;
    		for(int i = 0; i < 9; i ++){
    			for(int j = 0; j < 9; j ++){
    				x = this.getGridSquareValue(i, j);
    				board[i][j].setTextValue(x);
    			
    			}
    
    		}
    	}


    board[i][j] set text method
    Java Code:
    public void setTextValue(int value){
    		this.value = value;
    		this.setText(Integer.toString(value));
    
    	}


    ive tried pretty much anything i can think of, struggling to find a way to update.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Why wont my Gui update?

    Can you make a small, complete program that compiles, executes and shows the problem? Don't post a large program that this is used in.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Why wont my Gui update?

    Java Code:
    Jpanel x = new Jpanel();
    x.setText("0");
    
    
    SudukoCell[1][2] = x;
    
    
    public void solve() {
    
    class Solver extends SwingWorker<SudukoCell[][] , SudukoCell[][]> {
     
                public SudukoCell[][] doInBackground(){
    
    updateBoard();
    
    }
    
    }
    Solver s = new solver();
    s.execute();
    
    }
    
    public voud updateBoard() {
    
    SudukoCell[1][2].setText("1");
    
    }

    This a simplier version of what my code does, this wont update the value of my gui to show that the JLabel should now display 1

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Why wont my Gui update?

    This is not a complete SSCCE as requested. To be of help we need a small, complete, compilable example that demonstrates the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Why wont my Gui update?

    its difficult to post a short version of it, want me to just post a zip of the source code or something? the values do get updated but they dont get displayed.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Why wont my Gui update?

    Strip out all the code not related to the problem or probably better: write a new small program that shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Why wont my Gui update?

    Java Code:
     
    package dcs.aber.ac.uk.suduko.application;
    
    import java.awt.*;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.Border;
    
    public class SudukoBoard extends JFrame {
    
    	private SudukoCell[][] board = new SudukoCell[9][9];
    
    	private JButton solve, reset, stop, open; 
    
    	private JPanel boardHolder, buttonHolder;
    
    
    
    
    	SudukoBoard() {
    
    		{
    			try {
    				SwingUtilities.invokeLater(
    						new Runnable(){
    							public void run() {
    								buildDisplay();
    							}
    						}
    						);  //end of invoke later parameters      
    			}
    			catch (Exception e) {
    				System.out.println("invokeLater exception "+e);
    			}
    		}
    	}
    
    	public void buildDisplay() {
    
    
    		super.setTitle("CHE16 SUDOKU VERSION 1.0");
    
    		super.setSize(500,500);
    		super.setLocationRelativeTo(null); //centers the frame
    		super.setVisible(true);
    		super.setResizable(false);
    		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    		boardHolder = new JPanel(new GridLayout(9,9));
    
    		
    		boardHolder.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    
    		buttonHolder = new JPanel(new FlowLayout( FlowLayout.CENTER, 20, 10));
    
    		solve = new JButton("solve");
    		reset = new JButton("reset");
    		stop = new JButton("stop");
    		open = new JButton("load");
    
    		buttonHolder.add(solve);
    		buttonHolder.add(reset);
    		buttonHolder.add(stop);
    		buttonHolder.add(open);
    
    
    		for(int i = 0; i < 9; i++) {
    			for(int j = 0; j < 9; j++){
    				board[i][j] = new SudukoCell();
    				boardHolder.add(board[i][j]);
    
    			}
    
    		}
    
    		this.add(buttonHolder, BorderLayout.SOUTH);
    		this.add(boardHolder);
    	}
    
    	public void setSquareValue(int x, int y, int i){
    		if(i < 0 || i > 9){
    			return;
    		}
    		board[x][y].setText(Integer.toString(i));
    	}
    
    	public int getBoardLength(){
    		return board[0].length;
    	}
    
    	public int getBoardHeight(){
    		return board.length;
    	}
    
    	public int getGridSquareValue(int i, int j){
    
    		/* row i, column j */
    		return Integer.parseInt(board[i][j].getText());
    
    	}
    
    
    	
    
    
    	public SudukoCell getGridSquare(int x, int y) {
    
    		return board[x][y];
    	}
    
    	public void setCandidatesForCell(int x, int y, ArrayList<Integer> canList) {
    
    		board[x][y].setCanidateList(canList);
    
    	}
    
    	public void printCellsCandidates(int x, int y){
    
    		board[x][y].printCandidates();;
    
    
    	}
    
    	public void updateCellValue(int row, int col, int value){
    
    		board[row][col].setValue(value);
    
    	}
    
    	public void updateBoard() {
    		int x = 0;
    		for(int i = 0; i < 9; i ++){
    			for(int j = 0; j < 9; j ++){
    				x = this.getGridSquareValue(i, j);
    				board[i][j].setTextValue(x);
    
    			}
    
    		}
    	}
    
    	
    
    	public void update(int row, int col, int value) {
    
    		board[row][col].setText(Integer.toString(value));
    
    	}
    
    
    	public ArrayList<Integer> getCanListFromCell(int i, int j) {
    		return board[i][j].getCanidates();
    
    	}
    
    	
    	public void setGridSquareValue(int i, int j, Integer v) {
    		board[i][j].setValue(v);
    
    	}
    
    	public void checkForDefs() {
    		for(int i =0; i < 9; i ++){
    			for(int j =0; j < 9; j ++){
    				board[i][j].checkForDefinates();
    
    
    			}
    
    		}
    
    	}
    
    
    
    }
    Java Code:
     
    
    package dcs.aber.ac.uk.suduko.application;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    import javax.swing.border.Border;
    
    
    public class SudukoCell extends JLabel {
    
    	private ArrayList<Integer> candidates;
    
    	private int value;
    
    	private Border stdBorder = BorderFactory.createLineBorder(Color.black, 3);	
    
    	private Font gameFont = new Font("Arial black", Font.BOLD, 24);
    
    	SudukoCell() {
    
    
    		super("", SwingConstants.CENTER);
    		this.setBorder(stdBorder);
    		this.setFont(gameFont);
    		this.setForeground(Color.BLACK);
    		this.setBackground(Color.WHITE);
    		this.setOpaque(true);
    		this.setVisible(true);
    		candidates = new ArrayList<Integer>();
    
    
    	}
    
    
    	public void setCanidateList(ArrayList<Integer> list){
    
    		this.candidates = list;
    
    	}
    
    
    	public ArrayList<Integer> getCanidates() {
    
    		return candidates;
    	}
    
    	public int getValue() {
    		return value;
    	}
    
    
    	public void setValue(int value) {
    		this.value = value;
    		this.setText(Integer.toString(this.value));
    	}
    
    
    	public void setTextValue(int value){
    		this.value = value;
    		this.setText(Integer.toString(value));
    
    	}
    
    	public void printCandidates() {
    
    		System.out.println(candidates);
    	}
    
    
    	public void checkForDefinates() {
    		if(candidates.size() == 1){
    			this.value = candidates.get(0);
    			candidates.remove(0);
    
    		}
    
    	}
    
    
    }
    Java Code:
     
    
    package dcs.aber.ac.uk.suduko.application;
    
    
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Scanner;
    
    import javax.swing.JFileChooser;
    import javax.swing.JLabel;
    import javax.swing.SwingWorker;
    
    
    public class SudukoMethods {
    
    	private SudukoBoard sb;
    
    	private static final JFileChooser jfc = new JFileChooser(System.getProperty("user.dir"));
    
    	private JLabel temp;
    
    	private ArrayList<Integer> candidates;
    
    	private boolean solved;
    
    	private boolean firstRun;
    
    
    
    
    	public SudukoMethods(SudukoBoard sb)  {
    		solved = false;
    		firstRun = true;
    		this.sb = sb;
    		temp = new JLabel();
    		candidates =  new ArrayList<Integer>();
    		fillList();
    		readFileAndSetGrid((seclectFile()));
    		run();
    
    
    	}
    
    
    	public void readFileAndSetGrid(File fileName) {
    
    		String[] lines = new String[9];
    		try {
    
    			Scanner scan = new Scanner(fileName);
    			for(int i = 0; i < 9; i ++){
    				lines[i] = scan.nextLine();
    
    				if((i < 9)){
    					for(int j = 0; j < 9; j ++){
    						lines[i].replaceAll("\\s", "0"); //used to track empty "grid squares" in the file
    						/*Return numerical value, otherwise deafult is asci value of the char */
    						int temp = Character.getNumericValue(lines[i].charAt(j));
    
    						if(temp < 0){
    							temp = 0;
    						}
    
    						sb.setSquareValue(i,j,temp);
    
    					}
    				}
    				else {
    
    					System.out.print("FILLLEEE ERRRROROROROROR LINES OR COLUMNS ABOVE 9");
    
    					/*HERE I COULD, GIVE USER A BLANK GRID
    					 * OR SELECTED A RANDOM GRID TO SOLVE
    					 */
    				}
    
    
    			}
    
    			scan.close();
    
    		}
    
    		catch ( IOException e) {
    
    			e.printStackTrace();
    
    			//PRINT A PROPER ERROR MESSAGE
    
    		}
    
    
    	}
    
    
    
    	public void printBoard(){
    
    		for(int i = 0; i < 9; i ++){
    			for(int j = 0; j< 9; j ++){
    				System.out.print(sb.getGridSquareValue(i, j));
    			}
    			System.out.println();
    		}
    
    	}
    
    	private File seclectFile() {
    
    		jfc.showOpenDialog(null);
    		return jfc.getSelectedFile();
    	}
    
    
    
    
    	public void solve() {
    
    
    		setCandiates();
    		checkForDef();
    
    
    
    
    	}
    
    
    
    
    	public void getCandidatesFromRow(int x) {
    
    		if (x < 0 || x > 8){
    			return;
    		}
    
    		for(int i = 0; i < sb.getBoardHeight(); i++){
    			temp = sb.getGridSquare(x, i);
    			int value = Integer.parseInt(temp.getText());
    			if(value !=0){
    				removeCandidate(value);
    			}
    		}
    
    	}
    
    	public void getCandidatesColumn(int x){
    		if (x < 0 || x > 8){
    			return;
    		}
    
    		for(int i = 0; i < sb.getBoardHeight(); i++){
    			temp = sb.getGridSquare(i, x);
    			int value = Integer.parseInt(temp.getText());
    			if(value !=0){
    				removeCandidate(value);
    			}
    		}
    
    
    	}
    
    	/* start of de-bug print lines */
    
    	public void testColumn(int x){
    		if (x < 0 || x > 8){
    			return;
    		}
    
    		for(int i = 0; i < sb.getBoardHeight(); i++){
    			temp = sb.getGridSquare(i, x);
    			int j =(Integer.parseInt(temp.getText()));
    			System.out.println(j);
    
    		}
    	}
    
    	public void testRow(int x){
    		if (x < 0 || x > 8){
    			return;
    		}
    		for(int i = 0; i < sb.getBoardHeight(); i++){
    			temp = sb.getGridSquare(x, i);
    			int j =(Integer.parseInt(temp.getText()));
    			System.out.print(j);
    
    		}
    		System.out.println();
    	}
    
    	/*end of debug print line */
    
    
    	public void fillList() {
    		if(candidates.size() < 9){
    			candidates.clear();
    		}
    
    		for(int i = 1; i < 10; i++ ){
    			candidates.add(i);
    		}
    	}
    
    	public void removeCandidate(int x){
    
    		for (Iterator<Integer> iter = candidates.iterator(); iter.hasNext();){
    			int i = iter.next();
    			if (x == i){
    				iter.remove();
    			}
    		}
    	}
    
    
    	public void getCandidatesFrom3x3Sub(int startR, int startC){
    
    
    		int value = 0;
    		for (int i = startR; i < (startR +3); i++){
    			for (int k = startC; k < (startC + 3); k ++){
    				/*
    				 * Reason + 3 is used is because the loop will stop at (startR + 3) -1, so effectively startC + 2
    				 * Which will give us the value we need 
    				 */
    				value = sb.getGridSquareValue(i, k);
    				if(value !=0){
    					removeCandidate(value);
    
    					//value already in 3x3			
    				}
    			}
    		}
    
    	}
    
    
    	public void setCandiates() {
    
    
    		for(int i = 0; i < 9; i++){
    			for(int j = 0; j < 9; j++){
    				if(!firstRun){
    
    					setCandidates(sb.getCanListFromCell(i, j));
    
    				}
    				if(sb.getGridSquareValue(i, j) == 0){
    					if(sb.getGridSquareValue(i, j) == 0){
    						/* messy if statements to check sub grids */
    
    						/* top left sub-grid  */
    						if(i < 3 && j < 3){
    
    							getCandidatesFrom3x3Sub(0,0);
    
    						}
    						else if(i < 3 && (j > 2 && j < 6)){
    
    							getCandidatesFrom3x3Sub(0,3);
    
    						}
    
    						else if(i < 3 && (j > 5 && j < 9)){
    
    
    							getCandidatesFrom3x3Sub(0,6);
    
    						}
    
    
    						else if((i > 2 && i < 6)   && (j < 3)){
    
    							getCandidatesFrom3x3Sub(3,0);
    
    
    						}
    
    						else if((i > 2 && i < 6)   && (j > 2 && j < 6)){
    
    							getCandidatesFrom3x3Sub(3,3);
    
    
    						}
    						else if((i > 2 && i < 6)   && (j > 5 && j < 9)){
    
    							getCandidatesFrom3x3Sub(3,6);
    
    
    						}
    						else if((i > 5 && i < 9)   && j < 3 ){
    
    							getCandidatesFrom3x3Sub(6,0);
    
    
    						}
    						else if((i > 5 && i < 9)   && (j > 2 && j < 6)){
    
    							getCandidatesFrom3x3Sub(6,3);
    
    
    						}
    						else if((i > 5 && i < 9)   && (j > 5 && j < 9) ){
    
    							getCandidatesFrom3x3Sub(6,6);
    
    
    						}
    
    
    						getCandidatesFromRow(i);
    						getCandidatesColumn(j);
    						sb.setCandidatesForCell(i,j, deepCopyArrayList(candidates)) ;
    						System.out.println("Printing candidates for cell : " + i + " " + j);
    						sb.printCellsCandidates(i, j);
    						if(firstRun){
    							fillList(); // don't want candidates being reset
    						}
    					}
    				}
    			}
    		}
    		firstRun = false;
    	}
    
    
    	public void setCandidates(ArrayList<Integer> canListFromCell) {
    		this.candidates = canListFromCell;
    
    	}
    
    
    	public void run() {
    
    		class Solver extends SwingWorker<SudukoCell[][] , SudukoCell[][]> {
    
    			public SudukoCell[][] doInBackground() {
    
    				while(!solved){
    					solve();
    					sb.updateBoard();
    
    					publish(sb.getBoard());
    					solved = true;
    
    				}
    				return null;
    			}
    			@Override
    			protected void process(List<SudukoCell[][]> board) {
    
    
    			}
    
    		}
    
    
    		Solver worker = new Solver();
    		worker.execute();
    	}
    
    
    	public ArrayList<Integer> deepCopyArrayList(ArrayList<Integer> list) {
    
    		/* This method is used for the purpose of passing the contenets
    		 *of the candidates list to each cell, without the problem associated with
    		 *shallow copies 
    		 *
    		 */
    
    		ArrayList<Integer> tempList = new ArrayList<Integer>(list.size());
    
    		for (Integer i : list){
    			tempList.add(i);
    		}
    		return tempList;
    	}
    
    	public void addHiddenSinglesConstraint() {
    
    		/*Hidden single: one candidate for a given row, column or box, but it is hidden among other candidates.
    		 * 
    		 * e.g. say we have 2 cell with the candiadtes:
    		 * cell 1 : (1,2,4,7)
    		 * cell 2: (1,2,3,4,7)
    		 * 
    		 * we can conclude that the value for cell 2 is 3 since its only cell with that candidate.
    		 * 
    		 * this must be tested against every cell in the sub grid though
    		 */
    
    
    
    	}
    
    
    	public void checkForDef(){
    		sb.checkForDefs();
    	}
    
    
    }

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default Re: Why wont my Gui update?

    You have a problem with a SwingWorker updating a JLabel.
    So you should be able to write some small code that shows a SwingWorker updating a JLabel and failing.
    In doing that you might actually start to spot the problem.

    There's far too much in the code you posted for us to actually see the problem.

    (It also doesn't help the forum messing up comments in your code, but that's not your fault).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Why wont my Gui update?

    Also the posted code is missing a main() method that is needed to execute the program.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Update Function won't update!
    By Indigrace in forum New To Java
    Replies: 3
    Last Post: 02-05-2013, 01:49 PM
  2. CachedRowSet wont update data source
    By diamonddragon in forum JDBC
    Replies: 5
    Last Post: 02-10-2012, 12:00 PM
  3. panel wont update when repaint is called
    By yemista in forum AWT / Swing
    Replies: 3
    Last Post: 10-25-2011, 06:58 PM
  4. this wont run again and again?? why?
    By vibaviattigala in forum New To Java
    Replies: 2
    Last Post: 07-16-2011, 08:30 AM
  5. .jar wont run?
    By stevenpalomino in forum New To Java
    Replies: 1
    Last Post: 06-30-2011, 10:11 AM

Posting Permissions

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