在Excel中,可以刪除多個選定單元格的值,我如何通過JTable實現這一點?刪除多個單元格中的值
在此示例代碼中,只有一個單元格的值被刪除。
多個小區中選擇
只有一個在一個單元格的值被刪除,也刪除按鈕被按下,我不想因爲這是後進入編輯模式不在Excel中這樣做。
SSCCE
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;
import java.util.EventObject;
public class table extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
table frame = new table();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public table() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(2)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
);
table = new JTable(){
/**
*
*/
private static final long serialVersionUID = 1L;
public boolean editCellAt(int row, int column, EventObject e){
boolean result = super.editCellAt(row, column, e);
final Component editor = getEditorComponent();
if (editor == null || !(editor instanceof JTextComponent)) {
return result;
}
if (e instanceof KeyEvent) {
((JTextComponent)editor).selectAll();
}
return result;
}
};
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null}
},
new String[] {
"New column 1", "New column 2", "New column 3", "New column 4", "New column 5", "New column 6"
}
));
table.setCellSelectionEnabled(true);
scrollPane.setViewportView(table);
new PegarExcel(table);
contentPane.setLayout(gl_contentPane);
}
}
//*********************************************************************************************************
//Clase que se encarga del pegado
//*********************************************************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.datatransfer.*;
import java.util.*;
import java.io.IOException;
//----------------------------------------------------------------------------------------------------------------------
public class PegarExcel implements ActionListener{
private String rowstring,value;
private Clipboard system;
private StringSelection stringSelection,stsel;
private JTable jTable1 ;
//----------------------------------------------------------------------------------------------------------------------
public PegarExcel(JTable myJTable)
{
jTable1 = myJTable;
KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK,false);
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;
}
//----------------------------------------------------------------------------------------------------------------------
void showErrorMessage(String msg){
JOptionPane.showMessageDialog(null, msg,msg,JOptionPane.ERROR_MESSAGE);
}
//----------------------------------------------------------------------------------------------------------------------
void pasteAction(){
system = Toolkit.getDefaultToolkit().getSystemClipboard();
try{
String data= (String)system.getData(DataFlavor.stringFlavor);
if(data==null) {
showErrorMessage("No data on clipboard");
return;
}
int selectCol=jTable1.getSelectedColumn();
int selectRow=jTable1.getSelectedRow();
if(selectCol<0||selectRow<0) {
showErrorMessage("Please Select cell");
return;
}
//devuelve clipboard contenido
StringTokenizer st,stTmp;
st=new StringTokenizer(data,"\n");
int pasteRows=st.countTokens();
st=new StringTokenizer(st.nextToken().trim(),"\t");
int pasteCols=st.countTokens();
int marginCols=jTable1.getColumnCount()-selectCol;
int marginRows=jTable1.getRowCount()-selectRow;
//revisa espacio disponible
if(marginCols<pasteCols || marginRows<pasteRows){
//showErrorMessage("La tabla no posee el espacio suficiente para pegar los datos");
//return;
}
st=new StringTokenizer (data,"\n");
int rowCount=0,colCount;
//copia a la tabla
while(st.hasMoreTokens()){
stTmp=new StringTokenizer (st.nextToken(),"\t");
colCount=0;
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
System.out.println("model.getRowCount():"+model.getRowCount());
System.out.println("rowCount+selectRow:"+(rowCount+selectRow));
System.out.println("pasteRows:"+(pasteRows));
System.out.println("marginRows:"+(marginRows));
System.out.println("rowCount:"+rowCount);
if(rowCount+selectRow>model.getRowCount()-1){
if(0==model.getRowCount())
model.addRow(new Object[]{"","","",1,"MB",10000, true,true,"","",""});
else
model.addRow(new Object[]{model.getValueAt(model.getRowCount()-1, 0),"","",1,"MB", 10000, true,true,"","",""});
}
while(stTmp.hasMoreTokens()){
String columnClassName =jTable1.getColumnClass(colCount+selectCol).getName();
if("java.lang.String"==columnClassName)
jTable1.setValueAt(stTmp.nextToken(),rowCount+selectRow,colCount+selectCol);
else if("java.lang.Integer"==columnClassName)
jTable1.setValueAt(Integer.parseInt(stTmp.nextToken()),rowCount+selectRow,colCount+selectCol);
else if("java.lang.Boolean"==columnClassName){
boolean bool = Boolean.parseBoolean(stTmp.nextToken());
jTable1.setValueAt(bool,rowCount+selectRow,colCount+selectCol);
}
else
jTable1.setValueAt(stTmp.nextToken(),rowCount+selectRow,colCount+selectCol);
System.out.println("columnClassName: "+columnClassName);
//jTable1.setValueAt(stTmp.nextToken(),rowCount+selectRow,colCount+selectCol);
colCount++;
}
rowCount++;
}
}
catch(UnsupportedFlavorException uf){
System.out.println ("uf="+uf.getMessage());
}
catch(IOException io){
System.out.println ("io="+io.getMessage());
}
}
//----------------------------------------------------------------------------------------------------------------------
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().compareTo ("Paste")==0){
pasteAction();
return;
}
}
}
看起來您一直在使用視圖座標,但是您可能會交換兩列或排列要檢查的行。 – trashgod