🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8157

InputVerifier Dilemas

#8157
From: James Cook
Date: Wed, 18 Oct 2000 00:00
94 lines
3796 bytes
Have others discovered that the InputVerifier class in JDK 1.3, are pretty
much worthless?

Sure they work fine when tabbing from component to component, but they have
numerous faults. Suppose I have a panel with a textfield (with an
InputVerifier assigned), a combo box, a checkbox, and a button. Assume that
our textfield has focus, and the InputVerifier.verify() method always
returns false.
1. They don't prevent me from clicking on a button, checkbox, or combobox.
In fact, the behavior is pitiful. First the InputVerifier fires and I get
notified of a validation problem in my textfield. Then the mouseclick on any
of the other components gets fired. These components then respond to the
mouseclick event! What's up with that? True to form, the focus does not
shift from the textfield, but that is small consolation if you just
submitted the data, or closed the dialog.

2. How does one assign an InputVerifier to an *non-editable* JComboBox? Now
one may argue that there is not a need to make sure that a combobox
selection needs to be verified, but there are circumstances where two or
more combos need their selections to jive.

Note: #2 is not my main focus (ha), but #1 renders this potentially useful
construct completely useless.

thanks,
jim



Source
======
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JPanel {
    static JLabel status = new JLabel();

    public Test() {
  super();
  setupGUI();
    }

    protected void beep() {
        Toolkit.getDefaultToolkit().beep();
    }

    protected void setupGUI() {
        String[] items = { "Item 0", "Item 1", "Item 2", "Item 3"};

        JTextField jtxtField1 = new JTextField("Please enter a value");
        JTextField jtxtField2 = new JTextField("A different value");
        JCheckBox jchkBox = new JCheckBox("This is a test");
        JComboBox jcboBox = new JComboBox(items);
        JButton jbutton = new JButton("Do Something");

        this.add(jtxtField1);
        this.add(jtxtField2);
        this.add(jcboBox);
        this.add(jchkBox);
        this.add(jbutton);

        // JTextField Verifier - Must enter 10 characters or less
        jtxtField1.setInputVerifier(new InputVerifier() {
            public boolean verify(JComponent input) {
                JTextField jtxtField = (JTextField)input;
                if (jtxtField.getText().length() > 10) {
                    status.setText("Text field cannot contain more " +
                        "than 10 characters.");
                    beep();
                    return false;
                }
                return true;
            }
        });

        jbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                status.setText("Button pressed!");
            }
        });
    }

    public static void main(String[] args) {
  JFrame f = new JFrame();
  Test t = new Test();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.getContentPane().setLayout(new BorderLayout());
  f.getContentPane().add(t, BorderLayout.CENTER);
  f.getContentPane().add(status, BorderLayout.SOUTH);
  f.setBounds(0,0,300,200);
  f.setVisible(true);
    }
}
Technical Details
Message-ID: <39eda692$1_2@dnews>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,a228099bd5ee00b0,start
X-Google-Attributes: gid111d45,public
From: "James Cook" <jimcook.remove.for.spam@iname.com>
Subject: InputVerifier Dilemas
Date: 2000/10/18
Message-ID: <39eda692$1_2@dnews>
X-Deja-AN: 682886419
X-Trace: 18 Oct 2000 06:33:06 -0800, 156.63.133.8
X-MSMail-Priority: Normal
X-Priority: 3
Newsgroups: borland.public.jbuilder.java.swing
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400