Validate boolean true - java

I have a boolean inside a Checkbox and i want to validate if it is set to True.
So my entitie object looks kind of like this
#Validate("required,min=1")
private int Int1;
In my tml File (im using Tapestry 5.3.8) there is a Textfield which allows me to set a value for Int1
this works perfectly. If i put something else then an numeric number (or an int small 1) it shows me an error dialog.
but i can't figure out how to do that with a boolean. It needs to be checked an the user should get the same behauvior as with Int1 on a false entry.
Think of it like an Agree to the TOS checkbox which allways has to be checked to proceed.
Greetings Ilja

If I'm not mistaken a checkbox works with a boolean. So the following should achieve your result:
#Component(id = "agreeCheckbox")
private Checkbox agreeCheckbox;
#OnEvent(component = "agreeCheckbox", value = EventConstants.VALIDATE)
private void handleAgreeValidate(boolean agree) {
if(!agree) {
throw new ValidationException("Hey there! You have to agree or we can't do business with you.");
}
}
Disclaimer: I have not tested this code.

Related

Set text color using if and else statement

SO i have this application that uses retrofit client to send a request to the ROS server and now my problem is that I am setting up my Status that if the status is "True" it will set to textcolor as GREEN and else as RED but when i execute it to my application it seems that its only executing the else statement it displays all red even if the status is true. Can anyone help me with this Im just a beginner.
HardwareStatus is a TextView, so it's always not equal to false. Maybe you want to check it text? In that case you should use TextView.getText() method
if (HardwareStatus.getText().equals("false")) {
HardwareStatus.setTextColor(Color.RED);
} else {
HardwareStatus.setTextColor(Color.GREEN);
}
And the first letter of variable should be lower case HardwareStatus => hardwareStatus
Duy Khan Ngyuen answer is right, I just suggest you to use equalsIgnoreCase.
if(HardwareStatus.getText().toString().equalsIgnoreCase("false")){
HardwareStatus.setTextColor(Color.RED);
}else{
HardwareStatus.setTextColor(Color.GREEN);
}
It seems like you haven't get the text of the SoftwareStatus , you need to get the text first convert it to string and then compare it if it is true .
if(SoftwareStatus.getText().toString().equals("true"))
{
SoftwareStatus.setTextColor(Color.GREEN);
}
else
{
SoftwareStatus.setTextColor(Color.RED);
}
It is because .equals() is usually used for comparing strings/characters/text
edited.
after getting your response update your Boolean or string value in app class create setter and getter for that then simply on before add check on your textView `
Class ApplicationClass extends Application(){
String statusValue="false";
Boolean statusBoolean=false;
public void updateStatusValue(String value){
this.statusValue=vale;
}
public String getStatusValue(){
return statusValue;
}
}
after calling API and getting response updateStatusValue
ApplicationClass.updateStatusValue(response.body().getHardwareStatus())
if(ApplicationClass.getStatusBoolean() ||Application.getStatusValue().equalsIgnoreCase("true")) {
SoftwareStatusTv.setTextColor(Color.GREEN);
}else{
HardwareStatusTv.setTextColor(Color.RED);
}
now you can add check for whole application using this simple getter setter

Checking to see whether a hardcoded value is present in a gxt simplecombobox

I'm using gxt 2.0.3 in Java and I have created a SimpleComboBox which I then populate with 2 strings.
final SimpleComboBox<String> accessedComboBox = new SimpleComboBox<String>();
accessedComboBox.setTriggerAction(TriggerAction.ALL);
accessedComboBox.setEmptyText("Select a type");
accessedComboBox.add("Method 1");
accessedComboBox.add("Method 2");
I also have a listener attached to a different SimpleComboBox and depending on what is selected I need to either add or remove the value from the above accessedComboBox
if (typeComboBox.getSimpleValue() == "Type 1")
{
//Remove desktop app option
accessedComboBox.remove("Method 2");
}
else
{
if (accessedComboBox.??) { // <--- Check to see whether desktop app is an option
//if not then add it
accessedComboBox.add("Method 2");
}
}
I can't work out what function to use to check the see whether an option already exists in the SimpleComboBox. I have looked in this documentation but I've still had no luck.
Can anyone help?
Not sure if you got this yet. It's a little funky, but you can get the String values from the ListStore. Something like this:
for (SimpleComboValue<String> value : accessedComboBox.getStore().getModels()) {
if (!value.getValue().equals("Method 1")){
accessedComboBox.add("Method 1");
}
}

How to refresh dynamic contents in Wicket when you press F5?

I've implemented a MatchPage which displays the following informations:
MatchStatus (OPEN/CLOSED)
Winner (Name of the Winner)
Form to upload files
(follow the link to see how it looks like, [1]: http://www10.pic-upload.de/25.04.13/klmy9fe8cgk3.png)
Now here comes the problem. Let's assume, someone is reporting a result while another one has currently open the specific MatchPage. When the report is done, the MatchStatus will change from OPEN to CLOSED, the color will change from OPEN=green to CLOSED=red, the Winner will be set and the Form for uploading files will disappear (see [2]: http://www7.pic-upload.de/25.04.13/9diu5bcbws9.png).
The player who reported the result will see the updated MatchPage while the other one will still see the old version of the MatchPage, even if he refreshed the browser.
I could solve the problem with OPEN/CLOSED by using my own LoadableDetachableModel:
#Override
public String load()
{
Match m = dao.getMatchFromID(match_id);
String result = "OPEN";
if (m.getClosed())
{
result = "CLOSED";
reportForm.setVisible(false); //does not work
colorBehavior.setColor("red"); //does not work
}
return result;
}
Label on my MatchPage:
matchStatus = new Label("matchStatus", new MyMatchModel(m.getMatch_id(), matchDAO, reportForm));
As you can see on the load() method, setting the reportForm invisible and setting the color to red does not work.
Any idea how i can solve such kind of problem? How can i make the form disappear and change the color to red when the user pressed F5/refresh the browser.
You should override the "isVisible()" method of the form like this:
public boolean isVisible() {
return !yourModel.getObject().getClosed();
}

Typing Number Into JSpinner with Subclassed SpinnerListModel

I want to have a JSpinner that displays an non-patterened sequence of numbers (say, a sequence of prime numbers). This pattern is too complicated for a SpinnerNumberModel, so I decided to subclass SpinnerListModel. The constructor looks something like this:
public CustomSpinnerListModel() {
Vector<Integer> values = new Vector<Integer>();
values.add(1);
values.add(3);
values.add(5);
values.add(7);
this.setList(values);
}
This generates the model just fine and I can move through the values using the buttons on the JSpinner. However, typing a value in doesn't work. For instance, if the spinner is set to 3 and I type in 7, it remains at 3 (presumably because it doesn't think that 7 is a valid value). This works with the SpinnerNumberModel, so I'm not sure what's going on.
EDIT: I found out that if I save the numbers as string values, typing works. However, SpinnerNumberModel saves everything as Integers and that works too. So I'm not sure why my integers don't work, but SpinnerNumberModel's do.
I think the following solution is better than the suggestion to implement a Formatter, as it is not a formatting issue, but an issue of restricting the possible values, which should be the responsibility of the model. I had a similar problem and stumbling upon this threads solution, lead to a very ugly implementation. So hopefully what I came up with will keep you out of trouble.
This generates the model just fine and I can move through the values using the buttons on the JSpinner. However, typing a value in doesn't work. For instance, if the spinner is set to 3 and I type in 7, it remains at 3 (presumably because it doesn't think that 7 is a valid value). This works with the SpinnerNumberModel, so I'm not sure what's going on.
The Problem here is that setting a new model with setModel has the undocumented side effect of changing the JTextFieldEditor attribute depending on the type of the Model:
http://fuseyism.com/classpath/doc/javax/swing/JSpinner-source.html
By default, JSpinner uses a model of class SpinnerNumberModel with an editor of class DefaultNumberEditor. When you set the model to SpinnerListModel, it will instead use a ListEditor. In your case this is a bad choice, since it requires you to enter every prime number into a list to give it to the SpinnerListModel for input verification. Otherwise, as you pointed out, your input is ignored.
So the simple solution here is to subclass SpinnerNumberModel, which allows any number, instead of a specific list of values:
class PrimeNumberModel extends SpinnerNumberModel {
Object currentValue;
#Override
public Object getNextValue() {
return findNextPrimeFrom(currentValue);
}
#Override
public Object getPreviousValue() {
return findPreviousPrimeFrom(currentValue);
}
#Override
public void setValue(Object o) {
throwOnNonePrime(o); //Verify Input
super.setValue(o);
}
private void throwOnNonePrime(Object o) {
try {
int num = Integer.valueOf(o.toString());
if(!isPrime(num))
throw new IllegalArgumentException(o.toString());
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException(o.toString());
}
}
}
I think you could do it with strings and then use a method to get the number.
like this:
Spinner1(){
String[] values={"1","3","5","7"};
SpinnerModel model=new SpinnerListModel(values);
JSpinner spinner=new JSpinner(model);
}
int getValue(Object obj){
int out=0;
return out=Integer.parseInt((String)obj);
}

How to get int value from spinner

I'm using NetBeans 7.1 to code in Java. I have a JFrame where I have spinner with integer values on it, I want to know how to get the active value in the spinner, I mean, the one that the user picks when the program is running; to use it on another methods.
spinner.getValue() should do the trick. You can cast it to Integer, like
int value = (Integer) spinner.getValue();
Note from reggoodwin: You should also call spinner.commitEdit() prior to calling getValue() to ensure manually typed values with the editor are propagated to the model, otherwise you will only get the old value.
Hence, it should be something like below,
try {
spinner.commitEdit();
} catch ( java.text.ParseException e ) { .. }
int value = (Integer) spinner.getValue();
String value = getSpinner().getValue() + "";
Integer.parseInt(value)
My solution, this working for me...
Not work:
Integer.parseInt( getSpinner().getValue().toString()) //get object toString
I do not understand, but it works, I leave it in case anyone needs it.
String spinner = "catch Value";
Integer myint = (Integer) jSpinner1.getValue();
spinner = myint.toString();
jTextField1.setText(spinner);
This worked for me. Wanted to write the Integer value from jSpinner to a textfield.

Categories