Results 1 to 12 of 12
  1. #1
    Grom is offline Member
    Join Date
    Aug 2008
    Posts
    20
    Rep Power
    0

    Default Few Questions about java

    Hello Java Forum,

    I am trying to build simple calculator, and I need to make my inputs checked as soon as they are typed in TextField.

    What command should I use in order to get the TextField to check if typed character is number or letter. I need just numbers, and only positive. (double n > 0)
    Is there other command for clearing the TextField than this: NameTextField.setText(""); ?
    How to clear TextArea?
    How to add next line into TextArea? I only get one, at the top. How do I change font colour?

    I also have problem with decimal separator. How do I 'translate' string to double?

    Thanks for now!

    Kind regards Grom

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    To check what is typed into a text area, you could use a DocumentListener
    The Character class has methods for test characters types.
    Or use a parse method from Integer or Double and catch any exceptions.
    What's the problem with setText("") to clear field?
    Read API doc for TextArea. There is method to append text to existing text.
    Read the API doc for Double and for String to see conversion methods available.

  3. #3
    Grom is offline Member
    Join Date
    Aug 2008
    Posts
    20
    Rep Power
    0

    Default

    Hello Norm! Lightning fast!

    I looked into TextField Events, but i could not find DocumentListener.

    e.g. In TextFieldOne I input 20. In TextFieldTwo should not be bigger number than TextFieldOne (20), but bigger than zerro.
    So if I input 100 in TextFieldTwo, the font of TextFieldTwo should turn bold and red. And if I type a letter (or two, I expressed myself wrong when I wrote 'character'), the TextField should 'clear' itself. I have came up with this code:

    private void LengthTextFieldCaretUpdate(javax.swing.event.Caret Event evt) {
    // TODO add your handling code here:

    try {
    String number = LengthTextField.getText();
    Vejica.StringToDouble(number);
    statusLabel.setText(String.valueOf(number));
    }
    catch (Exception e) {
    LengthTextField.setText("");
    }

    }
    But this does not 'clear' text in Exception on first run, but on second.

    As you can see, I also use class Vejica, (decimal separator), where I change string to double:
    public class Vejica {

    public static double StringToDouble(String stringNum) {

    String decimalCharacter = "."; //HERE I'd like to change "." for something 'Sure'

    stringNum = stringNum.replace(".", decimalCharacter);
    stringNum = stringNum.replace(",", decimalCharacter);

    return Double.parseDouble(stringNum);


    }
    }

    could I bother you for links
    -to API doc for TextArea.
    -the API doc for Double and for String


    Kind regards, Grom

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    DocumentListener is in the API doc. See below.
    Are you using Swing classes such as JTextArea or AWT classes?
    Look at the setInputVerifier method. Its documentation shows an example of how to use it to verify the contents of a textfield.

    I've downloaded the API. My links are to local files.
    file:///E:/Java/Java-1_6_0/docs/api/java/lang/Double.html
    file:///E:/Java/Java-1_6_0/docs/api/java/awt/TextArea.html
    If you are going to program Java, YOU MUST know how to find class and method definitions in the API doc. Either download the full API doc or set a bookmark to it in your browser.

    You'll have to show more of your code for anyone to understand what it is doing.
    Comments on what you've shown:
    Vejica.StringToDouble(number);
    What happens to the value returned by the above method?
    It needs to be assigned to a variable if you want to save it.
    catch (Exception e) {
    LengthTextField.setText("");
    }
    Here you should print out the type of error so you can fix your code if it is not what you expect. Otherwise only catch the type of exception you expect. What type of exception do you expect here?
    Last edited by Norm; 09-06-2008 at 06:28 PM.

  5. #5
    Grom is offline Member
    Join Date
    Aug 2008
    Posts
    20
    Rep Power
    0

    Default

    Good Morning Forum,

    Hello Norm,

    I am not quite sure which classes I use (Swing or AWT), but as I lay buttons, labels and TextFields/Area I get their names as jButton1, jLabel1, jTextArea1... - so I use swing...?

    And thanks for links. I searched File System for double.html and TextArea.html and it turns out that I've downloaded jdk-6-doc.zip over two weeks ago. Unpacked and all... , but I switched to linux (ubuntu) a month ago, and I am still coping with simple installations...

    So now I have api and I look into swing thing.

    And here is the link to zipped file of my NetBeans 6.1 project here: beezgetz dot com/NetBeans/Truss03.zip (I am still not allowed to post links or pictures)

    Problem:
    the jTextField does not 'clear' itself when letter is typed into it. Look first three jTextFields tool tips for 'problem'.

    Kind regards, Grom

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    jTextField does not 'clear' itself when letter is typed into it
    Why would it do that? When would it know to clear the old and to keep what was typed in? Is it supposed to hold only a single character?

    Your zip file was huge. I don't use Netbeans and don't know where in the folder to look for the file you are talking about.
    Please post the code containing the problem.
    Last edited by Norm; 09-07-2008 at 02:41 PM.

  7. #7
    Grom is offline Member
    Join Date
    Aug 2008
    Posts
    20
    Rep Power
    0

    Red face

    Hello!

    380 K is huge?
    I am creating a calculator, and I need TextFields to input numbers to calculate.
    If, by mistake or deliberately, user should type a letter or anything but numbers, the textField should 'clear' itself. Not set invisible, just clear the textField.

    here is the picture:

    http: //shrani.si/f/a/mT/4hydyIgw/screenshot-truscalcpath-.png


    ok, here is the main code:

    Java Code:
    //    Here I call my Truss Class, so I can use it 
        // through the whole program
        Truss2Class Truss = new Truss2Class();
    
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    
          try
        {
            
             VnosV_class();
            // double a = Double(String WeightTextField.getText());
             
             // I display Truss Class content
             TrussClassNameLabel.setText(Truss.T_name());
             TrussClassWeightLabel.setText(String.valueOf(Truss.T_weight()));
             TrussClassLengthLabel.setText(String.valueOf(Truss.T_length()));
             TrussClassSpanLabel.setText(String.valueOf(Truss.T_span()));
             TrussClassLeftEndLabel.setText(String.valueOf(Truss.T_leftEnd()));
             TrussClassRightEndLabel.setText(String.valueOf(Truss.T_rightEnd()));
             jTextArea1.append(String.valueOf(Truss.T_weight())); // now, this is NOT adding text into next line...
             jTextArea1.append(String.valueOf(Truss.T_length())); // I need to be added in next line.
             jTextArea1.append(String.valueOf(Truss.T_span()));
             jTextArea1.append(String.valueOf(Truss.T_leftEnd()));
             jTextArea1.append(String.valueOf(Truss.T_rightEnd()));
             
             
        }
        catch (Exception e)  {
            
        }    
    }                                        
    
                                           
    private void VnosV_class () { 
        
        // Here I try to check if input is correct (one string, rest should be double)
        try {
            
            Truss.setT_name(NameTextField.getText());
            Truss.setT_weight(Vejica.StringToDouble(WeightTextField.getText())); // I call Vejica Class
            Truss.setT_length(Vejica.StringToDouble(LengthTextField.getText())); // to set it in right decimal character
            Truss.setT_span(Vejica.StringToDouble(SpanTextField.getText()));
            Truss.setT_leftEnd(Vejica.StringToDouble(LeftEndTextField.getText()));
            Truss.setT_rightEnd( Vejica.StringToDouble(LengthTextField.getText()) - Vejica.StringToDouble(SpanTextField.getText()) - Vejica.StringToDouble(LeftEndTextField.getText()) );
        } 
        
        catch (Exception e) { //  in case something goes wrong:
            Truss.setT_name("");
            Truss.setT_weight(0);
            Truss.setT_length(0);
            Truss.setT_span(0);
            Truss.setT_leftEnd(0);
            Truss.setT_rightEnd(0);        
        }   
        
    }
    
    private void WeightTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {      
        try { 
            String number = WeightTextField.getText();  
            Vejica.StringToDouble(number);
            WeightLabel.setText(String.valueOf(number) + " Weight");        
            //WeightTextField.setInputVerifier(arg0);
            
        }
        catch (Exception e) {
            WeightTextField.setText("");
        } 
    }                                                                               
                                                  
    
    private void LengthTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {   
    
          try {
            String number = LengthTextField.getText();
            Vejica.StringToDouble(number);
            LengthLabel.setText(String.valueOf(number) + " Length");
        }
        catch (Exception e) {
            LengthTextField.setText("");
        }
        
    }                                           
    
    private void SpanTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {      
    
          try {
            String number = SpanTextField.getText();
            Vejica.StringToDouble(number);
            double num = Double.parseDouble(number);
            
             String biger = LengthTextField.getText();
            Vejica.StringToDouble(biger);
            double big = Double.parseDouble(biger);
            
            if ( num >  big ) {
                SpanTextField.setText("");
                SpanLabel.setText("Span");
            }
            else {
                SpanLabel.setText( (big - num) + " Span" );
            }
            
            
            
        }
        catch (Exception e) {
            SpanTextField.setText("");
        }
    }                                         
    
    private void LeftEndTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {
    
         try {
            String number = LeftEndTextField.getText();
            Vejica.StringToDouble(number);
            LeftEndLabel.setText(String.valueOf(number) + " Left End");
            
        }
        catch (Exception e) {
            LeftEndTextField.setText("");
        }
        
    }


    Here is Vejica Class, that I use to check for input (string to double)

    Java Code:
    public class Vejica {
            
            public static double StringToDouble(String stringNum) {
                 
                String decimalCharacter = "."; 
    //            string decimalCharacter = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
    
    
    /**
     *This class I made in order to 'transform/translste' 
     * string to double. I replace whatever (well just comma and fullstop) 
     * decimal separator with fullstop(.) .
     * I have in C# this code:
     * string decimalCharacter = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                
    */
    
                stringNum = stringNum.replace(".", decimalCharacter);
                stringNum = stringNum.replace(",", decimalCharacter);
               
                    return Double.parseDouble(stringNum);
             
                
            }
    }


    And here is my Truss Class, where I store the input

    Java Code:
    public class Truss2Class {
    
        
     /**
     *Here are stored 
      * calculation data
     *(operants)
     */ 
        private String NameTruss;
        private double WeightTruss;
        private double LengthTruss;
        private double SpanTruss;
        private double LeftEndTruss;
        private double RightEndTruss;
        
        
        //here I can get my calculation operants
        public String T_name() {
            return NameTruss;
        }
        public double T_weight() {
            return WeightTruss;
        }
        public double T_length() {
            return LengthTruss;
        }
        public double T_span() {
            return SpanTruss;
        }
        public double T_leftEnd() {
            return LeftEndTruss;
        }
        public double T_rightEnd() {
            return RightEndTruss;
        }
        
        //here I can set new value 
        //of my operants
        public void setT_name(String name) {
            NameTruss = name;
        }
        public void setT_weight(double weight) {
            WeightTruss = weight;
        }
        public void setT_length(double length) {
            LengthTruss = length;
        }
        public void setT_span(double span) {
            SpanTruss = span;
        }
        public void setT_leftEnd(double leftEnd) {
            LeftEndTruss = leftEnd;
        }
        public void setT_rightEnd( double rightEnd) {
            RightEndTruss = rightEnd;
        }
    }

    Hope you could help me now... :o

    Kind regards, Grom

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    More than 100K for a java file is large. The largest one I have in my testing folder is 28K.
    need TextFields to input numbers to calculate.
    If, by mistake or deliberately, user should type a letter or anything but numbers, the textField should 'clear' itself. Not set invisible, just clear the textField.
    Can you make a one page simple program that demonstrates your problem. There appears to be a lot of extra stuff in what you've posted that is NOT related to the text field problem.
    Just a simple GUI with the textfield and the listeners etc that you need to solve your problem. Nothing else.

    Where are the textfields defined in the code you posted?

    Did you lookup DocumentListeners? They allow you to monitor and change what is typed in.
    You use getDocument() to get a Document to which you can add a listener.
    Last edited by Norm; 09-07-2008 at 07:46 PM.

  9. #9
    Grom is offline Member
    Join Date
    Aug 2008
    Posts
    20
    Rep Power
    0

    Default

    Hello Norm,

    I use NetBeans. For every new project it creates a folder, where are , I guess, all things it needs saved.
    I made new project, one window, Two jTextFields, one jButton and one jTextArea.

    It is supposed to get a string and a double from the user.
    For 'double' input it should check if it is really a number,
    check for decimal separator if there is any,
    and if the input is not a number or decimal separator, it should delete it.

    Clicking on jButton it should save that input in Store Class
    and displays it in TextArea.

    Again, this zip is 364 KB big, huge I guess.

    Here is link to zip file :
    beezgetz.com/NetBeans/Norm01.zip

    Here is picture of app:
    htt p://shrani.si/f/j/bq/3kjm9wyy/screenshot-basic-applica.png

    I did look into those api documentation, and I am getting familiar with it, but so far not much...
    'You use getDocument() to get a Document to which you can add a listener' - I need to check this out,

    Kind regards, Grom

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Can you make a one page simple program that demonstrates your problem?
    About two screens worth of code. You don't need to post the whole project.
    The source for your program is somewhere inside of Netbeans. Use a search to find the .java file.

    I mislead you on DocumentListener. However if you had read the Tutorial about DocumentListeners you would have found a couple of ways to verify text field input. Here are 3:
    Java Code:
    tf.setInputVerifier(...
    ((AbstractDocument)doc).setDocumentFilter(...
    
    tf.setDocument(new IntegerOnlyDoc());
     ...
    class IntegerOnlyDoc extends PlainDocument {
          public void insertString(...
    Last edited by Norm; 09-08-2008 at 12:15 AM.

  11. #11
    Grom is offline Member
    Join Date
    Aug 2008
    Posts
    20
    Rep Power
    0

    Default

    Hello Java Forum,

    Ola Norm, I am more of weekend programmer, since I have job, duties,... but no kids yet, so I can use weekends for 'programming'

    I have made my application with Visual C#, please take a look at www beezgetz.com/Tour.aspx . Now I want to translate it to java, so Mac users can use it.

    Here is the picture of my java application, where jTextField checks each typed 'character' of string (as it is typed) if it is really a number (double), and if it is not, to clear the jTextField.
    http ://shrani.si/f/2b/VV/d3SvkCl/application.png


    Here is code
    Java Code:
    private void DoubleTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {
    	
         try { 
            String number = DoubleTextField.getText();  // I get string from TextField
            DecimalSeparator.StringToDouble(number);    // I run it through DecimalSeparatot Class to make it double, or catch (Exception e).
            DoubleLabel.setText(String.valueOf(number) + " double");   //I run some action
            
        }
        catch (Exception e) {
            DoubleTextField.setText("");
        } 
        
        
    }

    Here is called Class:

    Java Code:
    public class DecimalSeparator {
     public static double StringToDouble(String stringNum) {
                 
                String decimalCharacter = "."; 
                stringNum = stringNum.replace(".", decimalCharacter);
                stringNum = stringNum.replace(",", decimalCharacter);
               
                    return Double.parseDouble(stringNum);
             
                
            }
    }



    I am getting familiar with api, but I need some time to make it search the 'right stuff'


    Kind regards, Grom

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    DecimalSeparator.StringToDouble(number);
    The method called here returns a value but your code doesn't use it, so the value of number won't be changed. Change your code to receive the value returned:
    double numberVal = DecimalSeparator.StringToDouble(number);

    If you are not sure what is happening to the values of variables, add System.out.println() statements to see.

Similar Threads

  1. 2 simple java questions
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 09:35 AM
  2. Replies: 0
    Last Post: 01-24-2008, 07:53 PM
  3. A few questions about Java and design
    By ldb88 in forum New To Java
    Replies: 4
    Last Post: 12-07-2007, 12:51 AM
  4. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •