Gradebook Program

I'm in the process of creating a java based gradebook application. I have my idea of how I want things to work, but I am unsure how I should do some of it. For example, I have the user inputing the class name, class size, and class period, but how should I have the user edit/add new grades or students? I looked into the JTables but that doesn't seem like it would completly work...

If you have any ideas on how to help me, please respond. I could use the help.

[477 byte] By [Little_Boberta] at [2007-10-2 11:48:58]
# 1
*bump*
Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 2
Why wouldn't JTables completely work?
tsitha at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 3
Well, because as I was looking through the API it didn't seem like it would be easy to add new columns (for the grades) and new rows (for the new students). I mean if they would work then I just need some help seeing how to do it.
Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 4

You should be able to use the JTable. For your application you should really be creating your own implementation of TableModel (or sub-class of AbstractTableModel or DefaultTableModel).You provide your own implementations of the column and row accessors such that they reflect the current values and number of rows and columns in your grade book:

int getColumnCount() {

// return the number of columns at any given time

}

int getRowCount() {

// return the number of rows at any given time.

}

Object getValueAt(int rowIndex, int columnIndex) {

// return the value at rowIndex, columnIndex at any given time.

}

Whenever you add a column or row be sure to notify the table. If you subclass AbstractTableModel you can do this by calling:

fireTableStructureChanged()

Likewise, anytime the data changes you can call:

fireTableDataChanged()

Or one of the more explicet fireXxxx methods

Add your model to the JTable with table.setModel or the JTable( model ) constructor.

mdnielsena at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 5
Thank you for your help. What should I do to add rows and columns? I mean I see that there is an addColumn() but there is no addRow() or equivilent. And as for the table model, when a new column is added how should I go about making sure the model changes with the correct column title?
Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 6
I take that back. I see the addRow and addColumn in the defaultTableModel.I would still like any help you guys might be able to add. So if you have 2 cents you would like to add, please feel free.
Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 7

Allright. I got the JTable to be created and shown. Unfortunitly, the titles of the columns do not appear. I use the DefaultTableModel constructor that takes in the (Object[] columnNames, int rowCount). I then use the JTable constructor and pass it the model I just made. I then add the JTable to the JFrame. It creates the right number of columns and rows, but the titles aren't there.

My code looks like this:

String[] columnNames = {"Names","Total Points","Possible Points","Grade"};

tabMod=new DefaultTableModel(columnNames,Integer.parseInt(classes.getSize()));

currentClass=new JTable(tabMod);

currentClass.setShowGrid(true);

con.add(currentClass);

repaint();

Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 8
Take a look at this tutorial from Sun: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
prometheuzza at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 9

I did look at that. I'm doing pretty much the same thing except instead of making my own model, I use the DefaultTableModel. I got the array of strings for the titles. And I call the right constructor. I looked at the code examples and I can't seem to find anything that tells me how to get the titles to appear.

Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 10
So, no idea anyone?
Little_Boberta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 11
The other alternative could be that you use JDBC with your program, and JTable to display, add, edit, delete stuff.
Jamwaa at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 12
When you were creating your own version of the TableModel, did you call super() in the importantt methods?Forexample if you are modifying the constructor, you should call super() in the firstline. ? Hope this helps.
SLKa at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 13

> I can't seem to find

> anything that tells me how to get the titles to

> appear.

Most easy way: wrap your JTable with a JScrollPane, that is instead of:

//parentContainer.add(myJTable, layoutConstraints);

doparentContainer.add( new JScrollPane(myJTable), layoutConstraints );

Less easy way: call getTableHeader() on the JTable. You get a JTableHeader instance back. That's a component. You can put in anywhere you want.

Background:

When initialized with a JTable, the JScrollPane calls configureEnclosingScrollPane() on the JTable, which method, in its turn, places its (the JTable's) TableHeader in the ColumnHeader of the ScrollPane.

Oh, and don't forget there's a dedicated [url http://forum.java.sun.com/forum.jspa?forumID=57]Swing Forum[/url].

da.futta at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 14

> When you were creating your own version of the

> TableModel, did you call

> super()

in the importantt methods?

>

> Forexample if you are modifying the constructor, you

> should call super() in the firstline. ? Hope this

> helps.

If he makes his own constructor (which is not "modifying the constructor"--it doesn't override the superclass constructor), then the no-argument "super()" constructor is called implicitly as the first line (if he doesn't explicitly call a different "super(something1, something2)" constructor or "this(...)" constructor. "super()" constructor without parameters never needs to be explicitly called.

MLRona at 2007-7-13 6:19:14 > top of Java-index,Java Essentials,Java Programming...
# 15
Thank you, everyone. You were a big help.da.futt, you solved my biggest problem, so I gave you the Duke Dollars, if you care. I may post again if I have anymore problems.
Little_Boberta at 2007-7-20 21:17:08 > top of Java-index,Java Essentials,Java Programming...
# 16
> da.futt, you solved my biggest problem, so I gave you> the Duke Dollars, if you care. I may post again if I> have anymore problems.Thanks a lot. Have fun coding.I/O, I/O,It's off to work we go ...
da.futta at 2007-7-20 21:17:08 > top of Java-index,Java Essentials,Java Programming...