Swing JTable dynamic additions and deletions described operation ranks (detail)

Swing JTable is very common control programming, here we summarize some common methods for inspection.

A variety of ways to create a table of control:
1) no-argument constructor call.
JTable = Table new new JTable ();
2) in Table header and data to create a table.
Object [] [] CellData = {{ "ROW1-col1", "ROW1-col2"}, { "ROW2-col1", "ROW2-col2"}};
String [] the columnNames = { "col1", "col2"};
   
the JTable table = new new the JTable (CellData, the columnNames);
. 3) in a header and data to create a table, and so that the table cell is not changed.
String [] headers = { "header a" "header two", "header three"};
Object [] [] CellData = null;

DefaultTableModel Model = new new DefaultTableModel (CellData, headers) {
  public Boolean the isCellEditable (int Row, int column) {
    return to false;
  }
} ;

 

Table new new = the JTable (Model);
II.Column of the control table
1) column is not provided with the container assembly to automatically adjust the size of the variation width.
table.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
2) limit the width of a column.
the TableColumn firsetColumn = table.getColumnModel () getColumn (0);.
firsetColumn.setPreferredWidth (30);
firsetColumn.setMaxWidth (30);
firsetColumn.setMinWidth (30 );
3) set the number of the current column.
DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();
int = COUNT. 5;
tableModel.setColumnCount (COUNT);
. 4) acquired number of table columns
int cols = table.getColumnCount ();
. 5) Add a column
DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();
tableModel.addColumn ( "new column name");
6) to delete the column
. table.removeColumn (table.getColumnModel () getColumn ( columnIndex)); // columnIndex is to delete the column number
three. control of the table row
1) set row height
table.setRowHeight (20 is);
2) set the current number of aircraft
DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();
int n-=. 5;
tableModel.setRowCount (n-);
. 3) to obtain the number of table rows
int rows = table.getRowCount ( ); feedom.net

. 4) add a table row
DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();
tableModel.addRow (new new Object [] { "sitinspring", "35", "Boss"});
. 5) to delete a table row
tableModel = DefaultTableModel (DefaultTableModel) table.getModel ();
model.removeRow (rowIndex); // rowIndex row number is to be deleted


. four data access table cell
1) cell data fetch
DefaultTableModel tableModel = (DefaultTableModel) table .getModel ();
String CellValue = (String) tableModel.getValueAt (row, column); // get the data cell, row is the row number, column number a column
2) to fill the data table.
Note: The data types are Member list, Member classes are as follows:
public class Member {
    // Name
    Private String name;
   
    // Age
    Private String Age;
   
    // post
    Private String title;
}
code for padding data :
public void FillTable (List <Member> Members) {

 

  DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();
  tableModel.setRowCount (0); // clear the original row
 
  // padding data
  for (Member Member: Members) {
    String [] = ARR new new String [. 3];
    ARR [0] = member.getName ();
    ARR [. 1] = member.getAge ();
    ARR [2] = member.getTitle ();
   
    // add data to form
    tableModel .addRow (ARR);
  }
 
  // update the table
  table.invalidate ();
}
2) in the acquired data table
public List <Member> getShowMembers () {
  List <Member> Members = new new the ArrayList <Member> ();
 
  DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();
 
  int = tableModel.getRowCount the rowCount ();
 
  for (int I = 0; I <the rowCount; I ++) {
    Member Member Member new new = ();
   
    member.setName ((String) tableModel.getValueAt (I, 0)); // obtain the i-th row first column data
    member.setAge ((String) tableModel.getValueAt (i , 1)); // obtaining the i-th line data of the second column
    member.setTitle ((String) tableModel.getValueAt (i , 2 )); // i-th row obtain data of the third column
   
    members.add (Member);
  }
 
  return Members;
}
. five to obtain the user's selected row
1) selected by the user to obtain a single line
int selectRows = table.getSelectedRows () length; . // row selected by the user to obtain the number of rows
DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();

 

IF (SelectRows ==. 1) {
  int selectedRowIndex table.getSelectedRow = (); // Get the user-selected single line
 
  .// correlation processing
}
2) to obtain a plurality of rows selected by the user
int selectRows = table.getSelectedRows () length; . // user to obtain the number of lines of the selected row
DefaultTableModel tableModel = (DefaultTableModel) table.getModel ();

 


IF (SelectRows>. 1)
  int [] = selRowIndexs table.getSelectedRows (); // sequence user selected row
 
  for (int I = 0; I <selRowIndexs.length; I ++) {
    // with tableModel.getValueAt (row, column) data fetch cell
    String CellValue = (String) tableModel.getValueAt (I,. 1);
  }
}
. Add six event handler table
view.getTable (). addMouseListener (the MouseListener new new () {
  public void the mousePressed (the MouseEvent E) {
    // process when a mouse down
  }

  public void the mouseReleased (the MouseEvent E) {
    // release processing mouse
  }

  public void the mouseEntered (MouseEvent e) {
    processing into the form of the mouse //
  }

  public void the mouseExited (MouseEvent e) {
    process when the mouse // exit table
  }

 

  public void the mouseClicked (MouseEvent e) {
    // when treated mouse click
  }
});

Guess you like

Origin www.cnblogs.com/HIT-ryp/p/12584162.html