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

focusing breaks w/ combinations of lightwt/heavywt comp (SAMPLE SOURCE incl)

XMLWordPrintable

    • b01
    • x86
    • windows_nt
    • Not verified



        Name: rm29839 Date: 12/09/97


        /**************
         * when a heavyweight component receives input focus,
         * the requestFocus method for lightweight components in the
         * same container fails until it is called twice.
         *
         * This example program illustrates it - a bunch of
         * lightweight and heavyweight components are drawn -
         * tabbing between groups of lightweight or groups
         * of heavyweight are fine, but when an awt.Choice grabs
         * the focus, even clicking the mouse on a lightweight
         * still fails, since requestFocus fails the first time.
         * try tabbing through the entire screen to see it.
         */

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

        public class FocusTest extends Frame {
        static int [][]comp={
        {1,1,1,1,1},
        {0,0,0,0,0},
        {1,1,0,1,1},
        {1,1,0,0,1}
        };

        public static void main(String argv[]) {
        new FocusTest().setVisible(true);
        }


        public FocusTest() {
        setLayout(new GridLayout(comp.length,comp[0].length));
        for (int i=0; i<comp.length; i++)
        for (int j=0; j<comp[i].length; j++)
        if (comp[i][j]==0) {
        Choice c;
        add(c=new Choice());
        c.add("item 1");
        c.add("item 2");
        }
        else add(new LightweightComp());

        pack();
        }
        }


        // simple lightweight component, focus traversable, highlights upon focus
        class LightweightComp extends Component {
        FontMetrics fm;
        String label="light";
        boolean focus=false;

        public LightweightComp() {
        enableEvents(AWTEvent.FOCUS_EVENT_MASK|AWTEvent.MOUSE_EVENT_MASK);
        }

        public Dimension getPreferredSize() {
        if (fm==null) fm=Toolkit.getDefaultToolkit().getFontMetrics(getFont());
        return new Dimension(fm.stringWidth(label)+2,fm.getHeight()+2);
        }

        public void paint(Graphics g) {
        Dimension s=getSize();

        // erase the background
        g.setColor(getBackground());
        g.fillRect(0,0,s.width,s.height);

        g.setColor(getForeground());

        // draw the string
        g.drawString(label,2,fm.getHeight());

        // draw a focus rectangle
        if (focus) g.drawRect(1,1,s.width-2,s.height-2);

        }


        public boolean isFocusTraversable(){return true;}

        protected void processFocusEvent(FocusEvent e) {
        super.processFocusEvent(e);

        if (!e.isTemporary()) {
        if (e.getID()==FocusEvent.FOCUS_GAINED) {
        focus=true;
        }
        else {
        focus=false;
        }

        repaint();
        }
        }

        protected void processMouseEvent(MouseEvent e) {
        super.processMouseEvent(e);

        if (e.getID()==MouseEvent.MOUSE_PRESSED) {
        requestFocus();
        }
        }

        }
        (Review ID: 21646)
        ======================================================================

              rkhansunw Robi Khan (Inactive)
              rmandelsunw Ronan Mandel (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: