如何在SWT Java应用程序上按下Ctrl-F时捕获键入的文本

时间:2017-07-14 17:54:41

标签: java swt

当在 SWT 表应用中点击 Ctrl F 时,会弹出一个搜索框。我的应用程序能够检测到键盘快捷键何时被击中,但是,我在捕获搜索字段中键入的文本时遇到问题。

这是我试图突出所发现的线条的过程。我想将捕获的文本分配给我的searchstring变量。

package stackE;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.wb.swt.SWTResourceManager;

public class QuickTableExampleA {

    public static void main(String[] args) {
        String data = "ljames@ubunzeus: ~|0x0480002c\n"
                + "Stack Overflow - Where Developers Learn, Share, & Build"
                + " Careers - Google Chrome|0x0520000a\n"
                + "Inbox - L. D. James - Mozilla Thunderbird|0x05208736\n";

        String searchstring = "Stack";

        Display display = new Display();
        Shell shell = new Shell(display);

        shell.setSize(700, 500);
        shell.setText("My Table Smipplet");
        shell.setLayout(new FillLayout());


        Table table = new Table(shell, SWT.BORDER);

        TableColumn column1 = new TableColumn(table, SWT.LEFT);
        TableColumn column2 = new TableColumn(table, SWT.LEFT);

        column1.setText("Window");
        column2.setText("Window ID");
        column1.setWidth(600);
        column2.setWidth(70);

        table.setHeaderVisible(true);
        String[] array = data.split("\n");
        for (int i = 0; i < array.length; i++) {
            // System.out.println(array[i]);
            String[] array2 = array[i].split("\\|");
            TableItem newrow = new TableItem(table, SWT.NONE);
            // newrow.setText(new String[] { array2[0], array2[1] });
            newrow.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
            if(array2[0].contains(searchstring))
                newrow.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
            newrow.setText(new String[] { array2[0], array2[1] });
        }

        table.addListener(SWT.MouseDoubleClick, new Listener()
        {
            @Override
            public void handleEvent(Event event)
            {
                Point pt = new Point(event.x, event.y);
                TableItem item = table.getItem(pt);
                if (item != null)
                {
                    /* Iterate over all columns and check if event is contained */
                    for (int col = 0; col < table.getColumnCount(); col++)
                    {
                        Rectangle rect = item.getBounds(col);
                        if (rect.contains(pt))
                        {
                            System.out.println("The WindowID is" + item.getText(1));

                            /*
                            The following methods are my efforts to remove the
                            current data so that only the new repopulated data
                            will be in the table.
                            */

                            table.removeAll();
                            repopulateTable();
                            break;
                        }
                    }
                }
            }

            private void repopulateTable() {
                System.out.println("This is a repopulateTable.");
                String[] array = getdata();
                for (int i = 0; i < array.length; i++) {
                    // System.out.println(array[i]);
                    String[] array2 = array[i].split("\\|");
                    TableItem newrow = new TableItem(table, SWT.NONE);
                    newrow.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
                    if(array2[0].contains(searchstring))
                        newrow.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
                    newrow.setText(new String[] { array2[0], array2[1] });
                }
            }
        });

        /////////////////////////////////////////////////////////////////////////////////////
        // This block will be triggered when CTRL-F is hit
        /////////////////////////////////////////////////////////////////////////////////////

        display.addFilter(SWT.KeyDown, new Listener() {

            @Override
            public void handleEvent(Event e) {
                if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
                {
                    System.out.println("From Display I am the Key down !!" + e.keyCode);
                }
            }
        });
        /////////////////////////////////////////////////////////////////////////////////////

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
              display.sleep();
          }
          display.dispose();
    }
    public static String[] getdata() {
        String data = "swt table delete records - Google Search - Google Chrome|0x0480002c\n"
                + "javaTools/src/javaTools/QuickTableExampleA.java -"
                + " /home/users/l/j/ljames/workspace - Eclipse|0x05a000aa\n"
                + "SWT Table Filters and Sorting - Stack Overflow - Google Chrome|0x05c002dc\n"
                + "swt java how to delete a range of rows - Google Search - Google Chrome|0x05208736\n";

        String[] array = data.split("\n");
        return array;
    }
}

这个问题How to detect ctrl-f in my SWT application是关于如何检测键盘快捷键,它有一个答案。

我的问题是如何输入默认弹出框中输入的文字。

0 个答案:

没有答案