我已在应用程序中实现了ExcelAdaper类,以允许我在Excel和jTable之间进行复制和粘贴。
在我提供的示例中,我可以在左列中键入文本,TableCellListener将其复制到右列
但是,如果我从剪贴板粘贴文本,则不会调用TableCellListener。 我尝试在TableModel上调用fireTableCellUpdated,但这也不起作用。
如何将文本粘贴操作上的TableCellListener触发器引入第0列?
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.StringTokenizer;
import javax.swing.*;
public class SimpleTableExample extends JFrame
{
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;
public SimpleTableExample()
{
// Set the frame characteristics
setTitle( "Simple Table Application" );
setSize( 300, 200 );
setBackground( Color.gray );
// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create columns names
String columnNames[] = { "Paste Text Here", "Should appear here" };
// Create some data
String dataValues[][] =
{
{ "", "" },
{ "", "" },
};
table = new JTable( dataValues, columnNames );
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
ExcelAdapter myAd = new ExcelAdapter(table);
TableCellListener tcl = new TableCellListener(table, action);
}
public static void main( String args[] )
{
SimpleTableExample mainFrame = new SimpleTableExample();
mainFrame.setVisible( true );
}
Action action = new AbstractAction()
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) /* Specific actions associated with manual cell edits */
{
TableCellListener tcl = (TableCellListener)e.getSource();
int row = tcl.getRow();
int column = tcl.getColumn();
if (column == 0)
{
table.setValueAt(table.getValueAt(row, column),row,column + 1);
}
}
};
static class TableCellListener implements PropertyChangeListener, Runnable
{
private JTable table;
private Action action;
private int row;
private int column;
private Object oldValue;
private Object newValue;
public TableCellListener(JTable table, Action action)
{
this.table = table;
this.action = action;
this.table.addPropertyChangeListener( this );
}
private TableCellListener(JTable table, int row, int column, Object oldValue, Object newValue)
{
this.table = table;
this.row = row;
this.column = column;
this.oldValue = oldValue;
this.newValue = newValue;
}
public int getColumn()
{
return column;
}
public Object getNewValue()
{
return newValue;
}
public Object getOldValue()
{
return oldValue;
}
public int getRow()
{
return row;
}
public JTable getTable()
{
return table;
}
@Override
public void propertyChange(PropertyChangeEvent e)
{
if ("tableCellEditor".equals(e.getPropertyName()))
{
if (table.isEditing())
processEditingStarted();
else
processEditingStopped();
}
}
private void processEditingStarted()
{
SwingUtilities.invokeLater( this );
}
@Override
public void run()
{
row = table.convertRowIndexToModel( table.getEditingRow() );
column = table.convertColumnIndexToModel( table.getEditingColumn() );
oldValue = table.getModel().getValueAt(row, column);
newValue = null;
}
private void processEditingStopped()
{
newValue = table.getModel().getValueAt(row, column);
if (newValue != null && !newValue.equals(oldValue))
{
TableCellListener tcl = new TableCellListener(
getTable(), getRow(), getColumn(), getOldValue(), getNewValue());
ActionEvent event = new ActionEvent(
tcl,
ActionEvent.ACTION_PERFORMED,
"");
action.actionPerformed(event);
}
}
}//TableCellListener
public class ExcelAdapter implements ActionListener
{
private String rowstring,value;
private Clipboard system;
private StringSelection stsel;
private JTable jTable1 ;
public ExcelAdapter(JTable myJTable)
{
jTable1 = myJTable;
KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK,false);
KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK,false);
jTable1.registerKeyboardAction(this,"Copy",copy,JComponent.WHEN_FOCUSED);
jTable1.registerKeyboardAction(this,"Paste",paste,JComponent.WHEN_FOCUSED);
system = Toolkit.getDefaultToolkit().getSystemClipboard();
}
public JTable getJTable() {return jTable1;}
public void setJTable(JTable jTable1) {this.jTable1=jTable1;}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().compareTo("Copy")==0)
{
StringBuffer sbf=new StringBuffer();
// Check to ensure we have selected only a contiguous block of
// cells
int numcols=jTable1.getSelectedColumnCount();
int numrows=jTable1.getSelectedRowCount();
int[] rowsselected=jTable1.getSelectedRows();
int[] colsselected=jTable1.getSelectedColumns();
if (!((numrows-1==rowsselected[rowsselected.length-1]-rowsselected[0] &&
numrows==rowsselected.length) &&
(numcols-1==colsselected[colsselected.length-1]-colsselected[0] &&
numcols==colsselected.length)))
{
JOptionPane.showMessageDialog(null, "Invalid Copy Selection",
"Invalid Copy Selection",
JOptionPane.ERROR_MESSAGE);
return;
}
for (int i=0;i<numrows;i++)
{
for (int j=0;j<numcols;j++)
{
sbf.append(jTable1.getValueAt(rowsselected[i],colsselected[j]));
if (j<numcols-1) sbf.append("\t");
}
sbf.append("\n");
}
stsel = new StringSelection(sbf.toString());
system = Toolkit.getDefaultToolkit().getSystemClipboard();
system.setContents(stsel,stsel);
}
if (e.getActionCommand().compareTo("Paste")==0)
{
System.out.println("Trying to Paste");
int startRow=(jTable1.getSelectedRows())[0];
int startCol=(jTable1.getSelectedColumns())[0];
try
{
String trstring= (String)(system.getContents(this).getTransferData(DataFlavor.stringFlavor));
System.out.println("String is:"+trstring);
StringTokenizer st1=new StringTokenizer(trstring,"\n");
for(int i=0;st1.hasMoreTokens();i++)
{
rowstring=st1.nextToken();
StringTokenizer st2=new StringTokenizer(rowstring,"\t");
for(int j=0;st2.hasMoreTokens();j++)
{
value=(String)st2.nextToken();
if (startRow+i< jTable1.getRowCount() &&
startCol+j< jTable1.getColumnCount())
jTable1.setValueAt(value,startRow+i,startCol+j);
System.out.println("Putting "+ value+"at row="+startRow+i+"column="+startCol+j);
}
}
}
catch(Exception ex){ex.printStackTrace();}
}
}
}
}
答案 0 :(得分:0)
ExcelAdapter类未引用预期的TableCellListener。 虽然我还没有开发出解决方案,但在应用程序之外复制问题有助于我确定问题所在。