+ Reply to Thread
Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By chili5

Thread: Basic JTable and Netbeans

  1. #1
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    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 Attached Thumbnails Basic JTable and Netbeans-jtable.jpg   Basic JTable and Netbeans-model2.jpg   Basic JTable and Netbeans-model3.jpg   Basic JTable and Netbeans-model.jpg   Basic JTable and Netbeans-run.jpg  

    hannibal likes this.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Basic JTable and Netbeans

    Those tables look great! Very nice tutorial, +rep.

  4. #3
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: Basic JTable and Netbeans

    Very nice. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: Basic JTable and Netbeans

    GRREAT!
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  6. #5
    shahidr100 is offline Newbie
    Join Date
    May 2011
    Posts
    1
    Rep Power
    0

    Re: Basic JTable and Netbeans

    Hi,

    Does anyone know where is the 2nd part of this tutorial (Basic JTable and Netbeans )??

  7. #6
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    Re: Basic JTable and Netbeans

    Never got around to writing it... it's on my list. life kinda got in the way!

  8. #7
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Re: Basic JTable and Netbeans

    I am waiting for part two as well.

  9. #8
    programmer is offline Newbie
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Re: Basic JTable and Netbeans

    please provide the second part of this tutorial
    thank you..

  10. #9
    hbhosale12 is offline Newbie
    Join Date
    Jul 2011
    Location
    Pune,Maharashtra,India
    Posts
    5
    Rep Power
    0

    Re: Basic JTable and Netbeans

    how to add listener in jtable

  11. #10
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    Re: Basic JTable and Netbeans

    Code:
    public class TableListener implements TableModelListener {
        private JTable table = new JTable();
    
        public SimpleTableDemo() {
            table.getModel().addTableModelListener(this);
        }
        
        public void tableChanged(TableModelEvent e)  {
            // do stuff on table changed
        }
    }
    JTable

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 5 users browsing this thread. (0 members and 5 guests)

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2011, 11:22 AM
  2. Netbeans JTable and resultset
    By Timinator in forum Java Help
    Replies: 0
    Last Post: 01-25-2011, 04:58 PM
  3. jtable in java swing
    By k.t.h in forum Java Help
    Replies: 1
    Last Post: 07-16-2007, 01:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts