在Text中setSelect()的作用是选中文本,和按住鼠标左键一直移动的选中的结果一样

selectAll()是全选

如果直接使用,是看不到效果的,需要结合setFocus()一起使用。两者的顺序不影响实际的结果。

测试使用的代码1

package test;import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.dialogs.*;
public class testlabel {public testlabel() {final Display display = Display.getDefault();final Shell shell = new Shell();shell.setSize(370, 235);shell.setText("Text 综合实例");final Label label1 = new Label(shell, SWT.NONE);label1.setBounds(25, 25, 60, 25);label1.setText("User Name:");final Text text1 = new Text(shell, SWT.BORDER);text1.setBounds(90, 20, 80, 20);// 当光标停留在该文本框时将出现提示信息text1.setToolTipText("文本项不能为空");text1.selectAll();final Label labe2 = new Label(shell, SWT.NONE);labe2.setBounds(190, 25, 55, 25);labe2.setText("PassWord:");final Text text2 = new Text(shell, SWT.PASSWORD | SWT.BORDER);text2.setBounds(250, 20, 80, 20);// setTextLimit(int x)为常用组件方法,用来设置文本框中最多可输入的字符数。text2.setTextLimit(8);text2.setToolTipText("文本项不能为空,且输入不超过8 位密码");text2.showSelection();final Text text3 = new Text(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);text3.setBounds(50, 55, 250, 90);final Button button1 = new Button(shell, SWT.NONE);button1.setBounds(140, 160, 80, 20);button1.setText("OK");button1.setToolTipText("单击OK 按钮,姓名将显示在下面的文本中");button1.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent e) {String AD = text1.getText();// 获得输入的文本内容。String BD = text2.getText();// 判断输入文本是否为空if (AD == null || AD.equals("") || BD == null || BD.equals("")){MessageDialog.openInformation(shell, " 信息提示", "失败信息!"+ '\n' + '\n' + " 注意:文本项不能为空!!!……");} elsetext3.append("User Name: " + '\n' + "" + AD + '\n'+ "PassWord:" + '\n' + "" + BD);/*** append()方法用来在文本框中显示内容。* 将append()方法改为insert()方法可达到同样的效果*/}});final Button button2 = new Button(shell, SWT.NONE);button2.setBounds(250, 160, 80, 20);button2.setText("Cancel");button2.setToolTipText("单击Cancel 按钮,清除文本中的内容");button2.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent e) {text1.setFocus();text1.setSelection(0,1);        //测试的地方//text1.clearSelection();// 清除文本内容方法//text1.showSelection();text2.setText("");text3.setText("");}});shell.open();shell.layout();while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}}public static void main(String[] args) {new testlabel();}
}

在输入内容后点击Cancel,结果为第一个字被选中

当使用text1.clearSelection()后,会取消选中显示光标,截不出来图,在“阿”后面即第一个位置,阿前是第0个位置。

最后一个是showSelection(),从官网找来的说明https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FText.html

即显示选中的内容,如果没有显示则通过滚动到选中位置来显示。

showSelection

public void showSelection()
Shows the selection.

If the selection is already showing in the receiver, this method simply returns. Otherwise, lines are scrolled until the selection is visible.

Throws:
SWTException -

  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

还有一个例子http://www.java2s.com/Code/JavaAPI/org.eclipse.swt.widgets/TextclearSelection.htm

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;public class MainClass {public static void main(String[] a) {Display d = new Display();Shell s = new Shell(d);Text text1 = new Text(s, SWT.WRAP | SWT.BORDER);text1.setBounds(100, 50, 100, 20);text1.setTextLimit(5);text1.setText("12345");Text text2 = new Text(s, SWT.SINGLE | SWT.BORDER);text2.setBounds(100, 75, 100, 20);text2.setTextLimit(30);FocusListener focusListener = new FocusListener() {public void focusGained(FocusEvent e) {Text t = (Text) e.widget;t.selectAll();}public void focusLost(FocusEvent e) {Text t = (Text) e.widget;if (t.getSelectionCount() > 0) {t.clearSelection();}}};text1.addFocusListener(focusListener);text2.addFocusListener(focusListener);s.open();while (!s.isDisposed()) {if (!d.readAndDispatch())d.sleep();}d.dispose();}
}

还有一个getSelection()

public Point getSelection()
Returns a Point whose x coordinate is the character position representing the start of the selected text, and whose y coordinate is the character position representing the end of the selection. An "empty" selection is indicated by the x and y coordinates having the same value.

Indexing is zero based. The range of a selection is from 0..N where N is the number of characters in the widget.

Returns:
a point representing the selection start and end
Throws:
SWTException -

  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

转载于:https://www.cnblogs.com/ant-xu/p/10963175.html

swt中关于Text.setSelection()的记录相关推荐

  1. sql中的text字段如何导入oracle

