Permlink Replies: 42 - Pages: 3 [ Previous | 1 2 3 | Next ] - Last Post: 21 Nov 16, 16:01 Last Post By: JeffTucker
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 13:32   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
I've now fitted the upcoming jAlbum 13.7 with a new class:
Thanks David for this new fieldtype.
I did test it, but there is still a problem:

I tested it with the next code in onload.bsh:
JTextField folderboxHeight = new JIntegerField("",3);
 
indexThumbs.add("tab",new JLabel("Folder box height:"));
indexThumbs.add("tab",ui.folderboxHeight);
and in common.css
String folderboxHeightpx= folderboxHeight + "px";
If I enter 140 in the Folderbox height field, it displays 140 as expected.
But if I make next an album with this setting, it displays 140140 in that field and with next Make 140140140

Negative numbers are not accepted, which is no problem for me, but integers can be negative, so is something like JposIntegerField not a better name?

Another very useful extension could be a 3th and 4th argument with the allowed minimum and maximum value.
If these arguments are absent, you could also allow negative numbers. If a third argument with value 0 is given, it allows only positive integers.

Edited by: AndreWolff on 17-Nov-2016 13:40
JeffTucker

Posts: 8,039
Registered: 31-Jan-2006
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 14:33   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
Gee, how about another parameter that defines the interval between acceptable values, like only even multiples of 5? And maybe provide a little up-and-down arrow next to the field so that you don't actually have to type in a number?
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 15:44   in response to: JeffTucker in response to: JeffTucker
  Click to reply to this thread Reply
jGromit wrote:
David, this is like giving a drink to an alcoholic - it's enabling behavior.

The way to accept integer values from the UI is to use a JSpinner. The way to accept text values from the UI is to use a JSmartTextField. Why do we need a new method to allow a confused developer to do it wrong, but then try to fix things on the fly?


I figured this solution has the advantage of allowing empty values
JeffTucker

Posts: 8,039
Registered: 31-Jan-2006
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 15:49   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
I figured this solution has the advantage of allowing empty values

I found an interesting little kludge online that will let you do that with a JSpinner, by simply making the value invisible if, for example, you spin it down to zero. Hit the up-arrow, and the values start magically reappearing. The zero value is still there, of course, so the skin needs to check for it (if it's zero, don't use it). Haven't tried it, but it looks like it would work.

https://coderanch.com/t/343622/java/JSpinner-show-default

So, instead of trying to turn text into numbers, how about a method that provides for "invisibility" at a given JSpinner value?
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 17:12   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
AndreWolff wrote:
But if I make next an album with this setting, it displays 140140 in that field and with next Make 140140140

Negative numbers are not accepted, which is no problem for me, but integers can be negative, so is something like JposIntegerField not a better name?


Thanks for spotting that. I've now updated the class. It's now called JNumberField and also has two methods to set whether to allow negative numbers or decimals too. These setter methods return the JNumberField itself so they can be chained for ease of use:
JNumberField temperature = new JNumberField(10).setAllowNegative(true).setAllowDecimals(true);
RobM

Posts: 3,815
Registered: 4-Aug-2006
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 17:29   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
AndreWolff wrote:
But if I make next an album with this setting, it displays 140140 in that field and with next Make 140140140

Negative numbers are not accepted, which is no problem for me, but integers can be negative, so is something like JposIntegerField not a better name?

Thanks for spotting that. I've now updated the class. It's now called JNumberField and also has two methods to set whether to allow negative numbers or decimals too. These setter methods return the JNumberField itself so they can be chained for ease of use:

JNumberField temperature = new JNumberField(10).setAllowNegative(true).setAllowDecimals(true);
But why are you using 'beep', shouldn't that be ribbit()? ;)
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 19:20   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
I've now updated the class. It's now called JNumberField and also has two methods to set whether to allow negative numbers or decimals too. These setter methods return the JNumberField itself so they can be chained for ease of use:
JNumberField temperature = new JNumberField(10).setAllowNegative(true).setAllowDecimals(true);
I did download jalbum-core again, but I see no difference with the previous version.

How can I test JNumberField?

Edit
I see a method public JNumberField(Document doc, String text, int columns)
what is the first argument used for?

Edited by: AndreWolff on 17-Nov-2016 19:22
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 19:50   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
AndreWolff wrote:
davidekholm wrote:
I've now updated the class. It's now called JNumberField and also has two methods to set whether to allow negative numbers or decimals too. These setter methods return the JNumberField itself so they can be chained for ease of use:
JNumberField temperature = new JNumberField(10).setAllowNegative(true).setAllowDecimals(true);
I did download jalbum-core again, but I see no difference with the previous version.

How can I test JNumberField?

Edit
I see a method public JNumberField(Document doc, String text, int columns)
what is the first argument used for?

All info you need in order to use it is available in the one-liner above. I've simply made available the same constructors as JTextField has. As for the constructor that takes a Document as argument, it enables a JTextField to be created using an external document model (two JTextField instances ca

Edited by: AndreWolff on 17-Nov-2016 19:22
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 19:50   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
AndreWolff wrote:
davidekholm wrote:
I've now updated the class. It's now called JNumberField and also has two methods to set whether to allow negative numbers or decimals too. These setter methods return the JNumberField itself so they can be chained for ease of use:
JNumberField temperature = new JNumberField(10).setAllowNegative(true).setAllowDecimals(true);
I did download jalbum-core again, but I see no difference with the previous version.

