as I mentioned in a post before, I'm porting my program to Java, to make it available for Mac OS and Linux users.
At the start of the program, I'd like to check if adb is installed to the system using this code:
private void checkADBExists()
// Checks if adb binaries exist and sets jTogglebutton1 correspondingly...
{
File adb = new File("/usr/bin/adb");
if (!adb.exists())
{
jToggleButton1.isSelected();
} else {
jToggleButton1.isSelected()= false;
}
}
Here's my problem:
If the file doesn't exist, the JToggleButton isn't selected, even though it should be and I get an error deselecting it.
Any help is much appreciated.
Thanks in advance,
Beats
Many of Swing's core components follow a simple getter/setter pattern.
That is, you can "get" a property value and "set" a property value (note, not all getters have a corresponding setter though).
In the case of a boolean property, the convention is to use "is" instead of "get", it just rolls off the tongue better.
So, in your case, all you are doing is getting the value if the selected property, not really what you want to do.
Instead use jToggleButton1.setSelected(true) or jToggleButton1.setSelected(false) based on your needs
You might like to take a look at How to Use Buttons, Check Boxes, and Radio Buttons for some more details
JToggleButton().isSelected() return a value not a variable. By JToggleButton().isSelected() = false, you are trying to assign a value to a value, it doesn't make sense, much like writing a statement 2 = 2;. use JToggleButton.setSelected(true) to set the toggle button as selected and JToggleButton.setSelected(false) to deselect.
Related
I'm using JavaFX for my application's GUI. I want to implement a validation method for all the textfields inside the sign up window. I want to check them all and than, whether they are true or false, I want to use a mark to show the user what field is incorrect. I also want to be able to show a small message box when I hover the mouse pointer over those marks.
Simple way is to create HBox , put TextField,Label in there ,label will be Bound on the textProperty/ or do it with listener
txtField.textProperty().addListener((v, oldValue, newValue) -> {
//code here if valid, set label visible false, else set label visible true(red image crossed or whatever)
});
, when value changes it will check if that is ACCEPTABLE/FAILED state , for instance empty box.States will be changed on property change , use array of your hboxes to check if they are valid or invalid at the time , you can check this based on visibility of Label or internal boolean state value.
For the hover over part , use Tooltip on label.
If you want to go lazyer way , take a look at controlsfx validation it will take care of graphics for you.And its already embedded in its component.Just create validation process
Good beginner reference might be newboston videos so you understand concept.In javafx you gonna use property binding ,listeners etc often , get familiar with them as you cant avoid it.
https://www.youtube.com/watch?v=s8GomyEOA8w
https://www.youtube.com/watch?v=6Zi2L0kHSx4
Since you've given no code I can't give you answer that involves actual coding, because I've got no way of knowing whether or not what I give you will be viable or conflict etc..
In regards to the validation that depends entirely on how your accessing a username/password to compare it to what the user has entered and with out knowing how you're thinking of doing it I cannot give you a good answer.
There are quite a few options to display your red x, you could draw it internally etc..
But the easiest is probably going to be creating an image and importing it to your project, you can set a label next to your JTextField and have the picture set to that lable. Once the user inputs the username/password if either or both are incorrect you could have a method that would set the label to be visible.
The message box is as simple as a tooltip that you could also place on the label which would tell the user that the information they entered is wrong.
I am able to use WindowsAccessBridge.dll to get access to java window (SwingSet2), and use the dll methods to interact with the controls.
I can iterate the tree (of java controls) using code shown here, and get information from the controls,
and I can do actions like SetText in Text box, or send click action to a button etc.
However, I am finding that the accessibility contexts are not consistent:
The SetText method works perfectly when I use the accessibility context got from the 'setMousePressedFP' method of the dll, after clicking on the text box.
However, I cant use the accessibility context got by iterating the control tree when trying to set text in a text box, although I verified that the other information e.g. size and other properties of the text box is same.
From this doc:
"The functions GetAccessibleContextAt and
GetAccessibleContextWithFocus retrieve an AccessibleContext object,
which is a magic cookie (really a Java Object reference) to an
Accessible object and a JVM cookie. "
However, the accessibility context differs everytime I click on the text box (without restarting the java program). The vmID matches eveytime.
If the AccessibleContext is really a Java Object reference, shouldnt I get the same AccessibleContext number everytime I click on the same control (without restarting the java window) ? and shouldnt that AccessibleContext number also be available in the tree?
Worst part is that the settext method doesnt work when I use the AccessibleContext got from iterating the control tree.
Edit to clarify the question further:
I am clicking on the same text box, so I would expect the windowsaccessdll function to return reference to the same control. As a long value the accessibeContext is different everytime I click, but its ok as the SetText method works everytime with the different long values and I can see the new text set in the text box.
The problem is when I iterate the control tree and the accessibility context got from the tree iteration doesnt work for the SetText method.
More specifically - how does the AccessibleContext compare to a c pointer - the c pointer would always be same when I get it for a specific control. The AccessibleContext appears to be a 8 digit long value that is different by few tens or hundreds based on where the mouse is clicked inside the text box.
Thanks in Advance.
I have an android Dialog with a 3 numberpickers inside. Changing the 3rd picker triggers a change in the displayed values of the first 2 pickers. However I've noticed when I change the displayed values, and call
setWrapSelectorWheel(false)
it still shows the new values as wrapped visually (I can see the last value above the first one).
If I touch the picker it suddenly snaps into non wrap selector wheel. What's odd is I call
getWrapSelectoWheel()
after setting the displayed values and it returns false... just like I set it. But visually it's wrong.
Any ideas what's going on?
Many thanks!
I found a solution, Daniel was on the right track, however it appears that initializeSelectorWheelIndices is actually a bad thing once you've already set your values. Because of this, you need to call setMinValue and setMaxValue BEFORE you set your values. However, if you already have an array set on your picker, if you call setMaxValue with a higher value than the current array, it will give you a ArrayIndexOutOfBounds exception.
The solution then is to clear the old display values, set your max value, then you can call setWrapSelectorWheel and setDisplayValues:
public void updatePickerValues(String[] newValues){
picker.setDisplayedValues(null);
picker.setMinValue(0);
picker.setMaxValue(newValues.length -1);
picker.setWrapSelectorWheel(false);
picker.setDisplayedValues(newValues);
}
I don't know what version of Android you're running but I would suggest you read the source code for the NumberPicker (v4.2.2). Perhaps you need to trigger a call to the private method initializeSelectorWheelIndices which can be done via a call to some of the public methods.
Although I do think your usage of the picker, by changing the wrapping flag part way through, does seem a little unusual. Don't you want this behaviour to be consistent? In which case make a call to setWrapSelectorWheel once after you've set your min and max values and then leave it alone.
I've verified this behavior as well on kitkat. It's a bug. Couldn't find an existing report so I created a new one here: bug# 98319
Per previous answers I found that making setWrapSelectorWheel(false) the last call after all setup calls will work for most scenarios. In my case I'm dynamically changing limits of the NumberPicker as the user is interacting and I just can't get it to behave 100%. I'm opting to live with the wrap selector.
I get the dialog for changing the properties of the print job by invoking the PrinterJob's printDialog() method without any parameter. There is a field where the user can change the the number of copies to be printed on the right-bottom of this dialog.
And now I want to disable this field (the spinner). That is only one copy to be printed and the user can't change it.
Are there any ideas for it
I don't think there is any way (or i am not aware of) to modify the native or the cross-platform Java print dialogs (last one might help you a bit). What you could do is maybe display your own dialog (without a spinner field) and under-the-hood do whatever you want (like PrinterJob.setCopies(1)).
More info i found here.
Also, have a look at the Java tutorial on PrinterJobs.
I am using wicket's CheckBoxMultipleChoice to let the user set a list of options. so far it works fine. but then i want to add a "check all" check box that checks all the options in the CheckBoxMultipleChoice and I am having problems with that.
here is my initial code
ArrayList<String> chosen;
List<String> choices = Arrays.asList(new String[]{"Train", "Bus", "Car"});
CheckBoxMultipleChoice myCheck = new CheckBoxMultipleChoice("transport", new Model(chosen), choices));
myCheck.setOutputMarkupId(true);
form.add(myCheck);
on submit i print out the values of chosen and its "Bus", "Car" etc. as expected.
now i am adding a checkbox to check all the choices using ajax:
Boolean checkOrNot;
final CheckBox checkAll = new CheckBox("checkAll", new Model(checkOrNot));
form.add(checkAll);
checkAll.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
// here i am not able to set the checkboxes
// i tried doing this
chosen.clear();
chosen.add(new String("Car"));
chosen.add(new String("Train"));
myCheck.modelChanged();
// i have also tried recreating the multiple choice
myCheck = new CheckBoxMultipleChoice<T>("transport", new Model(chosen), choices);
myCheck.setOutputMarkupId(true);
target.addComponent(myCgecj);
target.addComponent(form);
}
});
I am running out of ideas and wondering if anyone has any solutions? thanks in advance for any help.
I didn't (and can't) try that right now, so this is to be taken with a grain of salt, but couldn't you use a CheckGroup for that?
From the JavaDoc:
Component used to connect instances of Check components into a group. Instances of Check have to be in the component hierarchy somewhere below the group component. The model of the CheckGroup component has to be an instance of java.util.Collection. The model collection of the group is filled with model objects of all selected Check components.
So a
new Checkgroup("group", choices)
should work for you. No need to reimplement that functionality.
P.S.: I'll check that as soon as I've got the chance to do so...
You could use javascript to mark the checkboxes.
An example, using jQuery:
mycheck.setOutputMarkupId(true);
checkAll.add(new SimpleAttributeModifier("onclick",
"$('#" + mycheck.getMarkupId() + " input:checkbox').attr('checked', $(this).is(':checked'))");
Well, another way to go is to use
checkBoxMultipleChoice.setDefaultModelObject(listOfAllElements); // Select all.
checkBoxMultipleChoice.setDefaultModelObject(Lists.newArrayList()); // Deselect all.
I know that you fount already an answer but in my case I had exactly the same issue, but the option to use the CheckGroup was not feasible because my list is built dynamically and at the beginning it can even be empty.
The situation for me was:
I was obliged to use the CheckBoxMultipleChoice because it allows me to have empty lists thus reflecting in the GUI. CheckGroups will always print one check box without label, even if the options for the component are empty.
Using AjaxFormComponentUpdatingBehavior would throw an exception saying it was only possible to use it in other components.
Using OnChangeAjaxBehavior would work with the selector CheckBox (select/deselect) but if used with the CheckBoxMultipleChoice then the event will fire but the actual values of the component will not be reflected in the model within the event.
Thanks god I found This Forum in Nabble.
Basically here Pedro Santos propose to use the AjaxFormChoiceComponentUpdatingBehavior instead. I followed the advice and finally I was able to get the values in the model.
Sometimes I think Wicket is Wicked :P