This question is answered.


Permlink Replies: 16 - Pages: 2 [ 1 2 | Next ] - Last Post: 21-Aug-2016 13:34 Last Post By: jGromit
AndreWolff

Posts: 2,507
Registered: 14-Dec-2007
How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 14:31
 
  Click to reply to this thread Reply
I use next code in onload.bsh:
JComboBox defViewtime = new JComboBox(new Object[]
	 {"1 s", "3 s", "5 s", "8 s", "15 s", "25 s", "60 s" }
	);

This works OK, but now I want to set the default value to " 5 s".

I tried this:
JComboBox defViewtime = new JComboBox(new Object[]
	 {"1 s", "3 s", "5 s", "8 s", "15 s", "25 s", "60 s" }, "5 s"
	);

and this:
JComboBox defViewtime = new JComboBox(new Object[]
	 {"1 s", "3 s", "5 s", "8 s", "15 s", "25 s", "60 s" }, 2
	);

Butboth methods give a crash.

So my question is:
What is the correct value to set the default value of a combo-box?
monkeyboy

Posts: 464
Registered: 21-Jun-2008
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 14:59   in response to: AndreWolff in response to: AndreWolff
Correct
  Click to reply to this thread Reply
The Java 8 API docs for JComboBox can be found here.
There is no JComboBox constructor which takes a default value as a parameter. You'll need to use either setSelectedIndex or setSelectedItem.

Try either:
JComboBox defViewtime = new JComboBox(new Object[] {"1 s", "3 s", "5 s", "8 s", "15 s", "25 s", "60 s"});
defViewtime.setSelectedIndex(2);

... or:
JComboBox defViewtime = new JComboBox(new Object[] {"1 s", "3 s", "5 s", "8 s", "15 s", "25 s", "60 s"});
defViewtime.setSelectedItem("5 s");

Otherwise, you could use a SkinModel.java file to set types and default values.
Please note that if you do use a SkinModel.java file to set a default value for your defViewtime JComboBox, the value in the SkinModel.java file will override setSelectedIndex or setSelectedItem in onload.bsh.
jGromit

Posts: 31,948
Registered: 31-Jan-2006
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 14:59   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
Unless you use a SkinModel.java file, the only way to set the default value for a JComboBox is to change the sequence, since Java will always choose the first value in the list:
JComboBox defViewtime = new JComboBox(new Object[]
	 {"5 s", "1 s", "3 s", "8 s", "15 s", "25 s", "60 s" }
	);
But of course, that screws up the display sequence, so it's a poor solution. You need a SkinModel.java file that defines the default value:
public String defViewtime = "5 s";
jAlbum will use that file even if you're not compiling your UI.
jGromit

Posts: 31,948
Registered: 31-Jan-2006
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 15:07   in response to: monkeyboy in response to: monkeyboy
 
  Click to reply to this thread Reply
monkeyboy wrote:
You'll need to use either setSelectedIndex or setSelectedItem.

IIRC, using either of those methods collides with loading a saved project, so they're a bit of a minefield. SkinModel.java is the safe way! :)
monkeyboy

Posts: 464
Registered: 21-Jun-2008
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 15:14   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
I was just editing my post above whilst you were posting to mention the SkinModel.java method and to point out that values set in SkinModel.java will override any methods to set the JComboBox default in onload.bsh.

I don't think there should be a problem with loading a saved project.
When a project is loaded, setSelectedIndex or setSelectedItem will initially be run when the skin is loaded and then the project settings (including whatever value the JComboBox was set to when the project was saved) will be applied afterwards. (I think!)

In any case, as jGromit says, using a SkinModel.java file is a robust solution.

Edited by: monkeyboy on 19-Aug-2016 14:18
jGromit

Posts: 31,948
Registered: 31-Jan-2006
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 15:53   in response to: monkeyboy in response to: monkeyboy
 
  Click to reply to this thread Reply
