How to Handle Radio button validation and input in JavaFX - java

Using JavaFX 8 with FXML.
I have 2 sets of Radio Buttons; Set 1: A, B, C, D Set 2: X, Y
What I am looking for is
Make sure user checks one Radio button from each set before hitting submit, and prompt the user if they didn't.
Based on the selection, I will write certain data to an array.
For example, for combination A and Y selection, write " some text " to AYcombo array. For combination B and Y write "some text" to BYCombo array. And so forth.

This can easily be done by using bindings and properties. A ToggleGroup defines the selected toggle as a property (selectedToggleProperty) and you can create a BooleanBinding based on this properties:
BooleanBinding binding = groupA.selectedToggleProperty().isNotNull().and(groupB.selectedToggleProperty().isNotNull());
Now you can bind the disable property of the button to this binding:
button.disableProperty().bind(binding.not());
To define the data you can add a listener to the binding (that will be called whenever anything in the binding has changed):
binding.addListener(e -> {
if(binding.getValue()) {
// calculate and set data
}
})

Related

Manually trigger dropdown of CheckComboBox with JavaFX

I have a CheckComboBox that I populate with data I grab from a website with the following method.
public void getCompanies() {
// This method is called every time the user types a letter in the URLText box.
// Grab data from the website and add the data to a list.
HTMLParser p = new HTMLParser(URLText.getText());
List<String> a = p.GetCompanyNames();
// Remove old data so new data can be added.
dropdownMultiple.getItems().remove(0, dropdownMultiple.getItems().size());
for(String element : a) {
dropdownMultiple.getItems().add(element);
}
}
This works just fine but I would like to have the CheckComboBox open the dropdown whenever this method is called. I have a textbox overlayed on top of the CheckComboBox so the user can't click on it. Ultimately I want it to look like an autocomplete dropdown that will dropdown whenever the user types in the text box.
In other words, how can I activate the dropdown event of the CheckComboBox without having the user click on it?
I'm assuming you're talking about org.controlsfx.control.CheckComboBox. Unfortunately, it does not appear the library provides a way to programatically show the popup. But if you don't mind relying on implementation details there is a way to do what you want.
The CheckComboBox's skin uses a JavaFX ComboBox internally. This latter class has a method named show that can be used to manually display the popup. You can get access to this ComboBox via a call to Node.lookup(String).
CheckComboBox<String> box = new CheckComboBox<>();
((ComboBox<?>) box.lookup(".combo-box")).show();
Note: This requires that the CheckComboBox is being displayed in a window.
As a reminder, this deals with implementation details and can therefore break without notice. From looking at the source code this should work for both ControlsFX 8.40.14 and 9.0.0.

How to set up JOptionPane to have a default button and a different focused button

In many native OS X applications, there are dialog boxes that have a default option (in blue) and a different option (in outlined blue) that are initially set. This allows a user to use enter and space, respectively, to choose between two different common options.
I want to be able to replicate this in a java application. Using JOptionPane in the following way produces something close, but the default option (supplied as the initialValue parameter) is both focused and the default; The blue outline (and the button activated via the spacebar) should be on the left-most button, but it isn't.
int showOptionPane(JFrame parent, String file) {
String[] selections = {"Save", "Cancel", "Don't Save"};
return JOptionPane.showOptionDialog(parent,
"Do you want to save changes to: " + file + "?",
"Save Work?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE,
null, selections, selections[0]);
}
I have tried pulling out the source for JOptionPane.showOptionDialog in an attempt to tweak which button got its focus when displayed to the user, but I was having trouble getting at the buttons themselves (since I think those are specified at the pluggable-look-and-feel). How do I get a button focused that is distinct from the default button?

Set/Get values for RadioGroupFieldEditor in SWT

