JTable after using the code fragment

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A variety of ways to create a table control:

1) Call the parameterless constructor

JTable table = new JTable();

2) To create the table data table header

Object[][] cellData = {{"row1-col1", "row1-col2"},{"row2-col1", "row2-col2"}};
String[] columnNames = {"col1", "col2"};
JTable table = new JTable(cellData, columnNames);

3) To create the table data table headers, and let the table cell can not be changed

 String[] headers = { "A " header, " header;   two; "   " table three" };    Object[][] cellData = null;
 DefaultTableModel model = new DefaultTableModel(cellData, headers) {
 public boolean isCellEditable(int row, int column) {        return false;    }    };
 table = new JTable(model);

Two. The table column control
1) Set column can not automatically adjust the width varies with the size of container components

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

2) Limit one column width

TableColumn firsetColumn = table.getColumnModel().getColumn(0);
firsetColumn.setPreferredWidth(30);
firsetColumn.setMaxWidth(30);
firsetColumn.setMinWidth(30);

3) Set the current column number

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
int count=5;
tableModel.setColumnCount(count);

4) Make table columns

int cols = table.getColumnCount();

5) Add column

 DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
 tableModel.addColumn("New name");

6) Delete column

table.removeColumn(table.getColumnModel().getColumn(columnIndex));// ColumnIndex is the column that you want to delete

Three control of the table row
1) Line height

table.setRowHeight(20);

2) Set the current line number

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
int n=5;
tableModel.setRowCount(n);

3) Get number of table rows

int rows = table.getRowCount();

4) Adding a table row

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.addRow(new Object[]{"sitinspring", "35", "Boss"});

5) Delete rows in a table

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
model.removeRow(rowIndex);// RowIndex is the row to be deleted

Four access grid cell data
1) The cell data

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
String cellValue=(String) tableModel.getValueAt(row, column);// The cells of a data, row is the number of columns, column is No.

2) Filling data into the form

//Note: the data of Member type list, the Member class are as follows:
    public class Member{
        // Name
        private String name;
        // Age
        private String age;
        // Post
        private String title;
    }
    //Filling the data code:
    public void fillTable(List<Member> members){
    DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    tableModel.setRowCount(0);// Remove the original line
     // Filling data
    for(Member member:members){
        String[] arr=new String[3];
        arr[0]=member.getName();
        arr[1]=member.getAge();
        arr[2]=member.getTitle();
        // To add data to the table
        tableModel.addRow(arr);
    }
    // Update the table
    table.invalidate();
}

2) Obtain the data in tabular form
    

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));// Get the first column line I data
        member.setAge((String)tableModel.getValueAt(i, 1));// The I rows and second columns of data
        member.setTitle((String)tableModel.getValueAt(i, 2));// The I rows and third columns of data
        members.add(member);
    }
    return members;
}

Five. The user selected row
1) Get single user selected
   

int selectRows=table.getSelectedRows().length;// Achieve the user selected line number    
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    if(selectRows==1){        
    int selectedRowIndex = table.getSelectedRow(); // Get the user selected single line                  .//  correlation processing}

2) Multi line selected by the user
   

int selectRows=table.getSelectedRows().length;// Achieve the user selected line number    
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    if(selectRows>1){        
    int[] selRowIndexs=table.getSelectedRows();// Sequences of user selected row        
    for(int i=0;i<selRowIndexs.length;i++){            
    // Using tableModel.getValueAt (row,   column) the cell data            
    String cellValue=(String) tableModel.getValueAt(i, 1);        
    }    
   }

Six. Adding a table of event processing

view.getTable().addMouseListener(new MouseListener() {
    public void mousePressed(MouseEvent e) {    
        // Treatment of mouse down
    }
    public void mouseReleased(MouseEvent e) {
        // Treatment of mouse release}
    public void mouseEntered(MouseEvent e) {
        // Treatment of mouse into the form
    }public void mouseExited(MouseEvent e) {
       // Treatment of mouse exit
    }
    public void mouseClicked(MouseEvent e) {
      // Mouse clicks 
    }});


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Edmund at January 03, 2014 - 10:34 PM