首页 > 编程语言 >解决import javax.swing.JTable;偶发性复制不了的问题

解决import javax.swing.JTable;偶发性复制不了的问题

时间:2024-03-29 10:01:23浏览次数:22  
标签:KeyEvent swing System JTable table println import getKeyCode out

解决方法:

重写JTable类的键盘监听事件。

 

/**
* 20240313 add hzh
*/
table.addKeyListener(new KeyListener(){

@Override
public void keyPressed(KeyEvent e) {

//System.out.println("22222");
System.out.println("keycode"+e.getKeyCode());
if(e.getKeyCode()==KeyEvent.VK_CONTROL&&e.getKeyCode() == KeyEvent.VK_C ||e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_C|| (!e.isControlDown())&&e.getKeyCode() == KeyEvent.VK_C)
{
System.out.println("复制事件");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //获得系统粘贴板



table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());


String selectStr=ObjectUtils.toString(table.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));

System.out.println("复制文本:" + selectStr);
StringSelection textInfoSelected = new StringSelection(selectStr); //建立一个粘贴板内容实例.
clipboard.setContents(textInfoSelected, null); //将textInfoSelected加入到粘贴板中;
AIFClipboard aifclipboard = AIFPortal.getClipboard();
aifclipboard.clearClipboard();

}

}

@Override
public void keyReleased(KeyEvent e) {

//System.out.println("22222");
System.out.println("keycode"+e.getKeyCode());
if(e.getKeyCode()==KeyEvent.VK_CONTROL&&e.getKeyCode() == KeyEvent.VK_C ||e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_C|| (!e.isControlDown())&&e.getKeyCode() == KeyEvent.VK_C)
{
System.out.println("复制事件");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); // 获得系统粘贴板
String selectStr = ObjectUtils
.toString(table.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
StringBuffer sbf=new StringBuffer();
// 检查以确保我们仅选择了单元格的
// 相邻块
int numcols = table.getSelectedColumnCount();
int numrows = table.getSelectedRowCount();
int[] rowsselected = table.getSelectedRows();
int[] colsselected = table.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(table.getValueAt(rowsselected[i], colsselected[j]).toString().replace("\r\n", " ").replace("\n", " ").replace("\r", " "));
if (j < numcols - 1)
sbf.append(" ");
}
sbf.append("\n");
}

System.out.println("复制文本:" + sbf.toString());
StringSelection textInfoSelected = new StringSelection(sbf.toString()); // 建立一个粘贴板内容实例.
clipboard.setContents(textInfoSelected, null); // 将textInfoSelected加入到粘贴板中;
AIFClipboard aifclipboard = AIFPortal.getClipboard();
aifclipboard.clearClipboard();

}
}

@Override
public void keyTyped(KeyEvent arg0) {
System.out.println("33333");
}


});

标签:KeyEvent,swing,System,JTable,table,println,import,getKeyCode,out
From: https://www.cnblogs.com/huangzuhong/p/18103139

相关文章

  • importlib模块
    importimportlibres='myfile.b'ret=importlib.import_module(res)#frommyfileimportb#该方法最小只能到py文件名print(ret)  defsend_all(content):forpath_strinsettings.NOTIFY_LIST:#'notify.email.Email'module_path,cl......
  • ImportError: cannot import name ‘OrderedDict‘ from ‘typing‘
    问题描述使用timm时fromtimm.models.vision_transformerimportBlock遇到报错:"xxx/lib/python3.7/site-packages/torchvision/models/maxvit.py",line3,in<module>fromtypingimportAny,Callable,List,Optional,OrderedDict,Sequence,TupleImportE......
  • python3安装pandas库出现Could not import the lzma module
    1.安装lzma模块使用:python-mpipinstallbackports.lzma2.进入cd/usr/local/python3/lib/python3.7目录(系统不同,目录也有所不同,可以通过which命令来查找当前运行python是使用的那个目录的),然后编辑lzma.py,将下面代码:from_lzmaimport*from_lzmaimport_encode_......
  • 启动 uWSGI 时日志中显示 ImportError: No module named xxx 的解决方法
    前几天在部署Vue+Django+Uwsgi+Nginx的生产环境时,发现前端部署成功了,但是一直无法获取后端的数据,检查Uwsgi日志(位于uwsgi安装目录/projects/log下)发现日志中提示“ImportError:Nomodulenamedxxx”,试过网上的各种方法还是无法解决,最终摸索出一套解决方法如下: ......
  • `ij_javascript_spaces_within_imports = true` 这个设置表示在 JavaScript 代码的
    #http://editorconfig.orgroot=true[*]#表示所有文件适用charset=utf-8#设置文件字符集为utf-8indent_style=tab#缩进风格(tab|space)indent_size=4#缩进大小end_of_line=lf#控制换行类型(lf|cr|crlf)trim_trailing_whitespace=true#去除......
  • IDEA 如何自动导入(import)
    如果大家正在使用一个未曾导入(import)过的类,或者它的静态方法或者静态字段,IDEA会给出对应的建议,只要按下⌥(option)和回车就可以接受建议。但我觉得这样做仍然很麻烦,不够智能化。怎么办呢?打开IDEA的首选项,找到Editor|General|AutoImport。勾选上 Addunambiguousimpor......
  • 基于javaswing的学生信息管理系统
    (解析+源码)基于JAVASwing+MySQL实现学生信息管理系统(增、删、改、查)数据库/文件存储_学生信息管理系统实现增删改查-CSDN博客 Nosuitabledriverfoundforjdbc:mysql:建表之后登录报错 原因是没有mysql的jar包 在项目右键buildpath 选择configureBulidpath ......
  • idea import的maven类报红
    idea报红/显示红色的原因一般报红,显示红色,是因为idea在此路径下,找不到这个类。找到是哪个jar包的类导致idea报红点击报红的路径的上一层,进入jar包。比如:importcom.aaa.bbb.ccc.DddDto;这个import的ccc这个词报红,那就点击ccc路径的上一层,此处点击bbb,就可以......
  • [vue3 配置] import 图片无法识别的问题
    vue3引入图片,路径没错,但无法识别importdefaultImagefrom'@/assets/column.jpg'Cannotfindmodule'@/assets/column.jpg'oritscorrespondingtypedeclarations.为啥报这个错误?typescript无法识别图片文件,需要在配置中增加声明新建image.d.ts文件/*eslint-disable......
  • [GPT] export, export default, import, module.exports, require
     ES6规范:export和import配对 import的{}大括号里面指定要从其他模块导入的变量名, 如果export命令没有写default,那么import{}大括号里面的变量名,必须与export导出的名称相同。  exportdefault为模块指定默认输出,这样在引入时就不必关心模块输出的名字。 本质......