JDK-6267706 : Window couldn't be made focused (Linux 1.5)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.2_10
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: linux
  • CPU: generic
  • Submitted: 2005-05-10
  • Updated: 2011-01-19
  • Resolved: 2005-09-02
Description
This problem only happens on J2SE1.5 linux, and it works okay on J2SE 1.4/1.5 on Solaris and J2SE 1.4 on Linux.
In the following code, the test tries to make a focused window.
In case when the window is not focused, the test asks the user to manually click the window to make it focused.  But somhow on J2SE1.5 linux, when the user click on the window, the window doesn't have any response, seems that mouse click doesn't work at all.

See the following code:
=====================================================================
import java.awt.*;
import java.awt.event.*;

public class MyFocusableWindow {

    Frame frame = null;
    Window childWindow = null;
    
    public void checkCase1() {
	
	try {
	    frame = new Frame();
	    frame.setVisible(true);
	    childWindow = new Window(frame);
	    childWindow.setLocation(100, 100);
	    childWindow.setFocusableWindowState(true);
	    Component comp = new Button();
	    comp.setFocusable(true); 
	    childWindow.add(comp);
	    ComponentShow.makeVisible(childWindow);
	    if (!childWindow.getFocusableWindowState()) {
		System.out.println("getFocusableWindowState() return false");
		return;
	    }
	    if (!childWindow.isFocusableWindow()) {
		System.out.println("childWindow is not focusableWindow");
		return;
	    }
	    if (!moveFocusTo(childWindow)) {
		System.out.println("childWindow did not become focused window");
		return;
	    }
	    System.out.println("OKAY");
	} finally {
	    frame.dispose();
	}
    }   
    
    public static void main(String[] args) {
        MyFocusableWindow t = new MyFocusableWindow();
        t.checkCase1();
    }
    
    private  boolean moveFocusTo(Window window) { 
        window.toFront();
        if (!window.isFocused()) {
            Rectangle b = window.getBounds();
	    CustomComponent comp = new CustomComponent(
	            "PLEASE FOCUS THIS WINDOW");                                                              
            try {
                comp.setFocusable(false);
                window.add(comp);
		window.pack();           
                try {        
                    while (!window.isFocused()) {
                        System.out.println("I am in while");
                        Thread.sleep(100);
                    }
                } catch (InterruptedException e) {
                    System.out.println("got interruptedException");
                }
            } finally {
                window.setBounds(b);
	        window.remove(comp);
            }
        }
        return window.isFocused(); 
   }
          
     /**
     * Light weight component for interactive focusing
     */
    private class CustomComponent extends Component {

        private String text;
        int width = 0;
        int height = 0;

        public CustomComponent(String text) {
            this.text = text;
	    this.setFont(new Font("Dialog", Font.BOLD, 14));
            FontMetrics fm = getFontMetrics(getFont());
            width = fm.stringWidth(text) + 40;
            height = fm.getHeight() + 40;
        }

        public void paint(Graphics g) {
            g.setColor(Color.yellow);
            g.fill3DRect(0, 0, getWidth(), getHeight(), true);
            g.setColor(Color.black);
            g.drawString(text, 0, getSize().height/2);
            super.paint(g);
        }
        
        public Dimension getMinimumSize() {
            return new Dimension(width, height);
        }
  	
  	public Dimension getPreferredSize() {
	   return new Dimension(width, height);
        }   
    }         
}
=====================================================================
    

###@###.### 2005-05-10 01:18:00 GMT