Java notes jtable common usage rules

Source: Internet
Author: User

 

Original sitinspring. For more information, see author and source.

Jtable is a commonly used control in swing programming. Here we summarize some common methods for reference. You are welcome to add them. For more information, please indicate the author and source.

I. Various ways to create a table control:
1) Call the no-argument constructor.
Jtable table = new jtable ();

2) create a table with the header and table data.
Object [] [] celldata = {"row1-col1", "row1-col2"}, {"row2-col1", "row2-col2 "}};
String [] columnnames = {"col1", "col2 "};

Jtable table = new jtable (celldata, columnnames );

3) create a table with the table header and table data, and make the table cells unchangeable.
String [] headers = {"header 1", "header 2", "header 3 "};
Object [] [] celldata = NULL;

Defaulttablemodel model = new defaulttablemodel (celldata, headers ){

Public Boolean iscelleditable (INT row, int column ){
Return false;
}
};

Table = new jtable (model );

Ii. Control of Table G
1) Setting Columns cannot automatically adjust the width as the container component size changes.
Table. setautoresizemode (jtable. auto_resize_off );

2) limit the width of a column.
Tablecolumn firsetcolumn = table. getcolumnmodel (). getcolumn (0 );
Firsetcolumn. setpreferredwidth (30 );
Firsetcolumn. setmaxwidth (30 );
Firsetcolumn. setminwidth (30 );

3) set the current number of columns.
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();
Int COUNT = 5;
Tablemodel. setcolumncount (count );

4) obtain the number of table columns
Int Cols = table. getcolumncount ();

5) add columns
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();
Tablemodel. AddColumn ("new column name ");

6) delete a column
Table. removecolumn (table. getcolumnmodel (). getcolumn (columnindex); // columnindex is the sequence number of the column to be deleted.

3. control table rows
1) set the Row Height
Table. setrowheight (20 );

2) set the current flight count
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();
Int n = 5;
Tablemodel. setrowcount (N );

3) obtain the number of table rows
Int rows = table. getrowcount ();

4) add table rows
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();
Tablemodel. addrow (new object [] {"sitinspring", "35", "boss "});

5) Delete table rows
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();
Model. removerow (rowindex); // rowindex indicates the row number to be deleted.

4. Access Table cell data
1) Fetch cell data
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();
String cellvalue = (string) tablemodel. getvalueat (row, column); // you can call this operation to retrieve the cell data. row is the row number and column is the column number.

2) Fill in the data to the table.
Note: The data is a member linked list. The member class is as follows:
Public class member {
// Name
Private string name;

// Age
Private string age;

// Position
Private String title;
}

Data filling code:
Public void filltable (list <member> members ){
Defaulttablemodel tablemodel = (defaulttablemodel) Table
. GetModel ();
Tablemodel. setrowcount (0); // clear the original row

// Fill in data
For (Member member: Members ){
String [] arr = new string [3];
Arr [0] = member. getname ();
Arr [1] = member. getage ();
Arr [2] = member. gettitle ();

// Add data to the table
Tablemodel. addrow (ARR );
}

// Update the table
Table. invalidate ();
}

2) obtain the data in the table
Public list <member> getshowmembers (){
List <member> members = new arraylist <member> ();

Defaulttablemodel tablemodel = (defaulttablemodel) Table
. GetModel ();

Int rowcount = tablemodel. getrowcount ();

For (INT I = 0; I <rowcount; I ++ ){
Member member = new member ();

Member. setname (string) tablemodel. getvalueat (I, 0); // obtain the data in the first column of row I
Member. setage (string) tablemodel. getvalueat (I, 1); // obtain the data in the second column of row I
Member. settitle (string) tablemodel. getvalueat (I, 2); // obtain data in the third column of row I

Members. Add (member );
}

Return members;
}

5. Get the row selected by the user
1) obtain the row selected by the user
Int selectrows = table. getselectedrows (). length; // gets the number of rows selected by the user.
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();

If (selectrows = 1 ){
Int selectedrowindex = table. getselectedrow (); // retrieves the row selected by the user

. // Perform related processing
}

2) obtain the multiple rows selected by the user
Int selectrows = table. getselectedrows (). length; // gets the number of rows selected by the user.
Defaulttablemodel tablemodel = (defaulttablemodel) Table. GetModel ();

If (selectrows> 1)
Int [] selrowindexs = table. getselectedrows (); // sequence of the row selected by the user

For (INT I = 0; I <selrowindexs. length; I ++ ){
// Use tablemodel. getvalueat (row, column) to retrieve the cell data
String cellvalue = (string) tablemodel. getvalueat (I, 1 );
}
}

6. Add Table Event Processing
View. gettable (). addmouselistener (New mouselistener (){
Public void mousepressed (mouseevent e ){
// Processing when the mouse is pressed
}

Public void mousereleased (mouseevent e ){
// Processing when the mouse is released
}

Public void mouseentered (mouseevent e ){
// Processing when the cursor enters the table
}

Public void mouseexited (mouseevent e ){
// Processing when the cursor exits the table
}

Public void mouseclicked (mouseevent e ){
// Processing when the mouse clicks
}
});

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.