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; } }
把上面的JLabel 换成 JButton使用: TableColumnModel columnModel = dataTable.getColumnModel(); TableColumn column = columnModel.getColumn(0); column.setCellEditor( new DataTableCellEditor(ontology) ); column.setCellRenderer( new DataTableRenderer() );
当然可以了!
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.table.TableCellRenderer;/**
* @author yuch
*/
public class MyTableRenderer extends JButton implements TableCellRenderer {
public MyTableRenderer() { }
public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column ) {
if ( isSelected ) {
setBorder( BorderFactory.createLineBorder(Color.red) );
}
else {
setBorder( BorderFactory.createLineBorder(Color.DARK_GRAY) );
} setText( value.toString() );
return this;
}
}使用:
JTable table = new JTable();//自己初始化
TableColumnModel columnModel = table.getColumnModel();
TableColumn column = columnModel.getColumn(0); column.setCellRenderer( new MyTableRenderer() );
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;
}
}
TableColumnModel columnModel = dataTable.getColumnModel();
TableColumn column = columnModel.getColumn(0);
column.setCellEditor( new DataTableCellEditor(ontology) );
column.setCellRenderer( new DataTableRenderer() );
OntologyDialog----->无法识别!!
多谢 小鱼儿大哥了