SwingBuilder中使用Jtable,例4:某一行不可编辑+事件监听

-------------------------

import javax.swing.*;
import groovy.swing.SwingBuilder;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.event.*;

def data =[
	[nick: 'MrG', full: 'Guillaume Laforge'],
	[nick: 'jez', full: 'Jeremy Rayner'],
	[nick: 'fraz', full: 'Franck Rasolo'],
	[nick: 'sormuras', full: 'Christian Stein'],
	[nick: 'blackdrag', full: 'Jochen Theodorou'],
	[nick: 'Mittie', full:true]
]
def swing = new SwingBuilder();
def frame = swing.frame(title: 'Table Demo', defaultCloseOperation: JFrame.DISPOSE_ON_CLOSE) {
	vbox {
		scrollPane {
			tab = table {
			editing: true;
				model = tableModel(list: data) {
					propertyColumn(header: 'Nickname', propertyName: 'nick');
					propertyColumn(header: 'Full Name', propertyName: 'full',editable:false);
				}
			}
		}
		button() {
			action(name: '打印model的内容') {
				println(model.rows);
				//println(tab.cellEditor.getTableCellEditorComponent());
				//println(tab.editorComponent);
			}
		}
	}
}

frame.pack();
frame.show();

//model.getRows().add([nick:'李',full:'李某某']);
model.rows.add([nick: '李', full: '李某某']);
tab.updateUI();
//model.rows.clear();
//tab.updateUI();



/*
tab.getSelectionModel().addListSelectionListener(
    new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent event) {
            println event;
        }
    }
);
model.addTableModelListener(
    new TableModelListener() {
        public void tableChanged(TableModelEvent  event) {
            println event;
            println "yes";
        }
    }
)
*/

//监听JTable的鼠标事件
/*
tab.addMouseListener(
	new MouseAdapter() {
		public void mouseClicked(MouseEvent event) {
			//println event;
			//groovy.inspect.swingui.ObjectBrowser.inspect(event);
			//println "yes";
			def tab = event.component;
			tab.setValueAt(!tab.getValueAt(tab.getSelectedRow(), tab.getSelectedColumn()), tab.getSelectedRow(), tab.getSelectedColumn());
			tab.updateUI();
		}
	}
);
*/

/*
//监听JTable的键盘事件
tab.addKeyListener(
	new KeyAdapter() {
		public void keyPressed(KeyEvent event) {
			//groovy.inspect.swingui.ObjectBrowser.inspect(event);
		}
	}
);
*/

   propertyColumn(header: 'Full Name' , propertyName:  'full' ,editable: false ); 

-------------------------

 

fieldsTable = table() {
								//editing:true;
								//tableColumn(headerValue: "字段名");
								//tableColumn(headerValue: "类型");
								fieldsTableModel = tableModel(list: data) {
									propertyColumn(header: "字段名", propertyName: "field", editable: false);
									propertyColumn(header: "类型", propertyName: "type", editable: false);
									propertyColumn(header: "可见", propertyName: "visible", editable: false);
									//tableModel(list:[[field:"auto_id",type:'int']]);
									closureColumn (
									header: 'Location',
									read: {row -> println row;return 1;},
									write: {row, newValue -> println "write: $row new value: $newValue"}
									)
								}
							}
 

-------------------------

你可能感兴趣的:(swing,groovy)