Results 1 to 5 of 5
Thread: Swing GUI arrays
- 03-13-2012, 02:49 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Swing GUI arrays
Hello :)
I am writing a SuDoku problem solver and have got the basis of my code sorted.
However, I am struggling with making the GUI look good because at the moment I am just using a 9x9 array of text boxes that look like this:
Attachment 3240
I want it to look more like a normal sudoku with the 3x3 boxes separated within the 9x9 array.
My code for the SwingSudokuBoard is as follows:
public final class SwingSudokuBoard extends SudokuBoard {
private JTextField[][] cells; // Graphical game board in an array
private JPanel panel = new JPanel();
/**
* This method draws an empty board
* @param size - number of rows and columns
*/
public SwingSudokuBoard(int size) {
super(size);
cells = new JTextField[size][size];
panel.setLayout(new GridLayout(size, size));
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
cells[row][col] = new JTextField(1);
panel.add(cells[row][col]);
}
}
}
/**
* This method draws and initialises the Sudoku board
* @param board - array to initialise the contents
*/
public SwingSudokuBoard(int[][] board) {
this(board.length);
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
setCell(board[row][col], row, col);
}
}
}
/**
* This method puts a number into a specified cell
* @param num - number to put into the cell
* @param row - cell's row
* @param col - cell's column
*/
@Override
public void setCell(int num, int row, int col) {
super.setCell(num, row, col);
String text = (num == EMPTY) ? "" : String.valueOf(num);
cells[row][col].setText(text);
}
/**
* This method returns the number in a specified cell
* @param row - cell's row
* @param col - cell's column
* @return The number in the cell
*/
@Override
public int getCell(int row, int col) {
int cell;
try {
cell = Integer.parseInt(cells[row][col].getText());
}
catch (NumberFormatException e) {
cell = EMPTY;
}
return cell;
}
/**
* This method returns the JPanel which has the board
* @return Returns the board
*/
public JPanel getPanel() {
return panel;
}
}
Please can anyone help me with the arrays/GUI/swing components? I desparately need help!!
Thank you :)
xxx
- 03-13-2012, 02:56 PM #2
Re: Swing GUI arrays
You can nest layouts by putting a JPanel (or several JPanels) with one kind of layout inside another JPanel with a different (or the same) kind of layout. You can also use borders, etc to further separate the different pieces of your gui.
Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 03:02 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Re: Swing GUI arrays
Thank you for replying! However, do you know how I would go about nesting layout with my arrays? I'm not sure how/where to start!!
I would like to use JPanels as I have used them before, but I am struggling to get from what code I already have, to code where I can use JPanels.
Thanks
- 03-13-2012, 03:04 PM #4
Re: Swing GUI arrays
Well, think about how you want to set it up. Draw out the indexes of each cell and where you want that cell to be. Do you see a pattern?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-13-2012, 03:09 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
Re: Swing GUI arrays
Yes, I know there's a pattern, but with my code:
public SwingSudokuBoard(int[][] board) {
this(board.length);
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
setCell(board[row][col], row, col);
}
}
}
I am obviously just putting text boxes in a row when I am doing the setCell(board[row][col], row, col); method but I have no idea how to get from there to a JPanel where I can place the array boxes where I feel like.
Thank you
Similar Threads
-
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
map javax.swing.text.Element to javax.swing.text.View
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 07:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks