Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4126296

Adding Solaris AWT Panel cause FocusLost event to be generated

XMLWordPrintable

    • generic
    • solaris_2.5.1


      On Solaris, When using Swing Tooltips which are Panels, they are "stealing" the focus from the component which currently has focus. It doesn't appear the Panel is actually gettin the focus, although definately it is causing a focus lost to be sent to the JComponent which currently has focus.

      I've eliminated Swing and wrote this purely lightweight component version which demonstrates the problem.

      Clicking a "LWC" will cause the lwc to requestFocus(), repaint using "red". Moving the mouse into the awt.Canvas region will simulate a "tooltip" Panel being added to the frame. Whala, the currently lwc with focus gets a focusLost event, but the Panel didn't get a focusGained.

      import java.awt.*;
      import java.awt.event.*;

      public class FocusTest extends Frame {

        public FocusTest(){
          super("FocusTest Test");
          LWC ll = new LWC();
          LWC ll2 = new LWC();
          Canvas spacer = new Canvas();
          spacer.setSize(50,75);
          spacer.setBackground(Color.white);
          Panel p = new Panel();
          p.add(spacer);
          p.add(ll);
          p.add(ll2);

          spacer.addMouseListener(new MouseAdapter(){
            public void mouseEntered(MouseEvent e){
      showToolTip();
            }
          });
            
          add(p, BorderLayout.CENTER);
          setSize(200,250);
          pack();
          show();
        }
        
        public void showToolTip(){
          final Panel tooltip = new Panel();
          tooltip.setLayout(new BorderLayout());
          tooltip.add(new Label("YO"), BorderLayout.CENTER);
          tooltip.setVisible(true);
          tooltip.setSize(50,20);
          tooltip.setBackground(Color.black);
          tooltip.setForeground(Color.white);
          add(tooltip, BorderLayout.SOUTH);
          addFocusListener(new FocusAdapter(){
              public void focusGained(FocusEvent evt) {
      System.out.println("Tooltip focusGained: " + evt.getSource());
              }
              public void focusLost(FocusEvent evt) {
      System.out.println("Tooltip focusLost: " + evt.getSource());
              }
          });
            
          tooltip.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent e){
      remove(tooltip);
      repaint();
            }
            public void mouseEntered(MouseEvent e){
      remove(tooltip);
      repaint();
            }
            public void mouseExited(MouseEvent e){
      remove(tooltip);
      repaint();
            }
            public void mousePressed(MouseEvent e){
      remove(tooltip);
      repaint();
            }
            public void mouseReleased(MouseEvent e){
      remove(tooltip);
      repaint();
            }
          });
          pack();
        }

        
        public static void main(String args[]){
          FocusTest f = new FocusTest();
        }

        class LWC extends Component {
          boolean hasFocus = false;

          public LWC(){
            super();
            setSize(21,21);
            addMouseListener(new MouseEventHandler());
            addFocusListener(new FocusEventHandler());
            addKeyListener(new KeyEventHandler());
          }

          public boolean isFocusTestTraversable(){
            return true;
          }

          public Dimension getPreferredSize(){
            return new Dimension(50,50);
          }

          class MouseEventHandler extends MouseAdapter {
              public void mousePressed(MouseEvent evt) {
                  requestFocus();
              }
          }

          class KeyEventHandler extends KeyAdapter {
            public void keyPressed(KeyEvent evt){
      System.out.println("keyPressed: " + evt);
            }
          }

          class FocusEventHandler extends FocusAdapter {
              public void focusGained(FocusEvent evt) {
      System.out.println("focusGained: " + evt.getSource());
                  hasFocus = true;
                  repaint();
              }
              public void focusLost(FocusEvent evt) {
      System.out.println("focusLost: " + evt.getSource());
                  hasFocus = false;
                  repaint();
              }
          }

          public void paint(Graphics g) {
              if (hasFocus) {
      g.setColor(Color.red);
              } else {
      g.setColor(Color.blue);
              }
      g.fillRect(0,0,getSize().width-1, getSize().height-1);
          }
        }
      }


            jdunnsunw Jeffrey Dunn (Inactive)
            rschiavisunw Richard Schiavi (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: