gui problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sugard
    New Member
    • Aug 2007
    • 50

    gui problem

    I have this thing that I need to accept information from a user. I have a jTextField for where the user can place the info needed (example entering his name.) My problem is this.. I need to check whether the user has placed any text in the field. Since if he did not place any text I want an informtion message to be displayed on the same screen.

    I created an empty jlabel so that it would be written there.. Though i did not succeed! I tried to check if the jTextField is empty by using if (jTextField.get Text() == null) {
    jLabel.setText( "Name cannot be left blank");
    }

    Can please someone help me.. I thank you in advance
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sugard
    I have this thing that I need to accept information from a user. I have a jTextField for where the user can place the info needed (example entering his name.) My problem is this.. I need to check whether the user has placed any text in the field. Since if he did not place any text I want an informtion message to be displayed on the same screen.

    I created an empty jlabel so that it would be written there.. Though i did not succeed! I tried to check if the jTextField is empty by using if (jTextField.get Text() == null) {
    jLabel.setText( "Name cannot be left blank");
    }

    Can please someone help me.. I thank you in advance

    Use [CODE=java]jTextField.getT ext().equals("" )[/CODE]
    instead.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Another approach would be to enable the button only when the the text field holds some text:

      [CODE=Java]import java.awt.*;
      import javax.swing.*;
      import javax.swing.eve nt.*;

      class Enabler implements DocumentListene r {
      private JComponent target;

      public Enabler(JCompon ent target) {
      this.target = target;
      }

      @Override
      public void changedUpdate(D ocumentEvent e) {
      adjustTarget(e) ;
      }

      @Override
      public void insertUpdate(Do cumentEvent e) {
      adjustTarget(e) ;
      }

      @Override
      public void removeUpdate(Do cumentEvent e) {
      adjustTarget(e) ;
      }

      private void adjustTarget(Do cumentEvent e) {
      target.setEnabl ed(e.getDocumen t().getLength() > 0);
      }
      }

      public class RequiredField implements Runnable {
      public static void main(String[] args) {
      EventQueue.invo keLater(new RequiredField() );
      }

      @Override
      public void run() {
      JFrame f = new JFrame("Require dField");
      JTextField field = new JTextField(10);
      JButton submit = new JButton("submit ");
      submit.setEnabl ed(false);
      field.getDocume nt().addDocumen tListener(new Enabler(submit) );
      Container cp = f.getContentPan e();
      cp.setLayout(ne w FlowLayout());
      cp.add(new JLabel("Name (required)"));
      cp.add(field);
      cp.add(submit);
      f.pack();
      f.setDefaultClo seOperation(Win dowConstants.EX IT_ON_CLOSE);
      f.setLocationRe lativeTo(null);
      f.setVisible(tr ue);
      }
      }[/CODE]

      Comment

      • Stubert
        New Member
        • Dec 2007
        • 20

        #4
        gui problem

        Hi there I was just wondering if you could explain what @Override does, I had a look around on Google but couldn't really come up with anything except that it overrides the method in the superclass, which understand. But are the methods you're using in a superclass?

        I know this is a bit off topic but I've not came across it before.

        Thanks
        - Stu
        Last edited by Stubert; Jan 3 '08, 09:46 PM. Reason: Can't see text

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Threads get dull if they can't go off-topic occasionally. That was the annotation Override:
          Annotations Override API

          This annotation is useful because it catches typos and accidental changes. For example, consider this code:

          [CODE=Java]class A {
          public void f(){}
          }

          class B extends A {
          public void f(){}
          }[/CODE]

          B overrides f. But was this on purpose or by mistake? And what if B had been written:
          [CODE=Java]
          class B extends A {
          public void f(int x){}
          }[/CODE]
          B is no longer overriding f -- but did I mean to? If I add an annotation, the compiler will warn me of my mistake:
          [CODE=Java]
          class B extends A {
          @Override
          public void f(int x){}
          }[/CODE]

          Comment

          • Stubert
            New Member
            • Dec 2007
            • 20

            #6
            Ahh I think I get it, the @Override annotation is used to tell the compiler that you DO want to override the method but if you have done something wrong, ie declared the method with an argument, it will warn you that you have done it incorrectly and will NOT infact be overriding the method you wanted to?

            - Stu

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by Stubert
              Ahh I think I get it, the @Override annotation is used to tell the compiler that you DO want to override the method but if you have done something wrong, ie declared the method with an argument, it will warn you that you have done it incorrectly and will NOT infact be overriding the method you wanted to?

              - Stu
              Exactly. In this thread, however I was using it pedantically, just to point out which methods I wrote were implementing methods of interface DocumentListene r.

              Comment

              Working...