拖拽Drop - 从jTextPane白色部分滑动?

时间:2014-03-25 04:57:32

标签: java swing drag-and-drop highlighting jtextpane

如果我拖动&将选定的文本从jTextPane拖放到其工作的另一个窗口。

但是有没有办法拖拽和通过直接从jTextPane区域的白色部分单击滑动来从jTextPane中删除文本? (意味着无需点击选定的突出显示文本)

或自动将鼠标指针移动到突出显示的文本? (但似乎是一个"不太干净"解决方案:它是唯一的解决方案吗?

我称之为"白色部分",是jTextPane中没有突出显示文本的区域

简单示例:

        /**
         * Basic Frame Settings
         */
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

            JTextPane textPane = new JTextPane();
            textPane.setBounds(143, 73, 143, 78);
            contentPane.add(textPane);

            textPane.setEditable(false);
            textPane.setText("hello");
            textPane.setDragEnabled(true);

所选文字可以是DnD

但是如何从jTextPane的白色区域中获取DnD?

1 个答案:

答案 0 :(得分:0)

这里有一个更清晰的例子来展示一下,怎么做:

                boolean b = true;

                setBounds(100, 100, 450, 300);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);

                final JTextPane textPane = new JTextPane();

                textPane.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mousePressed(MouseEvent e) {

                        Robot robot = null;
                        try {
                            robot = new Robot();
                        } catch (AWTException ex) {
                            Logger.getLogger(Testframe.class.getName()).log(
                                    Level.SEVERE, null, ex);
                        }

                        // Set the Mouse X,Y Position
                        robot.mouseMove(textPane.getLocationOnScreen().x,
                                textPane.getLocationOnScreen().y);

                        if (b) {
                            // Simulate Mouse Double Click
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mousePress(InputEvent.BUTTON1_MASK);

                            b = false;

                        }



                    }

                });

                textPane.setBounds(143, 73, 143, 78);
                contentPane.add(textPane);

                textPane.setEditable(false);
                textPane.setText("hello");
                textPane.setDragEnabled(true);

有人有另一种更好的解决方案吗?

相关问题