Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Grid in Java

As a way of learning Java, I'm writing this little application for grade schoolers to practice basic math. The idea is that the kid does any number of math problems, as long as they are in the app, it just continues to throw problems at them until they click a 'Done' button. When they decide to be done, I want a new JFrame to come up that will show them all of the problems they attempted, along with their answer, and whether they got the problem right or wrong.

The advice that I am looking for is what is the best way for me present these results. I looked into the GridLayout and the GroupLayout, but I don't think that these are exactly right. I did something similar in VBA for Excel, and there I just ran a for loop with one iteration for every problem they attempted. Each iteration would add a row of labels to the frame with the elements of the problem displayed in the various labels. I tried this in Java, but I'm not even able to get the labels to even display.

So before I get all specific and start posting my code, I want to ask a bigger question, which is "what is the best method to create a view like this?" Often, I go off in one direction only to waste time before somebody suggests a totally different (and better) approach.

Thanks!

(edit: here's an image of how I did this in Excel. I'm trying to repeat basically the same thing in Java)

enter image description here

like image 404
AndroidDev Avatar asked Jul 03 '12 03:07

AndroidDev


2 Answers

One simple way to make that design would be to use a mix of components. You could have a bunch of JLabels and JPanels stacked in a vertical FlowLayout. The grid you have described would be best designed in a JTable, something like the below:

example breakdown of components with gaudy colors

like image 172
akf Avatar answered Oct 08 '22 20:10

akf


If you like tables like Excel then, Java provides JTable class to create tables, if you want.

Tutorial : http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

like image 20
TeaCupApp Avatar answered Oct 08 '22 19:10

TeaCupApp