Results 1 to 5 of 5
  1. #1
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Arrow GridLayout 1 column too wide

    Hello again,

    Stuck again I'm afraid. So I managed to display the gameboard and recognize what's in each position and set my labels accordingly. However, my gameboard doesn't get displayed right. Normally they're all 24x24 or 22x22 but from this little test class I saw it's 1 column too wide. I tried some variations on the GridLayout constructor but all in vain.

    Anyways, this is my SSCCE which has the same problem (luckily, now I at least know where it's at):

    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
    import javax.swing.SwingUtilities;
    
    
    public class Test extends javax.swing.JFrame {
    
    	/**
    	* Auto-generated main method to display this JFrame
    	*/
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				Test inst = new Test();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    			}
    		});
    	}
    	
    	private BoardPanel boardPanel;
    	
    	public Test() {
    		super();
    		initGUI();
    	}
    	
    	private void initGUI() {
    		try {
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			pack();
    			setSize(1180,800);
    			boardPanel = new BoardPanel();
    			getContentPane().add(boardPanel);
    			
    		} catch (Exception e) {
    		    //add your error handling code here
    			e.printStackTrace();
    		}
    	}
    public class BoardPanel extends JPanel{
    		
    		private JLabel lblFinish;
    		private LblEmpty lblEmpty;
    		private LblObst lblObst;
    		
    		public BoardPanel(){
    			super();
    			displayBoard();
    		}
    		private void displayBoard()
    		{
    			//String[][] spelbord=domeinController.startSpel(); // the real gameboard
    			String[][] spelbord = new String[22][22]; // for testing purposes
    			for(int row=0;row<spelbord.length;row++)
    			{
    				for(int col=0;col<spelbord.length;col++)
    					if(row%2==0){spelbord[row][col]= "  ";}
    					else spelbord[row][col]= " O"; // makes it easier to see when a new line begins
    			}
    			
    			GridLayout grid = new GridLayout(spelbord.length, spelbord.length); // Sets the rows and columns of the grid according to the row and colums of the array
    			// I tried spelbord[0].length -1 and spelbord.length -1 for the columns, didn't work 
    			System.out.println("Columns:" +grid.getColumns()); // for troubleshooting purposes
    			System.out.println("Rows:" +grid.getRows()); // for troubleshooting purposes
    			int w = grid.getColumns() * 30; // experiment, didn't work
    			int h = grid.getRows() * 30;
    			this.setSize(w, h);
    			this.setLayout(grid);
    			
    			for(int row=0;row<spelbord.length;row++)
    			{
    				for(int col=0;col<spelbord[0].length;col++)
    				{
    					if(row == spelbord.length -2 && col == spelbord.length -2){
    						lblFinish = new JLabel("F");
    						this.add(lblFinish);
    					}
    					if(spelbord[row][col].equals("  ")){
    						lblEmpty = new LblEmpty();
    						this.add(lblEmpty);
    					}
    					if(spelbord[row][col].equals(" O")){
    						lblObst = new LblObst();
    						this.add(lblObst);
    					}
    						
    				}
    			}
    		}
    		
    			private class LblEmpty extends JLabel{
    				public LblEmpty(){
    					super("e");
    					this.setBackground(getContentPane().getBackground());
    					this.setBorder(BorderFactory.createLineBorder(Color.white));
    				}
    			}
    			private class LblObst extends JLabel{
    				public LblObst(){
    					super("O");
    					this.setBackground(getContentPane().getBackground());
    					this.setBorder(BorderFactory.createLineBorder(Color.white));
    				}
    			}
    	}
    
    }
    If you need more info, please feel free to ask
    Last edited by Bloitz; 05-13-2011 at 10:33 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Bloitz View Post
    Java Code:
    			String[][] spelbord = new String[22][22]; // for testing purposes
    			GridLayout grid = new GridLayout(spelbord.length, spelbord.length-1); // Sets the rows and columns of the grid according to the row and colums of the array
    To me it looks like you should've done this instead:

    Java Code:
    GridLayout grid= new GridLayout(spelbord.length, spelbord.length);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default

    Thanks for the quick reply.

    I had it like that at first and it didn't work. I'll change my original post as I see it's an left-over from numerous changes.

  4. #4
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default

    I got my Test-version working. there was a double insert because the first if-statement was executed together with the others while the others should've been called only when the first one wasn't true. So there wasn't a column too many, there was a cell too many .

    Solution: Adding an else body behind the first if: (green)
    Java Code:
    private void displayBoard()
    		{
    			//String[][] spelbord=domeinController.startSpel(); // the real gameboard
    			String[][] spelbord = new String[24][24]; // for testing purposes
    			for(int row=0;row<spelbord.length;row++)
    			{
    				for(int col=0;col<spelbord.length;col++)
    					if(row%2==0){spelbord[row][col]= "  ";}
    					else spelbord[row][col]= " O";
    			}
    			
    			GridLayout grid = new GridLayout(spelbord.length, spelbord.length); // Sets the rows and columns of the grid according to the row and colums of the array
    			// I tried spelbord[0].length -1 and spelbord.length -1 for the columns, didn't work  
    			System.out.println("Columns:" +grid.getColumns()); // for troubleshooting purposes
    			System.out.println("Rows:" +grid.getRows()); // for troubleshooting purposes
    			int w = grid.getColumns() * 30; // experiment, didn't work
    			int h = grid.getRows() * 30;
    			this.setSize(w, h);
    			this.setLayout(grid);
    			
    			for(int row=0;row<spelbord.length;row++)
    			{
    				for(int col=0;col<spelbord[0].length;col++)
    				{
    					if(row == spelbord.length -2 && col == spelbord.length -2){
    						lblFinish = new JLabel("F");
    						this.add(lblFinish);
    					}
    					[COLOR="SeaGreen"]else 
    					{[/COLOR]
    						if(spelbord[row][col].equals("  ")){
    							lblEmpty = new LblEmpty();
    							this.add(lblEmpty);
    						}
    						if(spelbord[row][col].equals(" O")){
    							lblObst = new LblObst();
    							this.add(lblObst);
    						}
    					[COLOR="SeaGreen"]}[/COLOR]
    						
    				}
    			}
    Last edited by Bloitz; 05-13-2011 at 12:32 PM.

  5. #5
    Bloitz is offline Member
    Join Date
    May 2011
    Location
    Belgium
    Posts
    13
    Rep Power
    0

    Default

    I have been able to fix my real version. Basically my real gameboard had opponents on them and because I wanted to test if it displayed correctly I wanted to keep my code small and expand it when it worked. However this confused the loop because it didn't know what to do with the opponent boxes and didn't add empty labels (like I thought it would). Now I added the ifs for the opponents and it displays correctly. An else would've worked too but I had to add the opponents anyways

    Moral of the story: Make sure there's an else for your ifs or Java will go bonkers! (not really since it's a coding language and thus completely logical at all times, it's my imperfections that confuse it, not the other way around) ;)

    /solved
    Last edited by Bloitz; 05-13-2011 at 12:32 PM.

Similar Threads

  1. insert row and column and delete row and column
    By daredavil82 in forum New To Java
    Replies: 13
    Last Post: 09-22-2011, 06:10 PM
  2. Replies: 9
    Last Post: 02-15-2011, 01:13 AM
  3. A scope too wide?
    By JUser in forum Advanced Java
    Replies: 3
    Last Post: 09-28-2010, 08:38 AM
  4. Replies: 1
    Last Post: 07-06-2010, 04:50 PM
  5. Replies: 1
    Last Post: 06-07-2007, 05:06 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
  •