jTable实例

package test.table;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import
javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class JTableTest extends JFrame {
class TableTableModel extends DefaultTableModel { /** * */
private static final long serialVersionUID = 679265889547674796L ;
public final String[] COLUMN_NAMES = new String[] { " 列0 " , " 列1 " , " 列2 " , " 列3 " };
public TableTableModel() { }
public int getColumnCount() {
return COLUMN_NAMES.length;
}
public String getColumnName( int columnIndex) {
return COLUMN_NAMES[columnIndex]; } // 将Table设成只读的
public
boolean isCellEditable( int row, int column) { return false ; } }
private JTable table; private int i = 0 ; private int j = 0 ; private int rowI = - 1 ;
/**
* Launch the application * @param args */
public static void main(String args[]) {
try { JTableTest frame = new JTableTest(); frame.setVisible( true ); }
catch (Exception e) { e.printStackTrace(); } }
/** * Create the frame */
public JTableTest() {
super (); setTitle( " JTable Test " );
getContentPane().setLayout(
null );
setBounds(
100 , 100 , 500 , 375 );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(
10 , 28 , 460 , 271 );
getContentPane().add(scrollPane);
table
= new JTable(); table.setShowGrid( true );
table.addMouseListener(
new UserMouseAdapter() {
/**
*//** * 鼠标单击事件 * @param e 事件源参数 */
public void mouseSingleClicked(MouseEvent e){
//
System.out.println("Single Clicked!");
rowI = table.rowAtPoint(e.getPoint());
// 得到table的行号
if (rowI > - 1 ) System.out.println( " 单击鼠标 " + ((TableTableModel)table.getModel()).getValueAt(rowI, 0 )); }
/**
*//** * 鼠标双击事件 * @param e 事件源参数 */ public void mouseDoubleClicked(MouseEvent e){ // System.out.println("Doublc Clicked!");
rowI = table.rowAtPoint(e.getPoint());
//
得到table的行号
if (rowI > - 1 )
System.out.println(
" 双击鼠标 " + ((TableTableModel)table.getModel()).getValueAt(rowI, 0 )); } });
table.setModel(
new TableTableModel()); scrollPane.setViewportView(table);
final JButton button = new JButton(); button.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { ((TableTableModel)table.getModel()).addRow( new String[] { " " + (i ++ ), " 含一 " , " 行2 " , " 行3 " }); } }); button.setText( " 添加一行 " ); button.setBounds( 10 , 308 , 99 , 23 ); getContentPane().add(button); final JButton button_1 = new JButton(); button_1.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent arg0) { ((TableTableModel)table.getModel()).insertRow((rowI >= 0 ? rowI: 0 ), new String[] { " 插入行 " + (j ++ ), " 插入行1 " , " 插入行2 " , " 插入行3 " }); } }); button_1.setText( " 插入行 " ); button_1.setBounds( 121 , 308 , 99 , 23 ); getContentPane().add(button_1); } } // UserMouseAdapter.java package test.table; import java.awt.event. * ; /** *用定时器延时单击事件实现鼠标双击事件,单击和双击事件互不影响! */ public class UserMouseAdapter extends java.awt.event.MouseAdapter { private boolean flag = false ; // 用来判断是否已经执行双击事件 private int clickNum = 0 ; // 用来判断是否该执行双击事件 public void mouseClicked(MouseEvent e) { final MouseEvent me = e; // 事件源 this .flag = false ; // 每次点击鼠标初始化双击事件执行标志为false if ( this .clickNum == 1 ) { // 当clickNum==1时执行双击事件 this .mouseDoubleClicked(me); // 执行双击事件 this .clickNum = 0 ; // 初始化双击事件执行标志为0 this .flag = true ; // 双击事件已执行,事件标志为true return ; } // 定义定时器 java.util.Timer timer = new java.util.Timer(); // 定时器开始执行,延时0.2秒后确定是否执行单击事件 timer.schedule( new java.util.TimerTask(){ private int n = 0 ; // 记录定时器执行次数 public void run() { if (flag){ // 如果双击事件已经执行,那么直接取消单击执行 n = 0 ; clickNum = 0 ; this .cancel(); return ; } if (n == 1 ) { // 定时器等待0.2秒后,双击事件仍未发生,执行单击事件 mouseSingleClicked(me); // 执行单击事件 flag = true ; clickNum = 0 ; n = 0 ; this .cancel(); return ; } clickNum ++ ; n ++ ; } }, new java.util.Date(), 200 ); // 设置延迟时间 } /** *//** * 鼠标单击事件 * @param e 事件源参数 */ public void mouseSingleClicked(MouseEvent e){ // System.out.println("Single Clicked!"); } /** *//** * 鼠标双击事件 * @param e 事件源参数 */ public void mouseDoubleClicked(MouseEvent e){ // System.out.println("Doublc Clicked!"); } }

你可能感兴趣的:(jtable)