Java Forum / GUI / February 2004
JSpinner + JOptionPane, spinner keeps on spinning
Hans Van den fonteyne - 04 Feb 2004 10:17 GMT Hello,
I've written a little application in java which contains a JTextfield and a JSpinner. On the JTextfield I've added an inputverifier. When the users fills in a wrong value in the textfield, then a JOptionPane shows up with a warning. Now I've the following problem. When I fill in a wrong value in the JTextfield and next I push on the JSpinner's up or down button, then the JOptionPane apears to show the warning, but the JSpinner keeps spinning, instead of just spinning one value up or down it spins till it has reached the max or min value.
Does anyone know how I can solve this annoying problem?
grtz, Hans
Thomas Weidenfeller - 04 Feb 2004 10:29 GMT > Does anyone know how I can solve this annoying problem? Random guess (please show us complete source code http://www.physci.org/codes/sscce.jsp):
You mixed up something in the event handling, or Sun mixed up something in the event handling.
/Thomas
Hans Van den fonteyne - 04 Feb 2004 11:14 GMT If tried to make the code with my problem as short as possible. Problem: When I fill in a wrong number in the textfield (i.e. anything except a float between 5 and 30) and next I press the up or down button from the spinner, then the warning popup appears, but the spinner keeps spinning.
grtz and thx, Hans
Thomas Weidenfeller - 04 Feb 2004 11:23 GMT > If tried to make the code with my problem as short as possible. That's fine. And please come back once you have decided to actually SHOW us that short, complete code.
/Thomas
(do we have full moon today, or what?)
Hans Van den fonteyne - 04 Feb 2004 16:40 GMT > That's fine. And please come back once you have decided to actually > SHOW us that short, complete code. What are we funny. I just pressed the wrong button :-(. Now do you have any idea what might be the problem?
grtz, Hans
Andrew Thompson - 04 Feb 2004 17:07 GMT >> That's fine. And please come back once you have decided to actually >> SHOW us that short, complete code. > > What are we funny. I just pressed the wrong button :-(. We have to deal with a lot of people who think we possess a magic 'crystal ball' when it comes to their code.
I read your response and was thinking of giving you a small 'prod' to produce the SSCCE* as well. The difference is that I had more interesting things to do that instant and did not bother.
By contrast, Thomas was about a 100% more helpful.
[ And as an aside, I noticed your two posts were made at almost the same time. ]
> Now do you have any idea what might be the problem? I had a look at your, self contained example. Unfortunately I did not spot the problem. [ I have never used JSpinner ]
OTOH - fortunately, those that _might_ be able to solve your problem now have something definitive to work with.
I hope you get a solution, but my advice is, be a bit patient. You will probably get someone wandering along any moment that understands exactly what is happening here.
* I wrote, and am a great promoter of, the document on the SSCCE.
-- Andrew Thompson * http://www.PhySci.org/ Open-source software suite * http://www.PhySci.org/codes/ Web & IT Help * http://www.1point1C.org/ Science & Technology
Thomas Weidenfeller - 05 Feb 2004 08:03 GMT > What are we funny. I just pressed the wrong button :-(. > Now do you have any idea what might be the problem? Well, I had planned to give your code a try this morning. But guess what, I just figured out I have better things to do. It is your problem, not mine, and if you want to make it hard or unpleasent for people to help you, fine with me.
/Thomas
Hans Van den fonteyne - 05 Feb 2004 09:37 GMT >> What are we funny. I just pressed the wrong button :-(. >> Now do you have any idea what might be the problem? The reason I've posted this message was because you were very fast in giving comments on my wrong posted message. I've posted the wrong message on 12:14 and I've posted the corrected message on 12:17. But in this time you already posted a comment, which was, in my eyes, not very friendly. If you didn't gave that comment so fast, I would have had the time to cancel that message. Eveybody can make a fault, but you should at least give people time to correct them.
The reason I asked for a quick answer is because I'm having this problem on an assignment I'm making for school, which has to be handed over tomorrow.
> Well, I had planned to give your code a try this morning. But guess > what, I just figured out I have better things to do. It is your > problem, not mine, and if you want to make it hard or unpleasent for > people to help you, fine with me. > > /Thomas If you don't want to help me, then that's the way it has to be.
grtz, Hans
Hans Van den fonteyne - 04 Feb 2004 11:17 GMT Here is the a part of my code, I 've tried to make it as short as possible. Problem: When I fill in a wrong value in the textfield (i.e. anything except a float between 5 and 30) and next I click on the up or down button of the spinner, then a warning popup appears, but the spinner keeps spinning up or down until the maximum value or the minimum value is reached.
import java.awt.Toolkit; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel;
public class JSpinnerProblem extends JFrame { MyVerifier verifier = new MyVerifier(); JTextField text;
public JSpinnerProblem() { super(); getContentPane().setLayout(null); setSize( 100,100); text = new JTextField(); text.setBounds(5,5,80,20); getContentPane().add(text); //Verifier verifies if textfield contains a float between 5 and 30; text.setInputVerifier(verifier);
SpinnerNumberModel nightModel = new SpinnerNumberModel(13,5,30,0.1); JSpinner nightSpinner = new JSpinner(nightModel);
nightSpinner.setBounds(5,30,80,20); getContentPane().add(nightSpinner);
show(); }
public class MyVerifier extends InputVerifier { String message;
public MyVerifier() { super(); }
public boolean verify(JComponent input) { return checkField(input, true); }
private boolean checkField(JComponent input, boolean b) { if (input == text) { return checkText(b); } return true; }
private boolean checkText(boolean change){ boolean wasValid = true; float amount = 13;
//Parse the value. try { amount = Float.parseFloat(text.toString()); } catch (NumberFormatException pe) { message = "Invalid number format in text field"; return false; }
//Value was invalid. if ((amount < 5) || (amount > 30)) { wasValid = false; if (amount < 5) { message = "Number too small"; } else { //amount is greater than 30 message = "Number too large"; } }
//Whether value was valid or not, format it nicely. if (change && !wasValid) { text.setText(Float.toString(amount)); }
return wasValid;
}
public boolean shouldYieldFocus(JComponent input){ boolean inputOK = verify(input);
if (inputOK) { return true; }
//Beep and then tell whoever called us that we don't //want to yield focus. Toolkit.getDefaultToolkit().beep();
//Avoid possible focus-transfer problems when bringing up //the dialog by temporarily removing the input verifier. //This is a workaround for bug #4532517. input.setInputVerifier(null); //Pop up the message dialog. JOptionPane.showMessageDialog(null, //no owner frame message, //text to display "Invalid Value", //title JOptionPane.WARNING_MESSAGE);
//Reinstall the input verifier. input.setInputVerifier(this);
return false;
} }
public static void main(String[] args){ JSpinnerProblem window = new JSpinnerProblem(); }
}
Hans - 05 Feb 2004 10:06 GMT > amount = Float.parseFloat(text.toString()); I justed detected a little fault in the code.
This line has to be:
amount = Float.parseFloat(text.getText());
grtz, Hans
Christian Kaufhold - 04 Feb 2004 17:09 GMT > I've written a little application in java which contains a JTextfield and a > JSpinner. On the JTextfield I've added an inputverifier. When the users [quoted text clipped - 5 lines] > instead of just spinning one value up or down it spins till it has reached > the max or min value. Is this related to
http://developer.java.sun.com/developer/bugParade/bugs/4840869.html
?
Christian
Hans - 05 Feb 2004 10:05 GMT usenet@chka.de (Christian Kaufhold) wrote in news:0T10ttqtIkemNmc0 @uni.chka.de:
> http://developer.java.sun.com/developer/bugParade/bugs/4840869.html That's the one. Thank you for this link, I've just tested the suggested solution and it seems to work for me.
grtz, Hans
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|