How can I test JNumberField?

Edit
I see a method public JNumberField(Document doc, String text, int columns)
what is the first argument used for?


All info you need in order to use it is available in the one-liner above. I've simply made available the same constructors as JTextField has. As for the constructor that takes a Document as argument, it enables a JTextField to be created using an external document model (two JTextField instances can share the same document model for instance)
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 21:04   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
Thanks for spotting that. I've now updated the class. It's now called JNumberField and also has two methods to set whether to allow negative numbers or decimals too. These setter methods return the JNumberField itself so they can be chained for ease of use:
JNumberField temperature = new JNumberField(10).setAllowNegative(true).setAllowDecimals(true);
I did test the following declarations:
JTextField folderboxHeight = new JNumberField ("",3);
JTextField folderboxHeight = new JNumberField ("",3).setAllowNegative(true); 
JTextField folderboxHeight = new JNumberField ("",3).setAllowNegative(false);
JTextField folderboxHeight = new JNumberField ("",3).setAllowNegative(false).setAllowDecimals(true); 
JTextField folderboxHeight = new JNumberField ("",3).setAllowNegative(true).setAllowDecimals(true);
and the tests did give the expected result.

But it is strange that I can still use the declaration
JTextField folderboxHeight = new JIntegerField("",3);
I did expect to see there an error message, because I thought you should remove that possibility.
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 17 Nov 16, 21:55   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
AndreWolff wrote:

But it is strange that I can still use the declaration
JTextField folderboxHeight = new JIntegerField("",3);
I did expect to see there an error message, because I thought you should remove that possibility.

It should be gone really, but perhaps one of my builds left it over. I've made a clean rebuild now.
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: Building block: validate function for an integer in a textbox
Posted: 18 Nov 16, 00:08   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
It should be gone really, but perhaps one of my builds left it over. I've made a clean rebuild now.
Yes it is gone now, I get an error message.

I should like to apply your new class now in the Slide Show 4 and PhotoSwipe skin, but if I do that I create a problem: the skins can no longer be used in jAlbum version 13.6 and earlier versions.

I wonder is there pseudo construction possible to use as declaration in onload.bsh
 JTextField folderboxHeight = new JNumberField ("",3);
for jAlbum version > 13.6 and
 JTextField folderboxHeight = new JSmartTextField ("",3);
for earlier versions?
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 18 Nov 16, 00:30   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
Attachment UIComponents.jar (6.2 KB)
AndreWolff wrote:

I wonder is there pseudo construction possible to use as declaration in onload.bsh
 JTextField folderboxHeight = new JNumberField ("",3);
for jAlbum version > 13.6 and
 JTextField folderboxHeight = new JSmartTextField ("",3);
for earlier versions?

If you wish to support older jAlbum versions, then add a "lib" folder to your skin and put the attached file within it. It contains the implementation of the JNumberField class.

Edited by: davidekholm on 18-Nov-2016 12:27
(Right-aligned the numbers)
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: Building block: validate function for an integer in a textbox
Posted: 18 Nov 16, 09:12   in response to: davidekholm in response to: davidekholm
  Click to reply to this thread Reply
davidekholm wrote:
If you wish to support older jAlbum versions, then add a "lib" folder to your skin and put the attached file within it. It contains the implementation of the JNumberField class.
Yes that works great!

Just for my understanding: I assume there is no conflict if you release version 13.7 with the same class in the jAlbum core. But which version is used in that case, the code in the skin or the code in jAlbum core?

Thanks for this new class, that is much better as the solution I proposed at the start of the thread!

Edit
BTW is it not a good idea to right-justify the text in a JNumberField as you do in a SpinnerNumberModel field (see enclosed screenshot)?

Edited by: AndreWolff on 18-Nov-2016 12:03
davidekholm

Posts: 3,442
Registered: 18-Oct-2002
Re: Building block: validate function for an integer in a textbox
Posted: 18 Nov 16, 12:33   in response to: AndreWolff in response to: AndreWolff
  Click to reply to this thread Reply
AndreWolff wrote:
davidekholm wrote:
If you wish to support older jAlbum versions, then add a "lib" folder to your skin and put the attached file within it. It contains the implementation of the JNumberField class.
Yes that works great!

Happy to hear :-)

Just for my understanding: I assume there is no conflict if you release version 13.7 with the same class in the jAlbum core. But which version is used in that case, the code in the skin or the code in jAlbum core?

It's generally a bad practice to mix classes with the same name and package name (hard to tell which one is picked sometimes), but if we can keep the implementation like this now, so they don't diverge, then there shouldn't be any surprises. Your added UIComponents.jar should basically be disregarded if jAlbum's core lib provides an implementation. Technically a Java class is identified by the following: Class loader + package name + class name. When looking for classes, Java asks the core class loader first, and then skin specific class loaders.

Thanks for this new class, that is much better as the solution I proposed at the start of the thread!

:-)

Edit
BTW is it not a good idea to right-justify the text in a JNumberField as you do in a SpinnerNumberModel field (see enclosed screenshot)?

You're right. I've now updated the beta jalbum-core.jar and also updated UIComponents.jar to provide a right-aligned version.
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums