How to dynamically create gui
I'm currently building an app that is supposed to do some audio processing. I am doing this in java / eclipse with swt / jface. The processing itself needs some variations / properties for the algorithem inside. At this time, I have a .properties file that contains all the parameters such as:
trimLeadingSilence = 20
trimtrailingSilence = 20
trimhold = 5
fftFrameSize = 512 ...
I don't want the user to edit these options in a text editor like notepad ++, but in the gui of my application.
Now I am thinking about how to do it. I have 2 "ideas": Create a class for each set of options, and also manually enter all those boring lines of gui code. Like here for one option; -)
Spinner spinnerSilenceBack = new Spinner(shell, SWT.BORDER);
spinnerSilenceBack.setMinimum(0);
spinnerSilenceBack.setMaximum(200);
selection = Integer.valueOf(PropertyManager.getPropertyValue("trimming", "trailingSilence"));
spinnerSilenceBack.setSelection(selection);
spinnerSilenceBack.setIncrement(5);
spinnerSilenceBack.setPageIncrement(20);
spinnerSilenceBack.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int selection = ((Spinner) e.getSource()).getSelection();
int digits = ((Spinner) e.getSource()).getDigits();
int result = (int) (selection / Math.pow(10, digits));
PropertyManager.setPropertyValue("trimming", "trailingSilence", String
.valueOf(result));
}
});
This takes a long time due to the many different options. So I thought about how I can generate such gui code dynamically, or just dynamically create these gui windows when the application starts. At least I need a config file for the "gui creator", but I don't want to invent such a thing - that's why I'm asking you guys :)
a source to share
I could not clearly understand what you are asking.
But, since your question was How to create gui dynamically , I have a suggestion:
You can use Java Template Engine library like Freemarker . This will help you create a graphical interface that can be built to work on the fly. With freemarker you can have one template file and then replace accordingly.
I used it to generate HTML files on the fly. You can evaluate if you can use it otherwise. The API documentation is rich. So you could get through this.
a source to share
Do you want to create a user interface that has all the options that you specified? It doesn't matter its shape OR menu, it's up to you. But yes, you can fine tune the names and types in the file .properties
.
See you are creating an OR menu form in AWT / Swing OR Servlet, but can you read it correctly from the properties file?
You can also customize the same with XML Spring bean definitions which can provide additional support, for example you can specify all data in some list of maps, etc. etc.
thanks.
a source to share
I haven't used Swing for a long time, so this is just the basic principle.
The configuration has to be done in xml, the .properties file is bad here because it doesn't describe objects out of the box.
Add a button ("Apply config"), attach an actionListener, which 1) parses xml config, 2) then creates a form or changes the settings of an existing form, textarea, colors, etc.
example for xml config:
found - check it's x_coordinate, y_coord (or use layoutManager, logic dependent), action than jframe.getLayout (). add (new button (x_coord, y_coord, action).
found - same.
than jframe.setVisible (true);
a source to share