JTable的View和TableModel的关系

进rtgj的第一个培训任务,是一个User Manager UI System,要求熟悉java.swing.*,java.awt.* ,以及sun开源项目组SwingLabs的org.jdesktop.swingx.*等 API——一个继承自swing的扩展可视化组件包。

第一天,接下任务来真是无从入手,看似做起来只要简单拖拽一下NetBeans的Matisse就可以了,然而对APIs几乎忘得差不多的我(想起倚天里面张老道要张教主学太极,看招式要到尽忘的地步,偶惊为天人之...),开始一步步硬着头皮熟悉Netbeans的操作,swing和awt的窗口控件调用,上网狂找所以swingx的东西(http://swinglabs.org/index.jsp主要是这个SwingLabs的网站,从中找Demo看,Documentation看,Tutorial看),等几乎过了两天了,仍然没头绪,不行了,临时开始Coding吧。

效果总是不断完善的,代码总是不断修补的,时间总是不断流失的,美剧总是不断错过的(何止,mm啦,什么网游啊,哪个不是统统错过咯),最后,终于写就了,Resolve了一下,然后Alva看了下,指出了一个可以说是关键性的,不可忽视的错误,原来对JTable的View和TableModel映射关系还是没搞懂,啥也别说,看代码吧。

 

 

/*
 * UserManagerUI.java
 *
 
*/

/**
 *When you click one row,the record will show on the textfields;
 * When you enter the textfields and click the "Add Record", the new column will be added;
 * When you click one row ,change the contens in the textfields and click the "Modify Record",the new informations will be recorded.
 * when you select rows and click the "Delete Record",the rows will be removed.
 * 
@author   matthew
 
*/
import  java.util.logging.Level;
import  java.util.logging.Logger;
import  javax.swing.table. * ;
import  javax.swing. * ;
import  java.util. * ;
import  java.awt. * ;
import  java.awt.event. * ;
import  org.jdesktop.swingx.table. * ;
import  org.jdesktop.swingx. * ;
import  org.jdesktop.swingx.decorator. * ;

public   class  UserManagerUI  extends  javax.swing.JFrame {

    
public  UserManagerUI() {
        initComponents();


        dTableModel 
=   new  DefaultTableModel(p, n);
        jXTable1.setModel(dTableModel);
        jXTable1.packAll();

        jXTable1.setHighlighters(
new  Highlighter[]{AlternateRowHighlighter.classicLinePrinter});

        jXTable1.addHighlighter(
new  RolloverHighlighter(Color.ORANGE, Color.MAGENTA));
        jXTable1.setRolloverEnabled(
true );

        jXTable1.setColumnControlVisible(
true );
        jXTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    
/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     
*/
    
//  <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          

    
private   void  initComponents() {
        jFrame1 
=   new  javax.swing.JFrame();
        jXPanel1 
=   new  org.jdesktop.swingx.JXPanel();
        jXPanel2 
=   new  org.jdesktop.swingx.JXPanel();
        jLabel2 
=   new  javax.swing.JLabel();
        name 
=   new  javax.swing.JTextField();
        jLabel3 
=   new  javax.swing.JLabel();
        jLabel4 
=   new  javax.swing.JLabel();
        jLabel5 
=   new  javax.swing.JLabel();
        jLabel6 
=   new  javax.swing.JLabel();
        jLabel7 
=   new  javax.swing.JLabel();
        location 
=   new  javax.swing.JTextField();
        age 
=   new  javax.swing.JTextField();
        email 
=   new  javax.swing.JTextField();
        phone 
=   new  javax.swing.JTextField();
        jLabel8 
=   new  javax.swing.JLabel();
        sex 
=   new  javax.swing.JComboBox();
        marraged 
=   new  javax.swing.JComboBox();
        jScrollPane2 
=   new  javax.swing.JScrollPane();
        jXTable1 
=   new  org.jdesktop.swingx.JXTable();
        jXPanel3 
=   new  org.jdesktop.swingx.JXPanel();
        delR 
=   new  javax.swing.JButton();
        exit 
=   new  javax.swing.JButton();
        addR 
=   new  javax.swing.JButton();
        modR 
=   new  javax.swing.JButton();
        rowMode 
=   new  javax.swing.JComboBox();
        jXPanel4 
=   new  org.jdesktop.swingx.JXPanel();
        jLabel1 
=   new  javax.swing.JLabel();

        GroupLayout jFrame1Layout 
=   new  GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(
0 400 , Short.MAX_VALUE));
        jFrame1Layout.setVerticalGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(
0 300 , Short.MAX_VALUE));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel2.setText(
" Name " );

        name.setName(
"" );

        jLabel3.setText(
" Location " );

        jLabel4.setText(
" Age " );

        jLabel5.setText(
" E-mail " );

        jLabel6.setText(
" phone " );

        jLabel7.setText(
" Marraged " );

        jLabel8.setText(
" Sex " );

        sex.setModel(
new  javax.swing.DefaultComboBoxModel( new  String[]{ " Male " " Femal " }));

        marraged.setModel(
new  javax.swing.DefaultComboBoxModel( new  String[]{ " Already " " Never " }));

        jXTable1.setModel(
new  javax.swing.table.DefaultTableModel( new  Object[][]{{ null null null null },
                {
null null null null },
                {
null null null null },
                {
null null null null }},
                
new  String[]{ " Title 1 " " Title 2 " " Title 3 " " Title 4 " }));
        jXTable1.addMouseListener(
new  java.awt.event.MouseAdapter() {

                    
public   void  mouseClicked(java.awt.event.MouseEvent evt) {
                        showRecord(evt);
                    }
                });

        jScrollPane2.setViewportView(jXTable1);

        GroupLayout jXPanel2Layout 
=   new  GroupLayout(jXPanel2);
        jXPanel2.setLayout(jXPanel2Layout);
        jXPanel2Layout.setHorizontalGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(jScrollPane2, GroupLayout.DEFAULT_SIZE, 
506 , Short.MAX_VALUE).addGroup(jXPanel2Layout.createSequentialGroup().addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING,  false ).addGroup(jXPanel2Layout.createSequentialGroup().addComponent(jLabel2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(name, GroupLayout.PREFERRED_SIZE,  138 , GroupLayout.PREFERRED_SIZE)).addGroup(jXPanel2Layout.createSequentialGroup().addComponent(jLabel5).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(email))).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(jLabel6).addComponent(jLabel3)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING,  false ).addComponent(phone).addComponent(location, GroupLayout.DEFAULT_SIZE,  145 , Short.MAX_VALUE)).addGap( 14 14 14 ).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel2Layout.createSequentialGroup().addComponent(jLabel4).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(age, GroupLayout.PREFERRED_SIZE,  29 , GroupLayout.PREFERRED_SIZE).addGap( 4 4 4 ).addComponent(jLabel8).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(sex, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(jXPanel2Layout.createSequentialGroup().addComponent(jLabel7).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(marraged, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))).addContainerGap()));
        jXPanel2Layout.setVerticalGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel2Layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 
