f时键入的文本

ny6fqffe  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(203)

当在swt表应用程序上点击ctrlf时,它会弹出一个搜索框。我的应用程序能够检测何时按下键盘快捷键,但是,我在捕获在搜索字段中键入的文本时遇到问题。
这是我在努力突出所发现的线条方面取得的进展。我想将捕获的文本分配给我的 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;
    }
}

这个问题如何在我的swt应用程序中检测ctrl-f是关于如何检测键盘快捷键的,它有一个答案。
我的问题是如何在默认情况下弹出的框中输入键入的文本。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题