CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2010
    Posts
    9

    Swing code problem

    public void jTextArea2_keyTyped(KeyEvent e)
    {

    if (e.getKeyChar() == '\n')
    {
    System.out.print("got a new line");

    }

    int Count = jTextArea2.getLineCount();
    int I_1,I_2=0; String expr = "";

    try
    {
    I_1 = jTextArea2.getLineStartOffset(Count-2);
    I_2 = jTextArea2.getLineEndOffset(Count-2);
    --I_2;



    expr = jTextArea2.getText(I_1, I_2-I_1);


    }
    catch(Exception ex){}



    Hello everyone,

    This is my code that is supposed to take text from the user typing it in and save it into a variable so I can send the expression to my parser class.

    I am new to swing so I am not sure why this is not working. I also need it to keep reading in expressions until the user is done entering them.

    It would go like this

    User enters x + 4 -4 then hits enter
    then the program would send this expression to my parser class and it would parse the equation

    after this I need the program to wait for another expression to be entered ...

    help please?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Swing code problem

    It looks like you're extracting a whole line every time a key is typed, are you trying to parse the whole line every time? why?

    If you could explain more clearly exactly what you're trying to achieve and what is going wrong, it would be easier to make suggestions.

    Doing more things faster is no substitute for doing the right things...
    S. R. Covey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2010
    Posts
    9

    Re: Swing code problem

    What the program is supposed to do is every time an expression is typed into the text pane it extracts ONLY the expression..

    So say i typed in x+4-2x as my expression

    Then it would extract this expression from the text pane and save it into the variable expr.

    so expr = x+4-2x (I only want to extract this from the GUI text pane)

    This is so i can call my parser class and do this Parser parser = new Parser("expr");

    Then after my program will parse the equation and output it to the GUI

    Then it will wait for another expression to be typed in...

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Swing code problem

    OK. How do you plan to tell that an expression is complete so you can parse it?

    If you cannot describe what you are doing as a process, you don't know what you're doing...
    W. E. Demin
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2010
    Posts
    9

    Re: Swing code problem

    The user will hit enter when he/she is done writing the expression

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Swing code problem

    In that case the first thing you should do is check to see if the pressed key is the enter key and if not return immediately.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Apr 2010
    Posts
    9

    Unhappy Re: Swing code problem

    Yes, I understand the logic of what has to happen but I am new to the swing library so I am not sure what the syntax of this code will be. Does anyone know?

  8. #8
    Join Date
    Apr 2010
    Posts
    9

    Re: Swing code problem

    public void jTextArea2_keyTyped(KeyEvent e)
    {

    if (e.getKeyChar() == '\n' || e.getKeyChar() == '\r')
    {
    System.out.print("got a new line");

    int Count = jTextArea2.getLineCount();
    int i1, i2 = 0;
    String expr = "";

    try
    {
    i1 = jTextArea2.getLineStartOffset(Count - 2);
    i2 = jTextArea2.getLineEndOffset(Count - 2);
    --i2;

    expr = jTextArea2.getText(i1,i2-i1);

    } catch (Exception ex) {return;}




    }
    }

    This is my updated code... It makes sense but every time I type in an expression and hit enter to goes into the Catch section of my try catch block.... Very weird does not make sense to me anyone see a noticeable error?

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Swing code problem

    The whole point of having exceptions is they tell what has gone wrong and where it has gone wrong. But this only works if you handle the exceptions properly which means if you aren't going to take decisive action then at the very least you need to call the exception's printStackTrace() method to dump the error message and stack trace to the console.

    I suggest you add such a call to your catch statement prior to returning, you will then see what the problem is. If you still can't figure it out post the full stack trace and we will look at it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  10. #10
    Join Date
    Apr 2010
    Posts
    9

    Re: Swing code problem

    Figured it out.

    public void jTextArea2_keyTyped(KeyEvent e)
    {

    try{

    int Count = jTextArea2.getLineCount();
    int i1, i2 = 0;
    String expr = "";


    i1 = jTextArea2.getLineStartOffset(Count - 2);
    i2 = jTextArea2.getLineEndOffset(Count - 2);
    --i2;

    expr = jTextArea2.getText(i1,i2-i1);

    if (e.getKeyChar() == '\n' || e.getKeyChar() == '\r')
    {
    System.out.print(expr);
    }

    } catch (Exception ex) {return;}
    }

    You must input all the characters while the user is inputing the expression. THEN when the enter key is hit your expression is done being constructed and you are finished....

    Got it all working. GUI Version of my derivative program. It parses the expression and takes the derivative of it... nice

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Swing code problem

    You must input all the characters while the user is inputing the expression. THEN when the enter key is hit your expression is done being constructed and you are finished....
    Are you sure about this? If you follow the execution path of your code then you will see that every time a letter is pressed you get the input text and unless it is a enter key you then throw it away - all your variables are local so there is no persistence of their values.

    And why are you still throwing away the exception information without printing the stack trace?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured