Solved

Using JTable

Posted on 1998-07-01
8
463 Views
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?
0
Comment
Question by:dirku
  • 5
  • 3
8 Comments
 

Author Comment

by:dirku
ID: 1224281
Edited text of question
0
 
LVL 4

Accepted Solution

by:
evijay earned 250 total points
ID: 1224282
Firstly, If you want to use JDBC thru applet, you must have a 3 tier jdbc driver (one is available from Oracle - ask for database server . another option is Symantec DBAnywhere - There are also many other venders for that).

Did you go thru the TableExample2.java and JDBCAdapter.java example programs in the swing distribution examples\table directory? These are very close to your requirements. Only thing is these are applets and not applications.  The JDBCAdapter doesnt allow you to do updates (code is commented there - uncomment it and it works (line number 242)).
Regarding applet, it is very easy to convert that program to application and no jdbc calls need to be changed since these will be automatically handled by 3 tier jdbc driver.
See how the table model data is stored as vector of vectors instead of two dimensional arrays.

I think using these pointers you can implement the thing yourself. Ask me if you have any more doubts.


Vijay
0
 

Author Comment

by:dirku
ID: 1224283
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?!
0
 

Author Comment

by:dirku
ID: 1224284
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:evijay
ID: 1224285
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.

Vijay
0
 
LVL 4

Expert Comment

by:evijay
ID: 1224286
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.
0
 

Author Comment

by:dirku
ID: 1224287
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);

myTable.setModel(myTableModel);
JScrollPane scrollpane = new JScrollPane(myTable);
add(scrollpane);
}//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).

Dirk,
ulrich@tembit.com
0
 

Author Comment

by:dirku
ID: 1224288
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:
MyTableUI,
MyTableModel, and
MyTableDialog.

Do you think it's a good idea?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now