Results 1 to 5 of 5
Thread: GridLayout 1 column too wide
- 05-13-2011, 11:08 AM #1
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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)); } } } }
Last edited by Bloitz; 05-13-2011 at 11:33 AM.
- 05-13-2011, 11:18 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-13-2011, 11:33 AM #3
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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.
- 05-13-2011, 12:55 PM #4
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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 01:32 PM.
- 05-13-2011, 01:27 PM #5
Member
- Join Date
- May 2011
- Location
- Belgium
- Posts
- 13
- Rep Power
- 0
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) ;)
/solvedLast edited by Bloitz; 05-13-2011 at 01:32 PM.
Similar Threads
-
How to write column by column to text file
By trkece in forum JDBCReplies: 11Last Post: 10-21-2014, 05:46 AM -
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 07:10 PM -
A scope too wide?
By JUser in forum Advanced JavaReplies: 3Last Post: 09-28-2010, 09:38 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 05:50 PM -
Struts, Some API to obtain wide and the high one of an image?
By Eric in forum Web FrameworksReplies: 1Last Post: 06-07-2007, 06:06 AM
Bookmarks