monkeyboy wrote:
I don't think there should be a problem with loading a saved project.

I suspect you're right - I just have a memory flag set that tells me that I had run into problems with that. It was probably while doing something fancy with a StateMonitor(), like altering a combo box setting based on some other user input.
monkeyboy

Posts: 464
Registered: 21-Jun-2008
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 16:38   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
I've just run a couple of quick tests setting a JComboBox default via a SkinModel.java file and via setSelectedItem and everything seemed to work as expected. For both tests, the default value was set correctly when loading the skin and the default value was overwritten correctly when loading a project file.
(It always makes me a little worried when things work as expected. It almost never happens!).
AndreWolff

Posts: 2,507
Registered: 14-Dec-2007
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 17:27   in response to: monkeyboy in response to: monkeyboy
 
  Click to reply to this thread Reply
monkeyboy wrote:
Otherwise, you could use a SkinModel.java file to set types and default values.
Please note that if you do use a SkinModel.java file to set a default value for your defViewtime JComboBox, the value in the SkinModel.java file will override setSelectedIndex or setSelectedItem in onload.bsh.
I have no idea what a SkinModel.java file is, so I did a search in the forum and came in your thread. I did put the Create skin model file.bsh file in the tools directory.
But after I tried to use this tool, I got a crash:
 bsh.EvalError: Sourced file: inline evaluation of: ``import java.util.regex.Matcher;  import java.util.regex.Pattern;    String outpu . . . '' : Class: bsh.XThis not found in namespace : at Line: 16 : in file: inline evaluation of: ``import java.util.regex.Matcher;  import java.util.regex.Pattern;    String outpu . . . '' : bsh .XThis 
 in inline evaluation of: ``import java.util.regex.Matcher;  import java.util.regex.Pattern;    String outpu . . . '' at line number 16


So how can I make such a file by an text editor and what name should it get?

I tried also to put in headless-settings.jap:

skin.defViewtime=2

and

skin.defViewtime= 5 s

but both settings did not set the default value to the required value.
AndreWolff

Posts: 2,507
Registered: 14-Dec-2007
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 17:41   in response to: monkeyboy in response to: monkeyboy
 
  Click to reply to this thread Reply
monkeyboy wrote:
I've just run a couple of quick tests setting a JComboBox default via a SkinModel.java file and via setSelectedItem and everything seemed to work as expected. For both tests, the default value was set correctly when loading the skin and the default value was overwritten correctly when loading a project file.
Indeed I did try
	JComboBox defViewtime = new JComboBox(new Object[]
	 {"1 s", "3 s", "5 s", "8 s", "15 s", "25 s", "60 s" }
	);
	defViewtime.setSelectedIndex(2);
and this works fine, a saved value overrides the default value.

This is I think the most simple solution.

Thanks for your help!
monkeyboy

Posts: 464
Registered: 21-Jun-2008
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 17:52   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
AndreWolff wrote:
So how can I make such a file by an text editor and what name should it get?
Create a plain text file with the following content and name the file 'SkinModel.java'.
public String defViewtime = "5 s";

Now, create a new folder in your root skin directory named 'plugins' and put the 'SkinModel.java' file in the 'plugins' folder.
(You can add entries in the 'SkinModel.java' file for your other skin variables if you like).

I'll take a look at the 'Create Jalbum Skin Model File.jaskin' tool I created.
(It was over six years ago so it might need to be modified to work with the current version of jAlbum.)
monkeyboy

Posts: 464
Registered: 21-Jun-2008
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 18:20   in response to: monkeyboy in response to: monkeyboy
 
  Click to reply to this thread Reply
Attached is an updated version of the 'Create skin model file' tool.

Put the attached file in your 'jAlbum/tools/' folder.
Once you have loaded your skin, you can run the tool via 'Tools -> External Tools -> Create skin model file'.

It will automatically generate a 'SkinModel.java' file in the skin's 'plugins' folder (creating the folder if necessary) with an entry for each skin variable.

