Results 1 to 2 of 2
  1. #1
    otterandbear is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    Default Creating 2D jbutton array with objects

    Hello! I'm trying to get a grid of JButtons to show up on a JFrame but they won't add. I'm using references because for each one there is a specific action and event that will occur between them. So, adding them to array and adding the array into the frame seem to be my biggest problems at the moment, Thank you!

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    
    public class Game {
    
    	// properties
    	protected JButton [][] board;  // do not change or remove this line
    
    	private JButton player;
    	private JButton openCell;
    	private JButton wall;
    	private JButton closedCell;
    
    	public Game(String [] args) {
    
    		this.player = player;
    		this.openCell = openCell;
    		this.wall = wall;
    		this.closedCell = closedCell;
    
    		String [][] layout;
    
    		String file = "initial.txt";
    		File f = new File(file);
    		Scanner in = null;
    		try {
    			in = new Scanner(f);
    		}
    		catch (FileNotFoundException ex) {
    			System.out.println("Can't find file " + file);
    			System.exit(1);
    		}
    
    		//Debug: System.out.println(in.next);
    		int boardSize = in.nextInt();
    		board = new JButton [boardSize][boardSize];
    		layout = new String [boardSize][boardSize];
    
    		for (int rows = 0; rows < boardSize; rows++) {
    			for (int cols = 0; cols < boardSize; cols++) {
    				in.hasNext();
    				// Debug: System.out.print(in.next());
    				layout[rows][cols] = in.next(); 
    			}
    		}
    
    		for (int rows = 0; rows < boardSize; rows++) {
    			for (int cols = 0; cols < boardSize; cols++) {
    				if (layout[rows][cols] == "*") {
    					board[rows][cols] = new JButton();
    					this.wall.setForeground(java.awt.Color.gray);
    				}
    				if (layout[rows][cols] == ".") {
    					board[rows][cols] = new JButton();
    					board[rows][cols] = this.openCell;
    					this.openCell.setForeground(java.awt.Color.green);
    				}
    				if (layout[rows][cols] == "x") {
    					board[rows][cols] = new JButton();
    					board[rows][cols] = this.player;
    					this.closedCell.setForeground(java.awt.Color.red);
    				}
    			}
    		}
    		
    		JFrame game = new JFrame();
    		game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.setLayout(new GridLayout (boardSize, boardSize));
    		game.setSize(500, 500);
    		
    		for (int i = 0; i < board.length; i++) {
    			for (int j = 0; j < layout.length; j++) {
    				game.add(board[i][j]);
    			}
    		}
    
    		game.setVisible(true);
    
    		//DEBUG
    		/*for (int i = 0; i < layout.length; i++) {
    			for (int j = 0; j < layout.length; j++) {
    				System.out.print(layout[i][j]);
    			}
    			System.out.println("");
    		}*/
    
    
    	} // end of constructor
    } // end of Game class

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

    Default Re: Creating 2D jbutton array with objects

    The most likely problem is you are comparing Strings with ==. You should only compare Strings and other objects with equals. The exception is whether you are checking for null or whether the references are the same.

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

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Error in writing JButton Array of objects into File
    By justbeller in forum AWT / Swing
    Replies: 3
    Last Post: 06-08-2011, 11:06 PM
  3. Creating Array of Objects
    By chathurajeewaka in forum New To Java
    Replies: 4
    Last Post: 12-03-2009, 03:23 PM
  4. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 01:25 AM
  5. Creating an array of nonprimitive objects
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM

Tags for this Thread

Posting Permissions

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