Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-27-2009, 07:37 AM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Basic JTable and Netbeans

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.
Attached Thumbnails
basic-jtable-netbeans-jtable.jpg   basic-jtable-netbeans-model2.jpg   basic-jtable-netbeans-model3.jpg   basic-jtable-netbeans-model.jpg   basic-jtable-netbeans-run.jpg  

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-27-2009, 07:58 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Basic JTable and Netbeans

Those tables look great! Very nice tutorial, +rep.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-27-2009, 12:08 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Basic JTable and Netbeans

Very nice. +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-27-2009, 04:27 PM
MathX's Avatar
Guru
 
Join Date: Oct 2008
Location: Kosovo
Age: 19
Posts: 3,994
MathX has a spectacular aura aboutMathX has a spectacular aura about
Send a message via MSN to MathX
Re: Basic JTable and Netbeans

GRREAT!
__________________
My Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -5. The time now is 09:45 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0