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

JButton stays pressed when focus stolen

XMLWordPrintable

    • b72
    • generic, x86
    • generic, windows_98, windows_nt, windows_2000, windows_xp

        Name: skT45625 Date: 09/06/2000


        java version "1.3.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build
        Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

        A user enters a value in to a text field and then trys to click an apply
        button. The focus listener on the text field detects that the focus is being
        lost so it post-validates the text field value. It finds that an illegal value
        has been entered so it 1. requests that the focus be returned to it and 2.
        displays a JOptionPane error message. Strange things happen with the state of
        the JButton that was pressed.

        Two examples:

        1. IntField2 example - Type in a value other than "blah" into the text field
        and then click a button. You will get the illegal value dialog and focus will
        be returned to the text field. After you do this a couple of times (usually on
        the second try), you will notice that when you move your mouse over the button
        it appears pressed and when your mouse is not over the button, it is not
        pressed. If you use this example with 1.2.2 instead of 1.3, the button stays
        pressed all the time.

        2. VerifierTest example - a modified version of a demo written by Sun to
        demonstrate the InputVerifier class. Type in a value other than "pass" into
        the top field then click on the button. You will notice the error dialog pops
        up and then all appears fine. However, if you resize the frame, the button
        will redraw and you will see that it is in the pressed state. After you mouse
        over it for the first time, it will then show as pressed when the mouse is over
        it.


        // EXAMPLE 1 *******************************************

        import javax.swing.*;
        import javax.swing.event.*;
        import java.awt.event.*;
        import java.awt.*;
        import javax.swing.text.*;


        public class IntField2 {
            
            public static void main(String[] args) {
                JFrame f = new JFrame("IntField2 Demo");
                Container p = f.getContentPane();
                p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));

                final JTextField tf = new JTextField("blah");

                tf.addFocusListener(new FocusAdapter() {
                    public void focusLost(FocusEvent e) {
                        System.out.println("Trying to take focus");
                        if (!tf.getText().equals("blah")) {
                            
                            SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    tf.requestFocus();
                                    
                                    String message = "illegal value: " + tf.getText();
                                    JOptionPane.showMessageDialog(tf.getParent(),
        message,
                                                            "Illegal Value",
        JOptionPane.ERROR_MESSAGE);
                                                            
                                    
                                }
                            });
                            tf.setText("blah");
                            
                        }

                    }
                });

                final JButton b = new JButton("Button 1");
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if (b.hasFocus()) {
                            System.out.println("Button clicked." + b.isSelected() +
        tf.getText());
                            //b.setBorder(RAISE);
                            System.out.print(b.getBackground());
                        }
                            
                        else
                            System.out.println("(didn't have focus)" + b.isSelected() +
        tf.getText());
                    }

                });

                final JButton c = new JButton("Button 2");
                c.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("trying to reset b");
                        b.repaint();
                        b.invalidate();
                        b.revalidate();
                        b.requestFocus();
                        
                    }
                });
                
                
        // p.add(a);
                p.add(b);
                p.add(c);
                p.add(tf);
                
                f.addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
                
                f.pack();
                f.setVisible(true);
            }
        }


        // EXAMPLE 2 ****************************************

         import java.awt.*;
         import java.util.*;
         import java.awt.event.*;
         import javax.swing.*;
         
         // This program demonstrates the use of the Swing InputVerifier class.
         // It creates two text fields; the first of the text fields expects the
         // string "pass" as input, and will allow focus to advance out of it
         // only after that string is typed in by the user.

         class VerifierTest extends JFrame {

          public VerifierTest () {
            JTextField tf;
            tf = new JTextField ("TextField1");

            getContentPane().add (tf, BorderLayout.NORTH);
            tf.setInputVerifier(new PassVerifier());

            tf = new JTextField ("TextField2");
            getContentPane().add (tf, BorderLayout.SOUTH);
            
            final JButton b = new JButton("Button");
            b.setVerifyInputWhenFocusTarget(true);
            getContentPane().add (b, BorderLayout.EAST);
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (b.hasFocus())
                        System.out.println("Button clicked");
                }
            });
                          
            addWindowListener (new MyWAdapter ());
          }



           public static void main (String [] args) {
             Frame f = new VerifierTest ();
             f.pack();
             f.show();
           }

           class MyWAdapter extends WindowAdapter {

             public void windowClosing(WindowEvent event) {
               System.exit (0);
             }
           }
                       
           class PassVerifier extends InputVerifier {

             public boolean verify(JComponent input) {
               JTextField tf = (JTextField) input;
               String pass = tf.getText();
               if (pass.equals("pass")) return true;
               else {
                     String message = "illegal value: " + tf.getText();
                     JOptionPane.showMessageDialog(tf.getParent(), message,
                                      "Illegal Value", JOptionPane.ERROR_MESSAGE);
               
                 return false;
               }
             }
           }
         }
        (Review ID: 109338)
        ======================================================================

              shickeysunw Shannon Hickey (Inactive)
              skondamasunw Suresh Kondamareddy (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: