We help IT Professionals succeed at work.

Using JTable

dirku asked
Last Modified: 2013-11-23
I need some source code which does the following and so explains how it works:

Program description:
>> The JApplet is used to read from and write into an external database.
The ResultSet contains the table rows with the following columns:"portfolioname", "stockname", "stock_ID", "purchase_Date", "numberOfStocks", "purchasePrice" .
This ResultSet needs to be shown in a JTable within the JApplet.
After it is displayed the user may select a row and change the data. The changed row needs to be stored/updated.
After this work is done it must be stored into the database.

to consider:
1. The program should receive the data via JDBC from a database.
2. The ResultSet should be read in a 2-dimensional array: Object[ ][ ] dataModel;
The portfolioname and the stockname should be stored as Strings, the stock_ID as an Integer, the purchasePrice as a Float and the purchaseDate as a Date!
3. The classes Portfolio and Stock needs to exist:
public class Stock
private String name;
private Integer id;
private Float purchasePrice;
private Date purchaseDate;
}//end class Stock

public class Portfolio
private Vector allStocks;
private String name;
}//end class Portfolio

From the 2-dimensional array a new Portfolio instance must be created when the portfolioname occures the first time. Then the Stock must be created and added to the Portfolio.
If the portfolioname in the next row is the same only the Stock must be created and then added to the Portfolio object.
4. In the TableModel should be a Vector collecting all the Portfolio instances/objects. These portfolio objects should be used to generate the view of the table.
5. After displaying the table with those portfolios and stocks on the screen the user may select a row and change the data. For instance, he may increase the numberOfStocks from 20 to 50,...
These changes must take effect in the 2-dimensional array!!!
6. The user may delete or add any stock entries to a given portfolio or delete/add any portfolio. If a portfolio will be deleted all the containing stocks need to be deleted, too. These changes must take effect in the 2-dimensional array!!!
7. The up-to-date 2-dimensional array must be stored back into the database => the database needs to be updated.

(I want to extend teh TableModel from AbstractTableModel,
and I think I need a SelectionModel.)

Further question: When using Portfolio- and Stock objects -> is the 2-dimensional array needed?
Watch Question


Edited text of question
Unlock this solution and get a sample of our free trial.
(No credit card required)


I didn't go through the mentioned examples, but I'll do now.

But maybe you could give me another hint:
I want to implement as much components as possible as JavaBeans. Now I have a Portfolio class in my program and I want to implement it as a JavaBean. It is not a visual component but a dialog specific to make a new Portfolio object or alter the portfolios name exist. Is the component still a JavaBean when I put both the Portfolio.class and the NewPortfolioDialog.class file in a JAR file? So, are both classes together one JavaBean?
What do I have to do that a class/a Bean is a Bean?
For now I did the following:
- all propertiey/data fields are private
- these are accessible through public setter/getter methods

I intend to do:
- implementing a PortfolioEvent class; these events should be fired if a new Porfolio is created or the name is altered
(=> really needed that the class becomes a Bean?)

Is it essential to implement the following:
- a customizer or property editor
(=> really needed that the class becomes a Bean? How should I do this in the example above?)
- persistence
(=> I think this is really needed that the class becomes a Bean. How should I do this in the example above?)

I hope it's ok to ask you this in this 250 worth question?
After goin through the mentioned examples I'll grade you, okay?!


For trying out I presently us a MS Access database (locally). I think it's okay to try it this way? I mean, I simply have to change the driver, haven't I?
The functionality of the program may not be changed just because of the driver, does it?

But I think this way the mentioned example JDBCAdapter didn't run, does it?

Firstly, Note that as i told you for an applet, you need to have a three tier jdbc driver. The jdbc-odbc bridge is not a three tier driver but a two tier one. So, this will not work directly for testing unless you overcome the browser security restrictions using some test certificate and signing the applet.

A bean need not be visible. The Beanbox has some beans which are themselves invisible.

Note that the major difference between beans and ordinary java classes are that a bean is instantiated using the Beans.instantiate method and not thru new method. A bean is stored in a jar file along with meta info and other support classes. A bean can have customizer and property editor (while all this is optional). Internal functionality wise bot the java class and the bean are one and the same except we use some discipline while writing method names with beans. Beans use the support classes provided by beans package if necessary (not compulsory - only to avoid repetitive coding).

Its always better to make the beans members protected.

The JDBC Adapter will work with any program provided the driver name given is that of a three tier driver not a two tier one.

Note the difference between three and two tier drivers.

In two tier drivers, if a java program/applet invokes some of the driver methods like getConnection etc., it invokes the native method implementation to speak to the actual database. Since native methods usage is forbidden in an Applet, it cannot work unless is is signed. Also, even if it is signed , note that these methods are executed in the client browser machine not in the host from which the applet originated. This means, the database (if present) of the client is accessed and not yours.

In three tier drivers, all the driver methods forward the requests to a internet database server (like dbAnyWhere or oracle internet database server )residing on the host from which applet is originated and this doesnt require any native call. The server inturn may use the native methods. In this way, they operate correctly.

Go thru www.weblogic.com and their three tier drivers sample evaluation for more details.

You can create a portfolio bean and that inturn hides all the database stuff from the applet.


You need not write a customizer or property editor for your bean. All this is for third party tools which may manipulate your bean if you sell your bean to some third party company.


Sun says the JTable is already a JavaBean.
Which are the components of this Bean, so which classes are part of this Bean? Is the TableModel a part of this Bean?

I want to do program a JavaBean which includes the Bean JTable.
So I want to do the following:

public class MyTable extends JTable

public class MyTableModel extends AbstractTableModel
public MyTableModel(String driver, String db_url, String user, String password)


public class MyTableBean extends JPanel
private MyTable myTable;
private MyTableModel myTableModel;

public MyTableBean(String driver, String db_url, String user, String password)
myTable = new MyTable();
myTableModel = new MyTableModel(driver,db_url,user,password);

JScrollPane scrollpane = new JScrollPane(myTable);
}//end constructor
}//end class MyTableBean

I want to prevent the user from making the connection to the database separately so I simply want to create a MyTableBean instance and give it the driver, the url of the database, the user name, and the password. Then the MyTableModel class should do the rest after the query (SELECT-statement is given through myTableModel.setQuery(String todo);

The connection is set up (I can see this because of some system.out.println's) but then nothing is shown in the table.

Could you give me some source code?

You told me that a Bean is instantiated with the Bean.instantiate() method. How do I have to use this? I ever thought that -because JavaBeans are usual classes with private data memebers and accessor methods!- I have to create a new instance with the new operator. Do I have to implement an instantiate-method by myself?

Furthermore you told me that a Bean has to be stored in a JAR file. But I can't use a JAR-file from within my source code, can I? Let's look at the JTable class. Sun says it's a Bean but I have to implement it with JTable tyble = new JTable();
So, what did you want me to tell with it?

I need a lot of help now because time's running out for my final exam and I get panic!!!
I'd appreciate if you could help me some more ... I would be pleased to owe you all my points after I have completed implementation (currently 1150 available).



I forgot: In the MyTableBean there should be included a JDialog, too. It should be used to manipulate the data visible in the table. For instance the user should be able to alter the number of stocks he has purchased (since the table should be used for implementing a portfolio manager).

So this change has to be made:
public class MyTableBean extends JPanel
MyTableUI myTableUI;
MyTableModel myTableModel;
MyTableDialog myDialog;

public MyTableBean(Srting...)
myTableUI = new MyTableUI();
myTableModel = new MyTableModel(...);
myDialog = new MyTableDialog();
}//end class

Consider: I renamed the class MyTable to MyTableUI since it's the viewable part of the table which the user should interact with.

The parts of my Bean are therefore:
MyTableModel, and

Do you think it's a good idea?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.