Edit Text and CheckBox checking - java

I currently have 2 checkboxes with an edit text field beside them. There are 4 checkboxes total and I have the logic to check each one and uncheck others if they are checked (almost radio button style). These 2 however could both be checked if they have values in the edit text fields.
However, if the EditText field (which is set to numeric) has a value of 0 or is blank I want it to uncheck the check box and set the value to 0.
Here is the code I have to do this
if (etBase.getText().toString() == "0" || etBase.getText().toString() == ""){
etBase.setText("0");
cbBase.setChecked(false);
} else {
cbBase.setChecked(true);
}
if (etField.getText().toString() == "" || etField.getText().toString() == "0"){
etField.setText("0");
cbField.setChecked(false);
} else {
cbField.setChecked(true);
}
As it sits right now I default the two fields to be "0" when it starts. When this logic runs, it is setting both checkboxes to checked.
I must be missing something here.

To compare string, you must use .equals() function. ( Or .compareTo() but that's no the today's deal)
Ex :
if (etBase.getText().equals("0") || etBase.getText().equals("")){
...
}
var1 == var2 is used to get if these both variable have the same object

Related

How can I embed images into a Birt report to show an image depending on if the column result equals 1 or 0?

How can I embed images into a Birt report to show images depending on if the result equals 1 or 0?
I’ve tried following the guide from:
http://birtworld.blogspot.com/2010/09/birt-image-report-item.html
I am not able to show an image (checkbox) when the column value equals 1 and another image (unchecked box) when the column value equals 0.
I’ve tried using the following expression:
if( row["acceptsplititem"] == 1 ){
"checked-50x49.jpg";
}else if( row["acceptsplititem"] == 0 ){
"unchecked-50x49.jpg";
}
I’ve tried adding both images (checked and unchecked) by dragging the image report item and selecting from image file in shared resources, then editing the URL of the image via:
Properties > Advanced > URL
if( row["acceptsplititem"] == 1 ){
"checked-50x49.jpg";
}else if( row["acceptsplititem"] == 0 ){
"unchecked-50x49.jpg";
}
I’ve also tried a suggestion from a previous question/answer on stackoverflow:
Embedding an image in BIRT
But this doesn’t display either image.
Adding an image using Dynamic text
Embedding an image
Please help and advise where I am making a mistake
Thanks in advance,
Stuart
This is an expression, so you should NOT use a semicolon afteer the string literals.
Furthermore, the value might be different from 0 or 1 (e.g. null), so you should also add yet another image filename for the "else" part.
In order to check that your images can be displayed at all, you should also use a constant expression as the very first step:
Constant expression:
"check-50x49.jpg"
This should show the "checked" image.
Next, the expression depending on the value (assuming you also have an image file "other.jpg":
if( row["acceptsplititem"] == 1 ) {
"checked-50x49.jpg"
} else if( row["acceptsplititem"] == 0 ) {
"unchecked-50x49.jpg"
} else {
"unknown.jpg"
}
Or - slightly more verbose:
var fname = "unknown.jpg";
if( row["acceptsplititem"] == 1 ) {
fname = "checked-50x49.jpg";
} else if( row["acceptsplititem"] == 0 ) {
fname = "unchecked-50x49.jpg";
}
fname
I've found that by inserting an image then selecting visibility from the Property Editor you can then tick Hide Element and enter an expression:
row["preferred"]==0
This would hide the element (Tick Box) if the result is 0 Place another image (Crossed Box) on the same row with expression:
row["preferred"]==1
This will hide the image if the result is 1

java: how to write a conditional if statement like "if not (A empty) and (B empty) then"

In Java, I need to test that at least 1 of 2 radio groups (radiogroup1 and radiogroup2) is not empty when clicking on a button (i.e., all radio buttons shall not be empty AND at least one radio button is pressed).
The code structure would be something like:
button is pressed {
if (radiogroup1 not empty) or (radiogroup2 not empty) {
//do something
}
}
If both radiogroups are empty, nothing happens when the button is pressed.
Now, I set up something like this:
if (RadioGroup.getCheckedRadioButtonId() == -1 || RadioGroup2.getCheckedRadioButtonId() == -1) {
//do something
}
but I need actually exactly the opposite. I don't succeed to invert the condition with != or something else.
Any help appreciated!
May be this?
if (!(RadioGroup.getCheckedRadioButtonId() == -1 || RadioGroup2.getCheckedRadioButtonId() == -1))
{
//do something
}
You mean like this?
RadioGroup radioGroup1; // assumed initialized someplace else in code
RadioGroup radioGroup2; // assumed initialized someplace else in code
// Check to see if at least one of the two radio groups has a selection
if (radioGroup1.getCheckedRadioButtonId() != -1 ||
radioGroup2.getCheckedRadioButtonId() != -1) {
// Do stuff
}

java Validations to check if more than one textbox has values

I have three text boxes in my html ,where user can input values. In my java class i am doing validations to check if more than one input box has been entered .If user has entered values in two/three textboxes i need to throw an error message. If he hasnt entered the value in three textboxes also i am planning to throw error messsagae. My code is something like this .
if(!dId.equals("") && !PId.equals("") && !PPup.equals("")){
result.addError(new ErrorBO("only one should only be entered"));
}
if(dId.equals("") && PId.equals("") && PPup.equals("")){
result.addError(new ErrorBO(" one should be entered"));
}
dId,PId,PPup are the variables where i have my values. This code fails for the case where the user enters the value in two text boxes. Is there a simplified way to check all the cases.
int numEntered = 0
if(!dId.equals("")) numEntered ++;
if(!pId.equals("")) numEntered ++;
if(!pPup.equals("")) numEntered ++;
if(numEntered != 1) result.addError(new ErrorBO("Enter values in one text box"));
Basically, increase a counter every time a text box has entry. If anything but 1 of them has text, return your error.

Java: Display Error When JTextField is left Empty

I've done all the coding for this mid-term exam I got going on, but I've run into this one snag. Mainly, I cannot seem to get my code to throw an error message at a user when they leave one JTextField empty. I think the issue I am having here is that I've declared double variables for each of the JTextFields (with Double.parseDouble) and as far as all my searching around goes, there is no way to check for an empty double value. I've tried a strange workaround where I declare another set of String variables for the same JTextfields (i.e. they already have double variables attributed to them) and then testing for stringVariable.equals("") but that doesn't work either.
I've tried simply going straight from the JTextField - myJTextField.getText().equals("") - and that doesn't throw my error message either.
Keep in mind I am in a beginning Java course so this should be a simple solution.
My code for the relevant section is below:
// IF statement to display error message if no name entered
// OR no value input for any week's Homework
// OR no JCheckBox isSelected
// remember to check against the null string or non-selection of JCheckBox
if ( ( name.equals ( "" ) ) ||
(homework1JTextField.getText().equals(""))||
//Test against incorrect range of values for homework grades
(tut1 < 0 || tut1 > 10)||
(tut2 < 0 || tut2 > 10)||
(tut3 < 0 || tut3 > 10)||
(tut4 < 0 || tut4 > 10)||
(tut5 < 0 || tut5 > 10)||
(tut6 < 0 || tut6 > 10)||
(tut7 < 0 || tut7 > 10)||
(!discussionBoardZeroJCheckBox.isSelected() &&
!discussionBoardOneTwoJCheckBox.isSelected() &&
!discussionBoardThreeFourJCheckBox.isSelected() &&
!discussionBoardFiveSixJCheckBox.isSelected() &&
!discussionBoardSevenJCheckBox.isSelected()
)
)
{
// display error message
JOptionPane.showMessageDialog( null,
"Full Name, all homework points and at least one discussion board checkbox are
all required information!",
"Missing Information", JOptionPane.ERROR_MESSAGE );
}
An easy test is:
if (homework1JTextField.getText().trim().equals("")) {
// throw error
}
I would say why don't you assign intermediate boolean variables for each of those groups of comparisons. Use these booleans in the if.
Print out the values of the booleans since you're if is pretty complex, there's scope for lots of things to go wrong here.
There are various ways to fix the problem. Here is one. Replace JTextField with JTextFieldWithDefault to cause an out of range value (-1) to be returned if the user does not type something or just types spaces. Your existing code remains unchanged and will now work as you expect.
public class JTextFieldWithDefault extends JTextField {
public JTextFieldWithDefault() {
super();
}
#Override
public String getText() {
String s = super.getText();
return s.trim().equals("")?"-1":s;
}
}

Getting error when checking combobox for null value

I got a combobox, and a submit button, when the button is submitted, i want to check if the combobox value was null. Im using this code:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem().equals(null)) {
infoLabel.setText("Combo box value was null");
}
i am getting this error when i press the submit button:
java.lang.NullPointerException
how can i fix this?
You can not call equals on null. Instead simply use == null.
Something like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
Should work.
You can not give the null reference to equals(), do it like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
And a remark that has nothing to do with your question: I suggest using the Java Naming Convention, which would lead to your combo box being named comboBox (and not ComboBox).
The condition should be :
ComboBox.getSelectedItem() != null
or
ComboBox.getSelectedItem().toString().equals("")
This checks if what is selected in the Combobox is null or empty
Another way of doing this is leaving the first item empty, then check for the selected index against 0 i.e
ComboBox.getSelectedIndex() != 0
Thanks

Categories

Resources