295 , GroupLayout.PREFERRED_SIZE).addGap( 23 23 23 ).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(jLabel3).addComponent(jLabel4).addComponent(age, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(jLabel8).addComponent(sex, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(location, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel2Layout.createSequentialGroup().addGap( 6 6 6 ).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel5).addComponent(jLabel6).addComponent(email, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))).addGroup(jXPanel2Layout.createSequentialGroup().addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(jXPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel7).addComponent(marraged, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(phone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        GroupLayout jXPanel1Layout 
=   new  GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(jXPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jXPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGap(
42 42 42 )));
        jXPanel1Layout.setVerticalGroup(jXPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jXPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        delR.setText(
" Delete Record " );
        delR.addActionListener(
new  java.awt.event.ActionListener() {

                    
public   void  actionPerformed(java.awt.event.ActionEvent evt) {
                        deleteRecord(evt);
                    }
                });

        exit.setText(
" Exit " );
        exit.addActionListener(
new  java.awt.event.ActionListener() {

                    
public   void  actionPerformed(java.awt.event.ActionEvent evt) {
                        exit(evt);
                    }
                });

        addR.setText(
" Add Record " );
        addR.addActionListener(
new  java.awt.event.ActionListener() {

                    
public   void  actionPerformed(java.awt.event.ActionEvent evt) {
                        addRecord(evt);
                    }
                });

        modR.setText(
" Modify Record " );
        modR.addActionListener(
new  java.awt.event.ActionListener() {

                    
public   void  actionPerformed(java.awt.event.ActionEvent evt) {
                        modifyRecord(evt);
                    }
                });

        rowMode.setModel(
new  javax.swing.DefaultComboBoxModel( new  String[]{ " SINGLE_SELECTION " " SINGLE_INTERVAL " " MULTIPLE_INTERVAL " }));
        rowMode.addItemListener(
new  java.awt.event.ItemListener() {

                    
public   void  itemStateChanged(java.awt.event.ItemEvent evt) {
                        rowModeItemStateChanged(evt);
                    }
                });

        GroupLayout jXPanel3Layout 
=   new  GroupLayout(jXPanel3);
        jXPanel3.setLayout(jXPanel3Layout);
        jXPanel3Layout.setHorizontalGroup(jXPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel3Layout.createSequentialGroup().addContainerGap().addComponent(addR).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(modR).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(delR).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(rowMode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(exit, GroupLayout.DEFAULT_SIZE, 
86 , Short.MAX_VALUE).addContainerGap()));
        jXPanel3Layout.setVerticalGroup(jXPanel3Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jXPanel3Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(addR).addComponent(modR).addComponent(delR).addComponent(rowMode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(exit)).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        jLabel1.setFont(
new  java.awt.Font( " Times New Roman " 0 18 ));
        jLabel1.setText(
" User Manager UI System " );

        GroupLayout jXPanel4Layout 
=   new  GroupLayout(jXPanel4);
        jXPanel4.setLayout(jXPanel4Layout);
        jXPanel4Layout.setHorizontalGroup(jXPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel4Layout.createSequentialGroup().addGap(
169 169 169 ).addComponent(jLabel1).addContainerGap( 193 , Short.MAX_VALUE)));
        jXPanel4Layout.setVerticalGroup(jXPanel4Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jXPanel4Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        GroupLayout layout 
=   new  GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(jXPanel1, 
0 , GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jXPanel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jXPanel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jXPanel4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(jXPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 
12 , Short.MAX_VALUE).addComponent(jXPanel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addContainerGap()));
        pack();
    }
//  </editor-fold>                        

    
private   void  rowModeItemStateChanged(java.awt.event.ItemEvent evt) {
        String modeSelected 
=  rowMode.getSelectedItem().toString();

        
if  (evt.getStateChange()  ==  ItemEvent.SELECTED) {
            
if  (modeSelected.equals( " SINGLE_SELECTION " )) {
                jXTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } 
else  {
                
if  (modeSelected.equals( " SINGLE_INTERVAL " )) {
                    jXTable1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                } 
else  {
                    
if  (modeSelected.equals( " MULTIPLE_INTERVAL " )) {
                        jXTable1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                    }
                }
            }
        }
    }

    
private   void  showRecord(java.awt.event.MouseEvent evt) {
        
int  i1  =  jXTable1.getSelectedRow();
        
int  i2  =   0 ;
        name.setText(dTableModel.getValueAt(i1, i2
++ ).toString());
        location.setText(dTableModel.getValueAt(i1, i2
++ ).toString());
        age.setText(dTableModel.getValueAt(i1, i2
++ ).toString());
        email.setText(dTableModel.getValueAt(i1, i2
++ ).toString());
        phone.setText(dTableModel.getValueAt(i1, i2
++ ).toString());
        marraged.setSelectedItem(dTableModel.getValueAt(i1, i2
++ ).toString());
        sex.setSelectedItem(dTableModel.getValueAt(i1, i2
++ ).toString());
    
//  TODO add your handling code here:
    }

    
private   void  modifyRecord(java.awt.event.ActionEvent evt) {
        dTableModel.removeRow(jXTable1.getSelectedRow());
        add();
    
// dTableModel.getValueAt() //  TODO add your handling code here:

    }

    
private   void  exit(java.awt.event.ActionEvent evt) {
        System.exit(
0 );
    }

    
private   void  deleteRecord(java.awt.event.ActionEvent evt) {

        
int [] sRows  =  jXTable1.getSelectedRows();
        
for  ( int  i  =  sRows.length  -   1 ; i  >=   0 ; i -- ) {
            dTableModel.removeRow(jXTable1.convertRowIndexToModel(sRows[i]));
        }
    
/*
         逆序;view 和 TableModel之间的解藕关系;
        for(int i=0;i<sRows.length;i++){
            dTableModel.removeRow(sRows[i]);
            sRows[i+1] = sRows[i+1]-i-1;            
         }
         
*/


    
/* jXTable1.revalidate(); */

    }

    
private   void  add() {
        record 
=   new  Vector();
        record.add(name.getText());
        record.add(location.getText());
        record.add(age.getText());
        record.add(email.getText());
        record.add(phone.getText());
        record.add(marraged.getSelectedItem());
        record.add(sex.getSelectedItem());


        dTableModel.addRow(record);
        jXTable1.revalidate();
    }

    
private   void  addRecord(java.awt.event.ActionEvent evt) {
        add();
    }
    
//  Variables declaration - do not modify                     

    
private  javax.swing.JButton addR;
    
private  javax.swing.JTextField age;
    
private  javax.swing.JButton delR;
    
private  javax.swing.JTextField email;
    
private  javax.swing.JButton exit;
    
private  javax.swing.JFrame jFrame1;
    
private  javax.swing.JLabel jLabel1;
    
private  javax.swing.JLabel jLabel2;
    
private  javax.swing.JLabel jLabel3;
    
private  javax.swing.JLabel jLabel4;
    
private  javax.swing.JLabel jLabel5;
    
private  javax.swing.JLabel jLabel6;
    
private  javax.swing.JLabel jLabel7;
    
private  javax.swing.JLabel jLabel8;
    
private  javax.swing.JScrollPane jScrollPane2;
    
private  org.jdesktop.swingx.JXPanel jXPanel1;
    
private  org.jdesktop.swingx.JXPanel jXPanel2;
    
private  org.jdesktop.swingx.JXPanel jXPanel3;
    
private  org.jdesktop.swingx.JXPanel jXPanel4;
    
private  org.jdesktop.swingx.JXTable jXTable1;
    
private  javax.swing.JTextField location;
    
private  javax.swing.JComboBox marraged;
    
private  javax.swing.JButton modR;
    
private  javax.swing.JTextField name;
    
private  javax.swing.JTextField phone;
    
private  javax.swing.JComboBox rowMode;
    
private  javax.swing.JComboBox sex;
    
//  End of variables declaration                   

    
private  Vector record;
    
// private String[] rowModes = {"ListSelectionModel.SINGLE_SELECTION","ListSelectionModel.SINGLE_INTERVAL_SELECTION","MULTIPLE_INTERVAL_SELECTION"};

    
private  DefaultTableModel dTableModel;
    
private  Object[][] p  =  {{ " Mike " " Taipei " new  Integer( 30 ),  new  String( " [email protected] " ),  new  String( " 3243-434-432525 " ),  new  String( " Never " ),  new  String( " Female " )},
            {
" Jack " " ChiaYi " new  Integer( 24 ),  new  String( " [email protected] " ),  new  String( " 3113-434-433425 " ),  new  String( " Already " ),  new  String( " Male " )},
            {
" Mary " " Beijing " new  Integer( 34 ),  new  String( " [email protected] " ),  new  String( " 3103-434-433405 " ),  new  String( " Never " ),  new  String( " Female " )},
            {
" Tom " " London " new  Integer( 26 ),  new  String( " [email protected] " ),  new  String( " 3013-434-433025 " ),  new  String( " Already " ),  new  String( " Male " )},
            {
" White

你可能感兴趣的:(UI,jsp,swing,Netbeans,网游)