I'm trying to add a RadioGroupFieldEditor in an Eclipse RCP application I'm developing, but seem unable to do two key things:
set the value for the radio button (i.e. when the dialog/window is opened, I'd like to for example set the default to "button1")
get the current value of the selected radio button (i.e. what has been selected by the user, or if nothing has been set, the default value set above).
The code I'm using is as follows:
String[][] radioButtonOptions = new String[][] { { "Button1" "button1" },
{ "Button2" "button2" } };
RadioGroupFieldEditor radioButtonGroup
= new RadioGroupFieldEditor("PrefValue", "Choose Button1 or Button2", 2,
radioButtonOptions, parent, true)
I have a fireValueChanged() method, which I could use to set another String variable with the value (when the user makes a choice), but this just seems messy. It also won't allow me to set the default value...
I suspect I'm missing something significant! Should there be get/set methods for the above?
Since this control is operating on preferences, you can set the default value in your preference initializer.
To get the value of the control, you could gt the actual radio control via the getRadioBoxControl(Composite) method and query that object. Not the cleanest way, but it does work adequately.

How to select radio button when two or three radio buttons are there, using selenium

I am looking for selenium code using java on how to select a particular radio button when multiple radio buttons are there in a form.
For one radio button it is Ok with selenium.click("radio1"), but when in the above case
I.E., I am reading from excel sheet
Please help me in this regard
You can have multiple radio buttons with the same name. Therefore you will need to select either by an id attribute (which must be unique per element), or based on the value attribute (which I can only presume is different)... or by positional index (but this is a somewhat fragile approach)
e.g. use something like this
selenium.click("id=idOfItem");
selenium.click("xpath=//input[#value='Blue']");//select radio with value 'Blue'
Use selenium.check("name=<name> value=<value>");.
Note that <name> is the same for all of the buttons, but <value> will be different.
// get all the radio buttons by similar id or xpath and store in List
List<WebElement> radioBx= driver.findElements(By.id("radioid"));
// This will tell you the number of radio button are present
int iSize = radioBx.size();
//iterate each link and click on it
for (int i = 0; i < iSize ; i++){
// Store the Check Box name to the string variable, using 'Value' attribute
String sValue = radioBx.get(i).getAttribute("value");
// Select the Check Box it the value of the Check Box is same what you are looking for
if (sValue.equalsIgnoreCase("Checkbox expected Text")){
radioBx.get(i).click();
// This will take the execution out of for loop
break;
}
}

GWT Spreadsheet Cell

I am trying to reproduce the behavior of a spreadsheet cell using GWT. I was able to make a Composite widget called "Cell" which is by default a "Label" widget. When a user clicks on this widget, it becomes a "TextBox" widget. On a blur event, the widget becomes a "Label" widget once again.
My question concerns efficiency and rendering time. It would probably be easiest to just make my "Cell" a "TextBox" and just change the appearance to the user via CSS (according to whether they are entering data or not). However, I think that this will affect rendering time and so I revert to a "Label" widget whenever input is not necessary. The problem with this method, however, is that I am basically creating a new TextBox/Label each time the user needs to enter anything into the "Cell".
Here is my pseudo-code (since I am not around an IDE)...
public class Cell extends Composite {
private SimplePanel sp;
public Cell() {
Label l = new Label("");
sp.add(l);
}
private void switchMode() {
Widget w = sp.getWidget();
if (w instanceof Label) {
// we have a Label, change it to a TextBox
String text = ((Label) w).getText();
sp.remove(w);
sp.add(new TextBox(text));
// force garbage collection
w = null;
} else {
// we have a TextBox, change it to a Label
String text = ((TextBox) w).getText();
sp.remove(w);
sp.add(new Label(text));
// force garbage collection
w = null;
}
}
...
When there is a onBlurEvent on the TextBox or when there is an onClick event on the Label, the switchMode() method is called. Critiquing of code is welcomed.
Would it instead be smarter to include a TextBox and Label as private variables of the Cell class, and then just add or remove the corresponding object as needed?
We met similar problem: efficient display of excel-like table (a lot of rows and cols, each cell in-place editable).
The final solution was: render the table as string: each cell is rendered just as text, put all via innerHTML.
When the user selects a cell with mouse or keyboard, special hidden TextArea appeared over the selected cell (with the same size) and focus gives to the TextArea. OnBlur - text entered goes back to the cell and the TextArea is hidden again.
We use no widgets for cells. The TextArea is only one for the whole table.
See also
"Effective GWT: Developing a complex, high-performance app with Google Web Toolkit"
http://code.google.com/events/io/2009/sessions/EffectiveGwt.html
An even easier way would be to have both of them added to your panel (not simplePanel though), and use setVisable methods to alter visability.

Categories

Resources