0

Ok, I am trying to create a calculator type program that will solve for the extrema of the function that I input. In order to do this, I have to set the interval of which the function is on. This means that I have three different text fields, one for the equation, and two for the interval.

So, I can successfully input numbers into my equation field, but trying to input into the other two brings up problems. I know that if I click into either of the two text fields that it will gain focus. If I try and set the new text of the field by checking if it has focus or not, I never get a result because clicking a button always changes focus to the button.

TL;DR

How do I figure out which text field previously had focus so I can know to put the numbers into it.

EDIT: If there is a better way to input altogether, I would be very happy to know what it is.

cblair
  • 1
  • 3
    I'm confused -- why is focus even important in the first place? Won't the user press the button only after entering all info? Won't your code prevent the button from doing anything (or even make it disabled) if all fields are not properly filled? This seems like an example of [the X Y problem](http://www.perlmonks.org/index.pl?node_id=542341). – Hovercraft Full Of Eels Dec 05 '12 at 23:33
  • No, the buttons are for entering numbers into the fields. Of course they can always type them, but I'm adding the buttons just for functionality. – cblair Dec 06 '12 at 00:41
  • *"I'm adding the buttons just for functionality."* Does not seem to be functional at the moment. Personally, I'd use a [`JSpinner`](http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html) for the numbers. – Andrew Thompson Dec 06 '12 at 04:14

1 Answers1

2

Your question seems an example of the X Y problem where you concentrate on focus as if it were the primary issue when the real issue it seems (to me any way) is to make sure that the user has input all data before doing calculations.

I suggest not worrying about focus. Use DocumentListeners to check if the JTextFields are empty or not, and disable the calculate button until all JTextFields have some text in them. I don't see that focus should really matter for this.

Edit
You state:

Ok, If you think you can help better with the situation. I have three text fields and buttons corresponding to numbers 0-9 and various functions. I want to be able to click in one text field, input my numbers from the buttons, click in another field, and then input my numbers there from the buttons I've made as well. How do I go about getting which text field the user clicks?

OK, that helps me understand your problem a bit better. One possible solution: make the JButtons non-focusable by calling myButton.setFocusable(false) on each one. Then pressing a button will not remove focus from a JTextField.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Ok, If you think you can help better with the situation. I have three text fields and buttons corresponding to numbers 0-9 and various functions. I want to be able to click in one text field, input my numbers from the buttons, click in another field, and then input my numbers there from the buttons I've made as well. How do I go about getting which text field the user clicks? – cblair Dec 06 '12 at 00:55
  • @cblair: see **Edit** to answer. – Hovercraft Full Of Eels Dec 06 '12 at 03:45
  • (Pete)[I think that is possible to determine last Focusable JComponents](http://stackoverflow.com/a/11276222/714968) – mKorbel Dec 06 '12 at 10:12
  • Ok, I think that should work then. I haven't ever seen the setFocusable method. I'll definitely try this and see how it works. Thanks! – cblair Dec 06 '12 at 23:44