javafx-点击确定按钮发送选中的文字

思路是:

  1. 鼠标进入按钮使用Robot类执行ctr+c
  2. 按钮点击事件,从剪切板获取内存,设置到textField中
package sample;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import sample.main.scanNettyClient;
import sample.main.scanNettyClientHandler;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class scanResultController implements Initializable {

    @FXML
    private TextField searchtext;

    @FXML
    private GridPane resultgp;

    @FXML
    private ListView<?> resultlv;

    @FXML
    private VBox resultright;

    @FXML
    private Button hidde;

    boolean showAndHide=false;

    @FXML
    private Button searchid;
    private scanNettyClientHandler scanH;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        searchid.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                try {
                    keyPressWithCtrlC();
                } catch (AWTException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                }
            }
        });


        new Thread(new Runnable() {
            @Override
            public void run() {
                scanH=new scanNettyClientHandler();
                scanNettyClient sc=new scanNettyClient();
                sc.startScanNettyClient(scanH);
            }
        }).start();
    }

    @FXML
    void searchidAction(ActionEvent event) throws AWTException, IOException, UnsupportedFlavorException {
        Clipboard clip=Toolkit.getDefaultToolkit().getSystemClipboard();
        DataFlavor flavor=DataFlavor.stringFlavor;
        String s=(String) clip.getData(flavor);
        searchtext.setText(s);
        scanH.sendmsg(s);
    }

    @FXML
    void hiddeAction(ActionEvent event) {
        if (!showAndHide) {
            resultright.setVisible(false);
            GridPane.setColumnSpan(resultlv, 3);
            GridPane.setRowSpan(resultlv, 3);
        } else {
            resultright.setVisible(true);
            GridPane.setColumnSpan(resultlv, 2);
            GridPane.setRowSpan(resultlv, 3);
        }
    }

    public void keyPressWithCtrlC() throws AWTException, IOException, UnsupportedFlavorException {
        Robot r = new Robot();
        r.keyPress(KeyEvent.VK_CONTROL);
        r.keyPress(0x43);
        r.keyRelease(0x43);
        r.keyRelease(KeyEvent.VK_CONTROL);
        r.delay(100);//非常重要
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值