Create logical GUI components group using windows builder - java

I am writing a GUI app in WindowsBuilder eclipse java and have some questions:
I have a check button that if it's checked some controls are enabled. Is there an elgant way to allow all of them by one command? I mean that I dont want to enable them one by one, just enable them at once - is it possible to define a logical group that will allow me to do it?
Is there any common design pattern to write Java GUI applications?
I am new in Java, so will appriciate any guidance in these queastion.
Thanks!

There is no build-in function to check/uncheck them all with one command.
The "easiest" way that comes to mind is to store them all in a List and create a function that iterates over that list and checks/unchecks everything.
private List<Button> buttons = new ArrayList<Button>();
// ADD YOUR BUTTONS
private void setSelectionForButtons(boolean enabled)
{
for(Button button : buttons)
button.setSelection(enabled);
}
Then you can check/uncheck them all by calling:
setSelectionForButtons(true);
or
setSelectionForButtons(false);
As for the "design patterns": There is an excellent tutorial for writing SWT applications here.

Related

How to get values from a JPanel?

I've created a JPanel using the NetBeans designer filled with JTextFields and a submit button. I would like to get the values from those JTextFields and use them in my main class. How can I do that?
Also, what are some good tutorials that can help me further understand this? Thank you.
I'm guessing you mean JTextField and not TextField. Use the getText() method.
String text = yourTextField.getText();
Also works with the TextField class, actually.
You'll need an ActionListener on your submit button if you want to grab the text fields' values when a user clicks the button.
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yourButtonsName) {
text = yourTextField.getText();
}
}
Don't forget to add the ActionListener!
yourButtonsName.addActionerListener(this);
Or you could use Java 8 lambda expression:
yourButtonsName.addActionerListener(e -> text = yourTextField.getText);
If you'd like to learn more about Java's graphical capabilities, I recommend the Oracle docs: http://docs.oracle.com/javase/tutorial/uiswing/.
"I've created a JPanel using the NetBeans designer filled with JTextFields and a submit button. I would like to get the values from those JTextFields and use them in my main class. How can I do that?"
Sounds like to me you're facing the class problem of how do I reference instance variables from one, in another.
An easy way would be to pass one class as reference to another and use proper getters and setters. You can see a solution here.
A better solution though would be to create an interface that one of the classes implements and pass that class as an interface to the second class. You can see an example here.
If you feel you're ready for more advanced topics, you should look into MCV Design patterns for this type of problem. MVC is designed for multi-component interaction.

Java - Best design for setting up configuration from GUI to logic side

I am making some application for myself, just to practice on my designs and GUI.
My application is divided into two sides, first side is the model / logic however you call this, second side is the visual side, where you handle the gui, buttons and view.
So now my application has a feature that pops up and asks the user if he wants to use some feature, and then if he clicks yes, it will open a new JFrame window with many configurations.
these configurations will "probably" be in the Config class.
My question is, what is the best way to transfer data from the GUI to the model? Since you have to create a button listener in order to detect button clicks or text, what is the best proper way to update the configuration after the button was clicked?
For example, you have an application, and in order to start it you need to click on the button, I have two ideas in my head:
Transfer the Config object to the area where you handle the button & listen to it
Make Config static, and set up a Set() method, so you can set configurations without any objects, like Application.setConfiguration(config, type)
But I heard that statics are NOT always good, so I wondered, using static in this case is OK and good or are there better ways to do this? Or passing the config object to the GUI area is OK aswell?
This is how my structure looks like:
(source: gyazo.com)

How to set custom text on buttons in JFace Wizard (Java)

I am using JFace Wizard and I want to set my own text on buttons Next, Back, Finish and Cancel. I found only very old advices which are completely useless today. I also found some solution with external jar files, but I really don't want to add whole library to project only for setting text on 4 buttons...
Is there any reasonable solution?
Thanks in advance
After massive finding and trying, I have to say there is no such way. There are some brutal solutions, but compared with them, adding one jar file to project is much easier and nicer.
I will cite best working solution for me:
You need to download a language pack from here:
http://archive.eclipse.org/eclipse/downloads/drops/L-3.2.1_Language_Packs-200609210945/index.php
NLpack2-eclipse-SDK-3.2.1-gtk.zip works for me while I'm using Eclipse
3.7.2.
Extract org.eclipse.jface.nl2_3.2.1.v200609270227.jar (or other nl for
your language) from the archive and add it to your project. It will be
used automatically.
This do not let you to set texts on buttons, but at least gives you texts translated into your language.
Saw this post. Seems to be the answer to your question.
It basically says to create a dialog using WizardDialog class. Create a class that inherits from Wizard with the implementation of your choice then do below:
WizardDialog wizardDialog = new CustomWizardDialog(shell, new YourWizard());
and then in your CustomWizardDialog do the following:
public class CustomWizardDialog {
#Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button finishButton = getButton(IDialogConstants.FINISH_ID);
finishButton.setText("FinishButtonText");
Button cancelButton = getButton(IDialogConstants.CANCEL_ID);
cancelButton.setText("CancelButtonText");
}
}
All that is left is to perform wizardDialog.open() to open dialog.

