【问题标题】:How to stop multiple selections with multiple StyledText widgets如何使用多个 StyledText 小部件停止多项选择
【发布时间】:2012-02-28 20:53:40
【问题描述】:

我正在开发一个在 Java 中使用 SWT 的应用程序,我遇到了来自多个 StyledText 小部件的一些奇怪行为。这是相当一致的:如果窗口/视图/编辑器中同时显示多个StyledText 小部件,您可以同时从每个小部件中选择您想要的任何内容。在屏幕截图中,有 4 个独立的小部件,有 4 个独立的选择。

我的期望是,如果我开始从一个小部件中进行选择,那么任何其他可能已经有选择的小部件都会丢失它,类似于您期望从网络浏览器获得的行为;一次只能选择一个。

我想解决这个问题,但我希望避免需要一些管理器来监听每个小部件并在制作新小部件时关闭选择。有一个更好的方法吗? (还有为什么会发生这种情况?)

【问题讨论】:

    标签: java swt


    【解决方案1】:

    为什么会这样?

    据我所知,小部件的行为取决于它的实现,即它是否是一个

    1. native widget org.eclipse.swt.widgets.Text)或
    2. custom widget org.eclipse.swt.custom.StyledText

    区别在于mouse downmouse up 事件的处理。

    例如,

    如果是 org.eclipse.swt.widgets.Textleft mouse down 最终会转换为 OS.SendMessage (hwnd, OS.WM_LBUTTONUP, wParam, lParam);

    Whereas

    org.eclipse.swt.custom.StyledTexthandleMouseDown(Event event) 方法中使用鼠标事件处理程序和额外处理。大多数功能或 UI 都是使用 custom draw/redraw/validate/invalidate/update 方法完成的。

    用很粗俗的win32 sdk方式说:

    1. windows/win32 GDI提供了一些控件
    2. 而且,有些是自定义的用户绘制控件

    See 下面的 SWT 代码使用 textstyledtextbrowser 等进行测试。另外注意浏览器控件并不完全是win32控件,它是Internet Explorer activex或mozilla的gecko引擎的包装控件,therefore其行为与styled text相同。

    任何可能的解决方案?

    好吧,我只能想到借用 SWT 的 styledtext 代码,然后制作一个适合我的版本。

    或者

    正如您已经提到的,使用一些侦听器来重置所有其他未聚焦的小部件(即使在我看来这也不是一个非常干净的解决方案)。

    测试代码和输出

    Code:

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.browser.Browser;
    import org.eclipse.swt.custom.CCombo;
    import org.eclipse.swt.custom.StyleRange;
    import org.eclipse.swt.custom.StyledText;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Combo;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    
    public class StyledTextTest {
    
        private static Display display;
    
        public static void main(String[] args) 
        {
            display = new Display();
            Shell shell = new Shell(display);
            shell.setLayout(new GridLayout(2,true));
            shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
    
            createStyledText(shell);
            createStyledText(shell);
    
            createText(shell);
            createText(shell);
    
            createCombo(shell);
            createCombo(shell);
    
            createCustomCombo(shell);
            createCustomCombo(shell);
    
            createBrowser(shell);
            createBrowser(shell);
    
            shell.pack();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
    
        private static void createCustomCombo(Composite parent) 
        {
            new Label(parent, SWT.NONE).setText("Custom Combo");
            CCombo c = new CCombo(parent, SWT.DROP_DOWN);
            c.setItems(new String[] {"test best", "best rest", "rest test"});
            c.select(0);        
            c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        }
    
        private static void createCombo(Composite parent) 
        {
            new Label(parent, SWT.NONE).setText("Combo");
            Combo c = new Combo(parent, SWT.DROP_DOWN);
            c.setItems(new String[] {"test best", "best rest", "rest test"});
            c.select(0);        
            c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        }
    
        static void createBrowser(Composite parent)
        {
            new Label(parent, SWT.NONE).setText("Browser");
            Browser browser = new Browser(parent, SWT.NONE);
            browser.setText("<div>This is a test !!</div>");
            browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        }
    
        static void createText(Composite parent) {
            new Label(parent, SWT.NONE).setText("Text");
            final Text text = new Text(parent, SWT.BORDER);
            text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    
        }
    
        static void createStyledText(Composite parent)
        {
            new Label(parent, SWT.NONE).setText("Styled Text");
    
            StyledText text = new StyledText (parent, SWT.BORDER|SWT.SINGLE);
            text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
            // make 0123456789 appear bold
            StyleRange style1 = new StyleRange();
            style1.start = 0;
            style1.length = 10;
            style1.fontStyle = SWT.BOLD;
            text.setStyleRange(style1);
            // make ABCDEFGHIJKLM have a red font
            StyleRange style2 = new StyleRange();
            style2.start = 11;
            style2.length = 13;
            style2.foreground = display.getSystemColor(SWT.COLOR_RED);
            text.setStyleRange(style2);
            // make NOPQRSTUVWXYZ have a blue background
            StyleRange style3 = new StyleRange();
            style3.start = 25;
            style3.length = 13;
            style3.fontStyle = SWT.BOLD | SWT.ITALIC;
            text.setStyleRange(style3);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-07
      • 2018-06-18
      • 2013-02-20
      • 2013-10-15
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 2013-09-28
      • 2023-03-07
      相关资源
      最近更新 更多