    这一段时间在把SQLSERVER迁移到ORASCLE,中间遇到了一系列的问题,通过查找资料和想办法,已经基本顺利解决,后面我会针对两个数据库迁移过程中我遇到的问题,整理汇总成一个个小问题,带着实例编码 ...

  2. Sqlserver别太信任SysComments表中的text字段

    1.用SysComments的原因 最近新模块的开发,需要更改和新增的存储过程比较多,为了同步开发环境和测试环境的存储过程,能在更新程序后,马上能整理出更改的存储过程脚本,并更新到测试DB服务器上,我 ...

  3. 关于SWT中的表格(TableViewer类)

    JFace是SWT的扩展.它提供了一组功能强大的界面组件.其中包含表格,树,列表.对话框,向导对话框等. 表格是一种在软件系统中很常用的数据表现形式.特别是基于数据库的应用系统.表格更是不可缺少的界面 ...

  4. 检索每个组中的最后一条记录-MySQL

    有一个表messages ,其中包含数据,如下所示: Id Name Other_Columns ------------------------- 1 A A_data_1 2 A A_data_2 ...

  5. 【Unity3D编辑器扩展】Unity3D中实现Text的字体的替换

    推荐阅读 CSDN主页 GitHub开源地址 Unity3D插件分享 简书地址 我的个人博客 大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦. 一.前言 ...

  6. mysql获取删除的条数_如何从mysql表中删除数百万条记录而不会减速

    有没有一种很好的方法来删除很多记录而不会减慢网站的速度? 我需要从没有索引和主键的MySQL表中删除数百万条记录.我阅读了SO和网上的各种教程,基本策略是限制删除查询,在删除之间休眠一两秒钟,然后重复 ...

  7. SQLSERVER中统计所有表的记录数

    SQLSERVER中统计所有表的记录数 利用系统索引表sysindexes中索引ID indid<1的行中的rows列存有该表的行数这一特点.    方法是利用隐藏未公开的系统存储过程sp_MS ...

  8. 在Elasticsearch中对 text 类型的字段进行聚合异常Fielddata is disabled,Set fielddata=true

    在Elasticsearch中对 text 类型的字段进行聚合异常Fielddata is disabled,Set fielddata=true 参考文章: (1)在Elasticsearch中对 ...

  9. 如何获取mongodb中的最后N条记录?

    我找不到任何记录在案的文件. 默认情况下,find()操作将从头开始获取记录. 如何获取mongodb中的最后N条记录? 编辑:我也希望返回的结果从最近到最近排序,而不是相反. #1楼 您可以使用so ...

  10. SWT中调用Automation的方式

    在SWT中提供了访问OLE的方式,不过相关的例子都是进程内OLE的例子,比如嵌入浏览器.引用ActiveX控件什么的.由于客户的需求,我们需要在程序中通过进程外Automation服务的方式访问IE浏 ...

最新文章

  1. 当文员学计算机二级,二本学生毕业后在干什么?多半做3种工作,过来人深有同感...
  2. mysql访问被拒绝1045_mysqlimport:错误:1045,访问被拒绝
  3. TCC Demo 代码实现
  4. python代码物理_python+appium的物理按键代码
  5. 简单整理 - 常用设计模式
  6. 参考文献格式字号字体_论文格式字体字号要求
  7. Redis 官方推出可视化工具,颜值爆表,功能真心强大!这是不给其他工具活路啊!...
  8. typedef使用方法
  9. docx文档文字怎么加边框_word给正文加边框 word怎样给一段文字加上边框
  10. 家庭用计算机是一体好还是,国产家用电脑一体机,性能够用就好!!!
  11. steam游戏文件夹在哪儿?
  12. [4G5G专题-129]:RF-架构演进的驱动力与RF常见术语
  13. 蓝桥杯-第六届省赛第一题
  14. 基于即时通信软件聊天界面的设计
  15. A Linux Environment Zero Overhead
  16. 从零开始实现一个基于RISC-V的流水线处理器 (1) :RISC-V指令集架构详解
  17. Java实现中值问题
  18. 中国水球装备行业市场供需与战略研究报告
  19. 公众号运营引流月吸万粉之互推
  20. 搭建Android+QT+OpenCV环境,实现“单色图片着色”效果

热门文章

  1. 【转】 Apache分析脚本
  2. 水晶报表 动态控制图片显示 Changing pictures dynamically in Crystal Report
  3. 一家胡三家的人工智能来了
  4. 用Python快速实现YOLO目标检测
  5. 推荐几个学霸级的技术公众号陪你过暑假
  6. 入门 ggplot2 的图形语法
  7. WCF服务编程-非WCF应用程序使用WCF服务
  8. .编写一个文件加解密程序,通过命令行完成加解 密工作
  9. cron 任务执行表达式
  10. 【转】CentOS系统操作下安装相关各种软件