A recent project was developed by delphi, where data tables are used in many areas. This function can be easily implemented in delphi and vb. java does not
This function is provided, but can be implemented using Jtable, but it is a bit of trouble :).
The following is a simple data table program code that I implemented using applet.
Package com. applet. cat10;
Import java. awt .*;
Import java. awt. event .*;
Import java. applet .*;
Import javax. swing .*;
Import com. util .*;
Import java. util .*;
Import javax. swing. table .*;
Import java. SQL .*;
/**
* Title: Cat project
* Description: BCat
* Copyright: Copyright (c) 2001
* Company: smartcomm
* @ Author daniel
* @ Version 1.0
*/
Public class TestDatabase extends JApplet {
Boolean isStandalone = false;
JButton jButton1 = new JButton (); // trigger time query button
Database db = new Database ();
JTable table1 = new JTable ();
JScrollPane scroll = new JScrollPane ();
JTextField text1 = new JTextField ();
DefaultTableModel dtm; // defines the table data model.
Vector vCdata = null; // define the table column name (stored in vector)
ResultSet rsRow = null; // query the data set of the table.
/** Construct the applet */
Public TestDatabase (){
}
/** Initialize the applet */
Public void init (){
Try {
JbInit ();
UserInit (); // user-defined operations are in the Power Method
}
Catch (Exception e ){
E. printStackTrace ();
}
}
/** Component initialization */
Private void jbInit () throws Exception {
JButton1.setText ("jButton1 ");
JButton1.setBounds (new Rectangle (26,225, 79, 29 ));
JButton1.addActionListener (new java. awt. event. ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Jbutton?action=med (e );
}
});
This. setSize (new Dimension (664,300 ));
This. getContentPane (). setLayout (null );
Scroll. setBounds (new Rectangle (12, 24,644,189 ));
Text1.setBounds (new Rectangle (16,271,365, 22 ));
This. getContentPane (). add (scroll, null );
This. getContentPane (). add (jButton1, null );
// This. getContentPane (). add (text1, null );
Scroll. getViewport (). add (table1, null );
}
/* Initialize the userInit () data table */
Public void userInit ()
{
Db. connect (); // database connection
VCdata = db. getFieldNames ("T_REGISTRATION"); // get the column name (vector storage)
Dtm = new DefaultTableModel (); // defines the mode.
Table1.setModel (dtm); // sets the table mode.
/** For listing bad titles **/
For (int I = 0; I <vCdata. size (); I ++)
Dtm. addColumn (String) vCdata. elementAt (I ));
Rsrowmongodb.exe cuteQueryTable ("T_REGISTRATION"); // gets the data set
}
/** Start the applet */
Public void start (){
}
/** Stop the applet */
Public void stop (){
}
/** Destroy the applet */
Public void destroy (){
}
// Static initializer for setting look & feel
Static {
Try {
// UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName ());
// UIManager. setLookAndFeel (UIManager. getCrossPlatformLookAndFeelClassName ());
}
Catch (Exception e ){
}
}
// Trigger the query event
Void jbutton?action=med (ActionEvent e)
{
Try
{
While (rsRow. next ())
{
Vector vRdata = new Vector ();
For (int I = 0; I <vCdata. size (); I ++)
{
VRdata. addElement (rsRow. getString (String) vCdata. elementAt (I); // lists column data (in vector storage)
Storage)
}
Dtm. addRow (vRdata); // Add home data to the table
}
Db. close ();
} Catch (Exception ei)
{
System. out. println ("error at jbutton?action=med! In TestDatabase "+ ei. toString ());
}
}
}
This program implements the data table function. The initialization status is that the data table has only the title, and the data is displayed by clicking the button.
1. Initialize the data table in the userInit () method. The key method is as follows:
Create a table model: DefaultTableModel dtm = new DefaultTableModel (),
Set the table model: table1.setModel (dtm)
Set the table title: (all operations can be performed on the model) dtm. addColumn (Vector arg). arg is the table title, that is, the field name array.
Result set of the obtained data: rsrowmongodb.exe cuteQueryTable ("T_REGISTRATION"). This is the general data query result.
2. Key Methods:
Add data as rows: dtm. addRow (Vector arg ).
3. Jtable has many instantiation methods. Here the DefaultTableModel model is used, and table operations can be built on the model.
4. In the future, you can modify the data in the table in the model as follows:
Update three rows and four columns of data: dtm. setValueAt ("bigCat",), or table1.setValueAt ("bigCat ).
3 rows and 4 columns of data are obtained: Object rs = dtm. getValueAt (3, 4 );
Delete 3rd rows of data: dtm. removeRow (3)
You can modify the settings for the specific data table display mode.