Use of JTable

Source: Internet
Author: User

    • The table has two parts: a row header (column header) and a Row object (Column object), where the row header is at the top of the line, representing the meaning of this data, and the row object is the content under the row heading. We can take advantage of the jtable provided by the Gettableheader () method to go to the row header
    • Put JTable in the Jscollpane, this practice can be said row header and line object complete display, because Jscollpane will automatically get the row header if you do not want to use ScrollPane
    • If you want to set the value of a column width, set it with the Setpreferedwidth () method provided by the TableColumn class, and use the Setautoresizemode () method provided by the JTable class to set the width of the other columns when a column is wide

    • If the data we want to display is not fixed or the data type is not fully displayed as a string type, we can use the table (javax.swing.table) required by the various model designs that swing provides.

TableModel

Abstracttablemodel

DefaultTableModel

TableModel itself is a interface, in this interface defined a number of methods, and because TableModel itself is a interface, so it is not very convenient to create a table between, so Java provides two classes to implement this interface, One is Abstracttablemodel abstract class and one is DefaultTableModel entity class. The relationship between three people:  implements extends Tablemodel-->abstracttablemodel-->defaulttablemodel

1.AbstractTableModel

* * Inherit this class we want to implement GetRowCount method, getColumnCount method, getValueAt method

* Several more commonly used methods:

Each row of the *jtable can be displayed in the following ways, depending on the data type:

1.Boolean: indicated by check box

2.Number: In JTable, the text is aligned to the right

3.ImageIcon: Graphic jlable representation, graphics placed in the center of the jlable

4.Object: string content for jlable display object, text to the left

2. DefaultTableModel

3. Tablecolumnmodel

Use JTable's Getcolumnmodel () method to get the Tablecolumnmodel object, and then use this object to set

The TableColumn class can be set specifically for each row in the table.

? How can I modify the contents of a table and save the modified values?

Replication setValueAt and Iscelleditor methods of Abstracttablemodel

Selectionmodel

To use ListSelectionModel, you can use the JTable Getselectionmodel method to get the ListSelectionMode object, Then use the Setselectionmodel () defined by the ListSelectionModel interface to set the selection mode

        Table.setcellselectionenabled (true);        SelectionMode=Table.getselectionmodel (); Selectionmode.setselectionmode (Listselectionmodel.single_ SELECTION);
JTable event handling (referred to as TableModelEvent event)

Handling TableModelEvent Events We have to implement the Tablemodellistener interface

1. How do I intercept event information?

Provide registration listener method in the Abstracttablemodel class: Addtablemodellistener ()

2. Implementing code in the Tablechanged method

Use of JTable

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.