Java WinForm Development: JTable Detailed

Source: Internet
Author: User
Tags event listener header

Lists have a very important place in any development language, there are gridview in. NET, there are gridpanel in ExtJS ... , and in Java Swing, it's named JTable. These two days in the study of the use of jtable, there are some gains, so here to share with you, the following content will include: 1 basic usage of jtable; 2 How to add rows to JTable Click Response Events, double-click to open the window, 3 how to add a label for the JTable Row, such as row ID, 4) How to dynamically add new rows in JTable;

1) Basic usage of jtable:

I use the IDE is NetBeans, because it is also a simple to do a few pages of the client, mainly web development, so I do not intend to delve into the Java WinForm Development, so the interface how to do quickly, I directly in the form of "design" mode directly drag the "form" out of it, By default, this table will also have a few rows of columns filled with content, if you need to edit, you can click on the table to enter its editing state, and then right-click, select "Table content" can be used for table rows and columns of the edit, but usually, the contents of the table is dynamically generated, So it's not very meaningful to edit its content directly on the control, of course, it's more useful when doing demo.

How do I initialize the content for jtable? My habit is to construct the frame method to complete, in fact, through the search API can be learned that jtable support a variety of construction methods, and I think the dynamic generation of data, It is a simple and intuitive way to generate column header configuration and data configuration in the form of vector objects, and here's an example:

String[] Columnmodel = {"Time", "number", "Amount", "operator"};  
Vector cmvector = new vector ();  
for (int i = 0,cmcount = Columnmodel.length;i < cmcount;i++) {cmvector.addelement (columnmodel[i]);  
///Draw data Model vector datavector = new vector ();  
Temporderslist temporderlist = Tempordersmanager.getinstance (). GetList ();  
int billcount = Temporderlist.size (); if (Billcount > 0) {for (int j = 0;j < billcount;j++) {temporders OrderItem = (temporders) temporderlis  
        T.get (j);  
        String id = orderitem.getid ();  
        String time = Orderitem.gettime ();  
        String no = Orderitem.getno ();  
        Float total = Orderitem.gettotal ();  
        String user = Orderitem.getuser ();  
        Customtablecell Idcell = new Customtablecell (Id,no,orderitem);  
        Vector rowvector = new vector ();  
        Rowvector.addelement (time);  
        Rowvector.addelement (Idcell);  
        Rowvector.addelement (total);  
        Rowvector.addelement (user); DatavectoR.addelement (Rowvector);  
        Rowvector = null;  
        Idcell = null;  
    OrderItem = null; } DefaultTableModel TableModel = new DefaultTableModel (datavector,cmvector) {@Override public boolean Isce   
        lleditable (int row,int column) {//Allow users to edit data in column fourth only (fourth is quantity) if (column = = 4) {return true;  
        }else{return false;  
}  
    }  
};  
Billtable.setmodel (TableModel);  
Cmvector = null; Datavector = null;

When you use a vector object as an object to construct a method, you pass in an instance of two vectors, the first is the vector that holds the data, and the second parameter is the vector that holds the column header information,

The datavector in the example is populated dynamically by traversing a list object, and after instantiating and initializing the vector, you can display the column headers and data information in the table as long as the JTable Setmodel method is invoked.

2 How to add line click events

First, select the JTable in the JTable Design view, then right-click, select "Mouse-click" in the pop-up menu of the event to capture the Click event of the table, and in this event listener method, Will pass in an instance of Java.awt.event.MouseEvent class evt, with Evt.getclickcount (), you can know the number of clicks that triggered the event, as long as the return value of this method is greater than or equal to 2, which means that the user double-clicks, the following example code , for your reference:

if (Evt.getclickcount () >= 2) {  
            int rowIndex = Producttable.rowatpoint (Evt.getpoint ());  
            int columnindex = Common.getindetitycolumn (producttable);  
            If the marked column is not found, the If  
            (columnindex = 1) {return  
            }  
            is not executed down. Customtablecell Identifycell = (Customtablecell) producttable.getvalueat (RowIndex, columnindex);  
            String productId = Identifycell.getid ();  
            Productdetail productdetail = new Productdetail (productId);  
            Common.centerwindow (productdetail);  
            Productdetail.setvisible (true);  
        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.