Using SWT for multi-view application; similar to Android activity lifecycle

So my situation is simple (IMHO):
I am trying to create web-esque Java application (it's a sort of Point-Of-Sale application) that behaves much like a website, but is all in Java. Right now I have a semi-simple SWT application written in Eclipse and it displays a few options (sign in, price check, inventory check and employee timeclock). When any of these is pressed (or corresponding keyboard shortcuts are activated) a dialog box pops up prompting authentication. Assuming user is verified, I want the main application window to display a new set of functions (scan item, item lookup, etc.) seamlessly.
If this were HTML I would just make a new page, and if I were writing against the Android platform I would just create a new activity...but this is very new and I am having a very hard time finding any relevant information.
PS I'm not set on SWT if anybody thinks a different library/technology (such as Swing/AWT) is better.
In SWT, if you want to replace the content of a Composite, you first need to dispose the existing controls, next you create the new controls, and finally call the layout(...) method on the Composite:
// Retrieve existing composite
Composite composite = [retrieve existing composite]
// Remove exising children
for (Control child : composite.getChildren()) {
child.dispose();
}
// Create new children
Label label = new Label(composite, SWT.NONE);
// Layout
// Maybe update the composite layout with composite.setLayout()
composite.layout(true, true);
Another solution is to use a Composite with a StackLayout if you want to display back and forth several predefined contents.
Whether you use Swing, AWT or SWT is entirely your choice. Personally, I prefer Swing, but you can do the same thing with SWT.
As for your predicament, you need to do a bit of studying regarding GUI's first. A Java desktop application consists of a top-level container, usually a JFrame (Window) that can contain other components, windows, dialog boxes etc. Your best best here is to pop up a MODAL dialog box that asks the user for authentication information. If the user is authenticated, you can dynamically create buttons, text boxes etc. in your code, creating the "new" look you want.
Might I suggest you start of with some simple GUI design exercises first, before diving into a full-fledged application? Consider the Java GUI tutorials at http://docs.oracle.com/javase/tutorial/uiswing/ as a good starting point.
Once you have mastered basic dialogue boxes, forms and components, you'd be in a far better position to plan your GUI and will find it easier to create it just the way you want it.

How to dynamicly build up a gui

currently im building an application which is supposed for some sound processing. I'm doing this in java/eclipse with swt/jface.
The processing itself need some options/properties for the algorithem inside. At this time, i have a .properties file which holds all options like:
trimLeadingSilence=20
trimtrailingSilence=20
trimhold=5
fftFrameSize=512
...
I don't want the user to edit these options in a texteditor like notepad++, but within the gui of my app.
Now I think about how to do this. I have 2 "ideas":
Create a class for each option set, and manualy code all these boring gui code lines. Like here for just 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 lot of time due to the fact that there are a lot of different options. So I thought about how I can dynamicly create such gui code, or just dynamicly create these gui windows when starting up the application. At least I would need a config file for the "gui creator" but I don't want to reinvent such a thing-thats why i ask you guys :)
I couldn't get clearly what you are asking.
But, since your question was How to dynamicly build up a gui, i have a a suggestion:
You could use Java Template Engine
library like Freemarker. This
would help you create GUI's that can
be built to work on the fly. With
freemarker you can have one single
template file and then replace the
values accordingly.
I have used it to generate HTML files on the fly. You could evaluate if you can use it otherwise. The API Documentation is rich. So you could go through that once.
Do you mean, you want to make an UI which will have all options you specified? It doesn't matter its a form OR a menu, its up to you. But yeah you can surely configure the names and types in .properties file.
See, you build a Menu OR form in AWT/Swing OR Servlet, but you can read it from properties file right?
You can also configure the same using Spring bean XML definitions, which can provide more support like you can specify all details in some Map OR List etc...
thanks.
I didn't use Swing for a lot of time, so here is just a basic principle.
Config should be done in xml, .properties file is bad here, cuz it doesn't describe objects out-of-the-box.
Add button ("Apply config"), attach actionListener, which 1)parses xml config, 2)then creates form or change settings of existing form, textarea, colors etc.
example for xml config:
found - check it's x_coordinate, y_coord (or use layoutManager, depends on logic), action, than jframe.getLayout().add(new Button(x_coord, y_coord, action).
found - the same.
than jframe.setVisible(true);

Categories