Values for the 'SkinModel.java' file will be taken from the user interface at the time the tool is run so if you want your JComboBox to have a default value of "5 s" when users load your skin, change the JComboBox value to "5 s" in the user interface before running the tool.

Alternatively, after allowing the tool to generate a 'SkinModel.java' file, you could just open the file in a plain text editor and change the default values as required.

I hope you find it to be a useful tool.
It might save you a bit of time creating a 'SkinMode.java' file by hand.
AndreWolff

Posts: 2,507
Registered: 14-Dec-2007
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 19:22   in response to: monkeyboy in response to: monkeyboy
 
  Click to reply to this thread Reply
monkeyboy wrote:
I hope you find it to be a useful tool.
It might save you a bit of time creating a 'SkinMode.java' file by hand.
I tried this method and it works fine, it overrules indeed my setting
defViewtime.setSelectedIndex(2);
in onload.bsh

But I am still confused, I have now 3 files for setting defaults:
  • hints.jap
  • headless-settings.jap
  • SkinModel.java

The skin variables recorded in headless-settings.jap does not seem to work, should I remove that file?
jGromit

Posts: 31,948
Registered: 31-Jan-2006
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 19:34   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
You still need headless-settings.jap. It doesn't work quite the way you're thinking it does. Say you make an album with SomeSkin 1.0. Later, you install SomeSkin 2.0, which includes a couple of new variables. Without ever opening the project in jAlbum, you just use the command line to rebuild the album. There will be errors, because the new variables aren't defined in the project file. The headless-settings.jap file fills in the missing information for the engine. You should generate a new one every time you produce a new version of a skin.
AndreWolff

Posts: 2,507
Registered: 14-Dec-2007
Re: How to set the default value for a JComboBox control?
Posted: 19-Aug-2016 20:37   in response to: jGromit in response to: jGromit
 
  Click to reply to this thread Reply
jGromit wrote:
You should generate a new one every time you produce a new version of a skin
So to play it safe, you should, before uploading a new version, generate a new version of the headless-settings.jap file and for the SkinModel.java file, but with different tools. The contents of these 2 files is, except from the format, the same for all skin variables.

Why is jAlbum making things so complicated, why is not just one file used for both purposes?

And if it absolutely required to use 2 files, why is there not one tool to create both files?

The current two tools do not show the variables in the same order, which is not very convenient if you compare the 2 files.
It would be nice if the tool to generate the SkinModel.java file, would also generate the same type of time stamp you see at the start of the headless-settings.jap file.

Edited by: AndreWolff on 19-Aug-2016 22:47

With the tools I made a new headless-settings.jap file and a new SkinModel.java file.
I thought first it was working correctly with these file, but it does not:

If I open now a project file and close it again without changing anything, it asks whether the changed data should be saved!

After I removed the plugins folder from the skin, it was working again OK

I used the tool also with the Minimal skin and it showed the same problem.

I have no idea why this happens, so I set my default viewing time with the statement
defViewtime.setSelectedIndex(2);


Edited by: AndreWolff on 20-Aug-2016 22:58
jGromit

Posts: 31,948
Registered: 31-Jan-2006
Re: How to set the default value for a JComboBox control?
Posted: 21-Aug-2016 00:03   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
AndreWolff wrote:
If I open now a project file and close it again without changing anything, it asks whether the changed data should be saved!

This appears to be an unfortunate side-effect of combining a SkinModel.java file with an interpreted onload.bsh. I have a version of Minimal that appears identical to the released version, but that has a compiled user interface. The SkinModel.java contents generated by monkeyboy's tool are identical to what's in the file that I use for compiling. But the unneeded prompt for saving does not occur with the compiled version.

The best answer is to stop fighting with that balky onload.bsh. Move it into "real" Java.
Legend
Forum admins
Helpful Answer
Correct Answer

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