-
April 11th, 2010, 06:38 PM
#1
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?
-
April 12th, 2010, 04:38 AM
#2
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.
-
April 12th, 2010, 03:23 PM
#3
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...
-
April 13th, 2010, 04:29 AM
#4
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.
-
April 13th, 2010, 09:19 AM
#5
Re: Swing code problem
The user will hit enter when he/she is done writing the expression
-
April 13th, 2010, 09:32 AM
#6
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.
-
April 13th, 2010, 10:53 AM
#7
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?
-
April 13th, 2010, 12:01 PM
#8
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?
-
April 13th, 2010, 04:17 PM
#9
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.
-
April 13th, 2010, 10:57 PM
#10
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
-
April 14th, 2010, 05:05 AM
#11
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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|