下面是我写的一段代码,两个文件,功能:双击某一列,弹出对话框修改列值
/*
* Created on 2004-10-18
*/
package com.yuch.ui;import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;import java.util.Map;
import java.util.EventObject;import javax.swing.AbstractCellEditor;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;/**
* @author yuch
*/
public class DataTableCellEditor extends AbstractCellEditor
implements TableCellEditor {
private JLabel label = null;
private OntologyDialog dialog = null;
public DataTableCellEditor( Map classes ) {
label = new JLabel();
dialog = new OntologyDialog( classes,
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
stopCellEditing();
}
},
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
cancelCellEditing();
}
}
);
dialog.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
cancelCellEditing();
}
});
}
public Component getTableCellEditorComponent( JTable table,
Object value,
boolean isSelected,
int rowIndex,
int vColIndex ) {
return label;
}
public boolean shouldSelectCell( EventObject e ) {
//Start editing
dialog.setVisible( true );
//Tell caller it is ok to select this cell
return true;
}
public void cancelCellEditing() {
//Editing is canceled----hide dialog
dialog.setVisible( false );
super.cancelCellEditing();
}
public boolean stopCellEditing() {
//Editing is complete ---- hide dialog
dialog.setVisible( false );
super.stopCellEditing();
//Tell caller it is ok to use property value
return true;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue() {
return dialog.getProperty();
}
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
int clickCount = 2;
return ((MouseEvent)evt).getClickCount() >= clickCount;
}
return true;
}
}
/*
* Created on 2004-10-18
*/
package com.yuch.ui;import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;import java.util.Map;
import java.util.EventObject;import javax.swing.AbstractCellEditor;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;/**
* @author yuch
*/
public class DataTableCellEditor extends AbstractCellEditor
implements TableCellEditor {
private JLabel label = null;
private OntologyDialog dialog = null;
public DataTableCellEditor( Map classes ) {
label = new JLabel();
dialog = new OntologyDialog( classes,
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
stopCellEditing();
}
},
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
cancelCellEditing();
}
}
);
dialog.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
cancelCellEditing();
}
});
}
public Component getTableCellEditorComponent( JTable table,
Object value,
boolean isSelected,
int rowIndex,
int vColIndex ) {
return label;
}
public boolean shouldSelectCell( EventObject e ) {
//Start editing
dialog.setVisible( true );
//Tell caller it is ok to select this cell
return true;
}
public void cancelCellEditing() {
//Editing is canceled----hide dialog
dialog.setVisible( false );
super.cancelCellEditing();
}
public boolean stopCellEditing() {
//Editing is complete ---- hide dialog
dialog.setVisible( false );
super.stopCellEditing();
//Tell caller it is ok to use property value
return true;
}
// This method is called when editing is completed.
// It must return the new value to be stored in the cell.
public Object getCellEditorValue() {
return dialog.getProperty();
}
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
int clickCount = 2;
return ((MouseEvent)evt).getClickCount() >= clickCount;
}
return true;
}
}
/*
* Created on 2004-10-18
*/
package com.yuch.ui;import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;import com.yuch.xml.OntologyReader;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;import java.awt.event.ActionListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;/**
* @author yuch
*/
public class OntologyDialog extends JDialog {
private JList classList = null;
private DefaultListModel classListModel = null;
private JList propertyList = null;
private DefaultListModel propertyListModel = null;
private JList sysList = null;
private DefaultListModel sysListModel = null;
private JButton ok = null;
private JButton cancel = null;
private Map classes = null;
public OntologyDialog( Map classes,
ActionListener okListener,
ActionListener cancelListener ) {
this.classes = classes;
Set keys = classes.keySet();
classListModel = new DefaultListModel();
Iterator iter1 = keys.iterator();
while( iter1.hasNext() ) {
classListModel.addElement( iter1.next() );
}
classList = new JList( classListModel );
classList.setSelectedIndex(0);
classList.setVisibleRowCount(6);
classList.addListSelectionListener( new ClassSelectionListener() );
classList.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createBevelBorder(2),"类",TitledBorder.RIGHT,TitledBorder.BELOW_TOP) );
propertyListModel = new DefaultListModel();
ArrayList value = (ArrayList)classes.get( classList.getSelectedValue() );
Iterator valuesIter = value.iterator();
while( valuesIter.hasNext() ) {
ArrayList sys = (ArrayList)(valuesIter.next());
String temp = null;
if( sys.size() > 0 ) {
temp = (String)sys.get(0);
}
propertyListModel.addElement( temp );
// propertyListModel.addElement( (String)sys.get(0) );
}
propertyList = new JList( propertyListModel );
propertyList.setSelectedIndex(0);
propertyList.setVisibleRowCount(6);
propertyList.addListSelectionListener( new PropertySelectionListener() );
propertyList.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createBevelBorder(2),"属性",TitledBorder.RIGHT,TitledBorder.BELOW_TOP) );
sysListModel = new DefaultListModel();
sysList = new JList( sysListModel );
sysList.setVisibleRowCount(6);
sysList.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createBevelBorder(2),"属性别名",TitledBorder.RIGHT,TitledBorder.BELOW_TOP) );
ok = new JButton( "确定" );
cancel = new JButton ( "取消" );
ok.addActionListener( okListener );
cancel.addActionListener( cancelListener );
getContentPane().setLayout( new BorderLayout() );
JPanel center = new JPanel();
center.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(Color.magenta,Color.pink),"本体库查看选择器:") );
center.setLayout( new GridLayout(3,1) );
center.add( new JScrollPane(classList) );
center.add( new JScrollPane(propertyList) );
center.add( new JScrollPane(sysList) );
JPanel bottom = new JPanel();
bottom.setLayout( new FlowLayout(FlowLayout.RIGHT) );
bottom.add( ok );
bottom.add( cancel );
getContentPane().add( center,BorderLayout.CENTER );
getContentPane().add( bottom,BorderLayout.SOUTH );
pack();
}
/**
*
* @author yuch
*/
class ClassSelectionListener implements ListSelectionListener {
public void valueChanged( ListSelectionEvent e ) {
propertyListModel.removeAllElements();
ArrayList value = (ArrayList)classes.get( classList.getSelectedValue() );
Iterator valuesIter = value.iterator();
while( valuesIter.hasNext() ) {
// String sys = (String)(valuesIter.next());
// propertyListModel.addElement( sys.split(" ")[0] );
ArrayList sys = (ArrayList)(valuesIter.next());
String temp = null;
if( sys.size() > 0 ) {
temp = (String)sys.get(0);
}
propertyListModel.addElement( temp );
}
}
}
/**
*
* @author yuch
*/
class PropertySelectionListener implements ListSelectionListener {
public void valueChanged( ListSelectionEvent e ) {
sysListModel.removeAllElements();
ArrayList value = (ArrayList)classes.get( classList.getSelectedValue() );
String property = (String)propertyList.getSelectedValue();
Iterator valuesIter = value.iterator();
while( valuesIter.hasNext() ) {
// String sys = (String)(valuesIter.next());
// String[] syss = sys.split(" ");
// if( syss[0].equals(property) ) {
// for( int i = 0; i < syss.length; i ++ ) {
// sysListModel.addElement( syss[i] );
// }
// break;
// }
ArrayList sys = (ArrayList)(valuesIter.next());
if( sys.contains(property) ) {
for( int i = 0; i < sys.size(); i ++ ) {
sysListModel.addElement( sys.get(i) );
}
break;
}
}
}
}
/**
* Property value(alias)
* @return
*/
public String getProperty() {
if( sysList.getSelectedValue() == null ) {
return "null";
}
return (String)sysList.getSelectedValue();
}
public static void main(String[] args) {
Map map = new OntologyReader( "ont.owl" ).getClassesInfo();
new OntologyDialog( map,null,null ).show();
}
}
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
int clickCount = 2;
return ((MouseEvent)evt).getClickCount() >= clickCount;
}
return true;
}
可以定义你所要触发的事件