Bug 262940 - Swing Component loses focus with Display.post(Event)
Summary: Swing Component loses focus with Display.post(Event)
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Silenio Quarti CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-01-29 13:10 EST by Timo Krieger CLA
Modified: 2019-09-06 15:36 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Timo Krieger CLA 2009-01-29 13:10:56 EST
I've implemented a onscreen keyboard for a SWT Application. The onscreen keyboard generates and submits events this way:

  Event evt = new Event();
  evt.keyCode = keyCode;
  evt.type = SWT.KeyUp;
  getDisplay().post(evt);

I know that i normally should not use Display.post, but i didn't find a better solution. For SWT Text composites etc. it works fine on every OS. I can type using the onscreen keyboard.

Now I have an embedded Swing application

  Frame frame = SWT_AWT.new_Frame(new Composite(..., SWT.EMBEDDED);
  Panel panel = new Panel(new BorderLayout());
  panel.add(..., BorderLayout.CENTER);
  frame.add(panel);

In the Swing applicaton are JTextFields and i want to use the OnScreen keyboard for these components, too.
On Linux it works fine with SWT 3.4, there is no difference between Swing and SWT composites.
On Windows the JTextField looses focus on the Display.post command. There is no caret or any other focus indicator. Sometimes it types the key, but normally it just only looses focus. I've tested this behaviour with SWT 3.4 and SWT 3.5 M4 - no difference.
Comment 1 Felipe Heidrich CLA 2009-01-29 16:21:56 EST
Is the swing compenent embed in SWT ?


IMO, this is not a support configuration. Does Swing have support to emulate events? If so you should try using that with Swing application.
Comment 2 Timo Krieger CLA 2009-01-30 03:39:32 EST
Here is a test program for this behaviour. On linux it's working, on windows the focus on the Swing JTextField gets lost.



public class SwtSwingFocusBug extends Composite {
  private static int keyChar = 0;
  
  public SwtSwingFocusBug(Shell shell, int style) {
    super(shell, style);
    
    FillLayout fl = new FillLayout();
    fl.type = SWT.VERTICAL;
    setLayout(fl);

    //Create the SWT Text input
    Composite swtInput = new Composite(this, SWT.NONE);
    swtInput.setLayout(new FillLayout());
    Label swtLabel = new Label(swtInput, SWT.NONE);
    swtLabel.setText("SWT Input");
    Text swtText = new Text(swtInput, SWT.BORDER);

    //Create the Swing Text input
    JPanel swingPanel = new JPanel(new GridLayout(1, 2));
    swingPanel.add(new JLabel("Swing Input"));
    swingPanel.add(new JTextField());

    Composite swingInput = new Composite(this, SWT.EMBEDDED);
    Frame frame = SWT_AWT.new_Frame(swingInput);
    //workaround for java 1.5 bug. Must add heavyweight container to receive mouse events.
    //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4982522
    Panel panel = new Panel(new BorderLayout());
    panel.add(swingPanel, BorderLayout.CENTER);
    frame.add(panel);
    
    //Create a Label to create KeyEvents
    Label key = new Label(this, SWT.BORDER | SWT.CENTER | SWT.NO_FOCUS);
    key.setText("Press here to post event");
    key.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseDown(MouseEvent e) {
        Event evt = new Event();
        evt.character = (""+keyChar).charAt(0);
        evt.type = SWT.KeyDown;
        getDisplay().post(evt);
      }
      
      public void mouseUp(MouseEvent e) {
        Event evt = new Event();
        evt.character = (""+keyChar).charAt(0);
        evt.type = SWT.KeyUp;
        getDisplay().post(evt);
        keyChar = (keyChar + 1) % 10;
      }
    });
  }
  
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    shell.setLayout(new FillLayout());
    new SwtSwingFocusBug(shell, SWT.NONE);
    if(!shell.isDisposed()) {
      shell.pack();
      shell.open();
    }
    while(!shell.isDisposed()) {
      if(!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
    System.exit(0);
  }
}
Comment 3 Eclipse Webmaster CLA 2019-09-06 15:36:19 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.