By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Noob Question - getFocusOwner not working?

P: 12
Ok, heres the deal, i'm using netbeans 5.5 to create a Rational calculator(1/2 + 2/3 = 5/6, so on so forth) anyways, I have looked up on google, on this site, everywhere, and I am apperently missing the big idea, I can't get getFocusOwner to work, what does it return, what's the object u call it with? heres what i've been trying, so far nothin's workin.

// When i hit the 1 button, i want to add the integer 1 to the end of the int's in
// the focus'd jTextField. I don't even think i'm doin the appending correctly, but
// thats a whole nother story that I am alot less worried about.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String temp = this.getFocusOwner().getText();
this.getFocusOwner().setText(temp + 1);
}

What i want in specific is, how do i find the focusowner, what variable does it return(ie jTextField1, jTextField2, and jTextField3 are textfields, does it return jTextField1, or does it return something else, such as text field 1?)
and how do i append an integer onto the end of the textfield's variables.

I know this is a simple question but I am about to lose it, i'm normally the one that just keeps pounding away tell i figure it out, but i've given up, and resorted to outside help. Thanks for the, sorry if it's already been "discussed" 100 times now, redirect me if it will answer my question fully.
Nov 7 '06 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
Ok, heres the deal, i'm using netbeans 5.5 to create a Rational calculator(1/2 + 2/3 = 5/6, so on so forth) anyways, I have looked up on google, on this site, everywhere, and I am apperently missing the big idea, I can't get getFocusOwner to work, what does it return, what's the object u call it with? heres what i've been trying, so far nothin's workin.

// When i hit the 1 button, i want to add the integer 1 to the end of the int's in
// the focus'd jTextField. I don't even think i'm doin the appending correctly, but
// thats a whole nother story that I am alot less worried about.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
String temp = this.getFocusOwner().getText();
this.getFocusOwner().setText(temp + 1);
}

What i want in specific is, how do i find the focusowner, what variable does it return(ie jTextField1, jTextField2, and jTextField3 are textfields, does it return jTextField1, or does it return something else, such as text field 1?)
and how do i append an integer onto the end of the textfield's variables.

I know this is a simple question but I am about to lose it, i'm normally the one that just keeps pounding away tell i figure it out, but i've given up, and resorted to outside help. Thanks for the, sorry if it's already been "discussed" 100 times now, redirect me if it will answer my question fully.
Read the docs.

you call it on a widow(JFrame etends Window). It returns a Component(JTextField extends Component).

you could therefore do
((JTextField)frame.getFocusOwner()).getText();
Nov 7 '06 #2

P: 12
See, i just tried it, and its saying jTextField isn't a variable, i want a generic code that turns out the owner of the focus(i've only got focusable on the 4 text fields.) so that when i hit the button it adds the value of the button i hit. and, read the docs, i've read about 10 tutorials and none of em haev worked, i spent about 1 hour making the rational class, 1 hour designing the buttons and about 10 trying to get the focusowner, i need a solid example, not some snippet that is incorrect.
Nov 7 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
See, i just tried it, and its saying jTextField isn't a variable, i want a generic code that turns out the owner of the focus(i've only got focusable on the 4 text fields.) so that when i hit the button it adds the value of the button i hit. and, read the docs, i've read about 10 tutorials and none of em haev worked, i spent about 1 hour making the rational class, 1 hour designing the buttons and about 10 trying to get the focusowner, i need a solid example, not some snippet that is incorrect.
Are you still using the code you posted above? Or did you change it to include the cast and the other parentheses? (Can you post both the new snippet and your error message?)
Nov 7 '06 #4

P: 12
I've given up, i just resulted in doing this, as i only have 4 textfields and 10 buttons, it's not gonna make a bit of difference(maybe the teacher might not like it so much, o well)


// button/ 0 /

private void jButton10ActionPerformed(java.awt.event.ActionEven t evt) {
boolean tf1 = jTextField1.isFocusOwner();
boolean tf2 = jTextField2.isFocusOwner();
boolean tf3 = jTextField4.isFocusOwner();
boolean tf4 = jTextField5.isFocusOwner();
if(tf1 == true)
{
jTextField1.setText(jTextField1.getText() + "0");
}
if(tf2 == true)
{
jTextField2.setText(jTextField2.getText() + "0");
}
if(tf3 == true)
{
jTextField4.setText(jTextField4.getText() + "0");
}
if(tf4 == true)
{
jTextField5.setText(jTextField5.getText() + "0");
}

So i got that for button's 1 - 10(0 - 9), its alright, ill just go have an office hour with my teacher and figure all this crap out. thanks for the help, sorry for being short with whoever posted that.
Nov 7 '06 #5

10K+
P: 13,264
See, i just tried it, and its saying jTextField isn't a variable, i want a generic code that turns out the owner of the focus(i've only got focusable on the 4 text fields.) so that when i hit the button it adds the value of the button i hit. and, read the docs, i've read about 10 tutorials and none of em haev worked, i spent about 1 hour making the rational class, 1 hour designing the buttons and about 10 trying to get the focusowner, i need a solid example, not some snippet that is incorrect.
And at this rate, you will take 10 years to learn the java language. That snippet is correct. I gave you a snippet because I thought you knew what you were doing and would know how to use it.
Nov 8 '06 #6

sicarie
Expert Mod 2.5K+
P: 4,677
Your code:
Expand|Select|Wrap|Line Numbers
  1. String temp = this.getFocusOwner().getText();
  2.  
Snippet given to you by r035198x:
Expand|Select|Wrap|Line Numbers
  1. ((JTextField)frame.getFocusOwner()).getText();
  2.  
I would suggest looking at the help given to you, and playing around with it a little bit, before saying you ran it and it didn't work.
Nov 8 '06 #7

P: 12
Ok, if you must. his code, and the errors i get with it

Expand|Select|Wrap|Line Numbers
  1. ((JTextField)frame.getFocusOwner()).getText();
  2.  
  3. cannot find symbol
  4. symbol: class JTextField
  5. location: class FractionCalculator.FractionCalculator
  6.  
  7. cannot find symbol
  8. symbol: variable frame
  9. location: class FractionCalculator.FractionCalculator
now my code(adapted, i think correctly, but apparently not)
Expand|Select|Wrap|Line Numbers
  1. ((jTextField)FractionCalculator.getFocusOwner()).getText();
  2.  
  3. cannot find symbol
  4. symbol: class JTextField
  5. location: class FractionCalculator.FractionCalculator
  6.  
  7. non-static method getFocusOwner() cannot be referenced from a static context
as i may have said before, it is much easier for me to learn from code, not generic examples, like lets say:

what the class name is for JTextField and frame is, like say when you initialize it, do they still stay JTextField and frame, or am i supposed to change them. as i said before, i'm a beginner, using generic stuff on me and me knowing what it's referenced to is a strech from the truth. i need examples. If i need to, i'll post my fractioncalculator.java somehow or put it on rapidshare or somethin, maybe send it to some1's email if thats allowd??
Nov 8 '06 #8

P: 12
here's the full code(for netbeans, the src files are inside it)

Fraction Calculator

sorry if this isn't considered helpful resources(it is to me:)) thanks for the help.
Nov 8 '06 #9

sicarie
Expert Mod 2.5K+
P: 4,677
It would probably be helpful to have the full code.

What do your import statements look like? (import java.swing.text.*)?
Nov 8 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.