Tables in Java
Often times, what you need in a program is a way to display tabular data. Swing provides a JTable class but this can be rather tricky to work with.
Using the Netbeans IDE, I will show you a few things you can do with tables. In the Netbeans IDE create a new swing JFrame in the GUI creator. In the Palette you want to move a JTable to the frame.You will find the JTable under swing controls called table. Click on it and drag it to your form.
Resize your form so it looks like this:
First thing we want to do is click on the table and in the properties change the variable name. I'm going to call it tableCust.
We are going to display data in the table to represent customers.
In the first column, we are going to show a name. Then in the second column, a country and in the third column we are going to show how much money they have spent. We will change it so that the 4th column is not shown in a second.
One thing that is important, is the swing uses a TableModel to represent the data in the table. For our cases we don't need to create a table model but in some situations you do. Like you want to display pictures in the table, or use combo boxes or other weird things like that. Another thing that requires your own table model is the use of database information in a table. That is a topic for another day.
Changing the table model
In the properties window, you should see a field called Model near the top. There is a button with three ellipises right beside it. Click on that button.
You will see a form like this:
It has a tabbed pane with two tabs. The currently selected tab is for changing the columns in the table. The default values tab is used to set what values appear in the table.
We only want 3 columns so look at the bottom where it says columns and click the - button. The 4 columns will change to 3.
The first column is going to display a name, so double click on "Title 1" and change that to "Name". The type of data you want it to be is a String. When you click on Object under Type, a combo box will appear and you want to change this to String. Let us leave this field as editable.
This allows you to make changes in the table, and have those changes occur in the actual underlying data source.
The second column we want to display a country. This we will also change to a String just like above. However, this time we are going to make it so the field is not editable. In the second row, un check the check box.
Now, in column3 change the data as follows:
Title becomes Amount Spent
Type becomes double
Editable -> It is not editable.
Your form should now look like:
Now, all we have to do is add some data. In the current form, change the tab to default values.
Now let us add this data to the table:
Code:
James Canada 35.57
Bob USA 49.97
J.D. UK 39.99
Adding this data is as simple as typing in the text fields. So enter all the above data into the text fields and you will have a form that looks much like this:
The biggest problem with this is the TableModel uses a 2D Object array to represent the data in the table. Since an array is of fixed size, we must know exactly how much data we are going to have. We know what the second dimension will be (the number of columns) but we don't know the first dimension (the number of rows). We will look at this problem in the next tutorial.
When you run the program, you will see a table like this:
Double click on any row in the first column, and you will find that you can edit it. Notice, that you cannot edit data in the second and third fields?
The next tutorial on JTables will go into how to add and delete rows from the table. This is more tricky than it might appear. We will also look into formatting the items in the table so they look more how we want them. Granted I like to use a table model for everything even if it isn't necessary because it gives me more flexibility than this gui tool.
Netbeans provides a lot of GUI tools but I don't use much of them.
I will go into more detail on flexibility in the next tutorial. The nice thing is we did a lot of stuff and we didn't look at one line of code. The next tutorial there will be A LOT of code. More flexibility, means more code.