Getting the name of a JRadioButton in Java - java

I am trying to get the names of all selected JRadioButtons from my graphics interface. As such I create the allFacilities array in which I include all of my JRadioButtons.
The first for loop serves to find the number of selected radio buttons.
The second for loop aspires to get the name of each selected button.
When checking what the .getName() returns:
System.out.println("A##" + button.getName());, only null is returned for all cases.
Here is my code:
JRadioButton[] allFacilities = {restaurant, laundry, parking};
int selectedFacilitiesCounter = 0;
for(JRadioButton check : allFacilities) {
if(check.isSelected()) {
selectedFacilitiesCounter += 1;
}
}
String[] selectedFacilities = new String[selectedFacilitiesCounter];
int index = 0;
for(JRadioButton button : allFacilities) {
if(button.isSelected()) {
System.out.println("A##" + button.getName());
switch(button.getName()) {
case "restaurant":
selectedFacilities[index] = "restaurant";
break;
case "laundry":
selectedFacilities[index] = "laundry";
break;
case "parking":
selectedFacilities[index] = "parking";
break;
default:
System.out.println("Facility Not Found");
}
index += 1;
}
}
Does anybody have any ideas on how I can solve my problem?

I believe that what you want is this:
JRadioButton button = new JRadioButton("test");
System.out.println(button.getText());
Which will print test.
The method getName retrieves the name of the component, which you should've set with setName, which I believe you didn't.

Related

Compare values with Selenium and JXL

The following code reads the spreadsheet cell values ​​with the JXL plugin and then compares these values with the values ​​on the page and chooses the matching value from the combo box.
The code I have works but it is case sensitive, the value must be identical.
I want to improve this code to search the combo box faster and select the closest value without being identical. Currently it runs through all values slowly.
String valDesejado = tipopromocao;
String valorComboBox = "";
Select verificaOpt = new Select(driver.findElement(By.name("tipoDePromocaoPromocao")));
int tamanhoBox = verificaOpt.getOptions().size();
int variavelVerificadora1 = 0;
System.out.println("Tamanho: " + tamanhoBox);
for (int i = 0; i < tamanhoBox; i++)
{
verificaOpt.selectByIndex(i);
valorComboBox = verificaOpt.getFirstSelectedOption().getText().toString();
if (valDesejado.equalsIgnoreCase(valorComboBox))
{
i = tamanhoBox;
variavelVerificadora1 = 1;
}
}
if (variavelVerificadora1 == 0)
{
System.out.println("ALERTA: The Option + valDesejado + " no comboBox \"tipoDePromocaoPromocao\" not found.");
}
I put some comments in the code that explains what I'm doing and makes corrections on a few things.
Instead of using an int and setting it to 0/1, use a boolean and set it to true/false.
This loop should be faster because I'm not selecting each option as I loop. You can examine the text of each option without selecting it... then once you find a match, select the match.
Use break to exit a loop instead of setting the counter to max value.
Give this code a try.
String valDesejado = tipopromocao;
boolean variavelVerificadora1 = false; // use boolean instead of int set to 0/1
Select verificaOpt = new Select(driver.findElement(By.name("tipoDePromocaoPromocao")));
System.out.println("Tamanho: " + verificaOpt.getOptions().size());
// as this loops, the variable 'option' contains the current loops' OPTION element
// you don't need to select the option to get its text so this loop should be much faster
// it selects the OPTION once the correct one is found
for (WebElement option : verificaOpt.getOptions())
{
if (valDesejado.equalsIgnoreCase(option.getText()))
{
verificaOpt.selectByVisibleText(option.getText()); // select the OPTION match
variavelVerificadora1 = true; // set the boolean to true to indicate we found a match
break; // exits the for loop
}
}
if (!variavelVerificadora1) // this is the equivalent of variavelVerificadora1 == false, it's basically saying if not true
{
System.out.println("ALERTA: The Option" + valDesejado + " no comboBox \"tipoDePromocaoPromocao\" not found.");
}

The increment counter in method not working for java

I am developing two separate program which involve method call from each side.Program A is MyNoteCenter.java and program B is SocketServer.java. There consist of a method call in MyNoteCenter to trigger the method in the SocketServer for download the resource, so the counter in SocketServer will increment by 1. when I click the button download inside the MyNoteCenter, it will contact the SocketServer for download request and increment the counter by one if SocketServer receive valid argument but why my counter only rise one time? it will only function well for the first time I click the download button but when I click the second time, the counter still showing 1
This is some portion of my SocketServer program
public String getDownload()
{
int c = 0;
c = c + 1;
switch(software)
{
case "1" :
message = "ITune";
// counter++;
break;
case "2" :
message = "ZoneAlarm";
// counter++;
break;
case "3" :
message = "Winrar";
// counter++;
break;
case "4" :
message = "Audacity";
// counter++;
break;
}
JOptionPane.showMessageDialog(null,"Your download is\n" +message+ "\n the number of download is\n"+c);
return message;
}
This is the method in MyNoteCenter, the method will be trigger after the btn2 is click which is download button, the runCC method will contact the SocketServer method for download
public static void runCC(String software,String id,String name,String job,String country)
{
SocketServer dc = new SocketServer(software,id,name,job,country);
String ServerReplyMessage = dc.getDownload();
JOptionPane.showMessageDialog(null,"Downloading :\n" +ServerReplyMessage);
int answer = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (answer == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null,"Please click close button");
}
else
{
JOptionPane.showMessageDialog(null,"Please proceed");
}
http://codepad.org/ >>my full SocketServer program
You are re initializing c every time you run the method.
c should be a field defined within the class that will maintain value every time you need to increment it OR evaluate it's current value.
public class MyClass {
private int c = 0;
public String getDownload() {
c++;
switch case...
}
}

Code not doing as it should?

public void testDialog()
{
JPanel myPanel = new JPanel();
JTextField tfNames [] = new JTextField[getNumOfPlayers()];
for(int i=0;i < getNumOfPlayers();i++)
{
tfNames[i] = new JTextField(20);
myPanel.add(new JLabel("Player " + (i+1)));
myPanel.add(tfNames[i]);
}
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter The Player Names", JOptionPane.OK_CANCEL_OPTION);
playerNames = new String [getNumOfPlayers()];
if(result == JOptionPane.OK_OPTION)
{
for(int i=0;i < getNumOfPlayers();i++)
{
if(tfNames[i].getText() == null)
{
//NOT GETTING INSIDE HERE FOR ONE REASON OR ANOTHER
System.out.println("NULL FIELD" + i);
testDialog();
}
else
{
playerNames[i] = tfNames[i].getText();
System.out.println(playerNames[i]);
}
}
}
else if (result == JOptionPane.CANCEL_OPTION)
{
numPlayersDialog();
}
else
{
numPlayersDialog();
}
}
Basically I'm trying to check if one of the textFields are blank when the 'OK' button is clicked and if it is recall this method again but for some reason it never gets inside the piece of the code checking to see whether the textField is null or not it skips straight over it everytime even if it is null :/ Can anyone explain why? Set for an hour trying to figure it out and haven't been able to find a reason for it :/ Thanks, for any advice you may have.
PS. If there is away of disabling the 'OK' button while one of more of the textFields are blank please let me know.
Because most likesly tfNames[i].getText() returns an empty string, not null.
Maybe you should check for that instead:
if(tfNames[i].getText() != null && tfNames[i].getText().isEmpty()){
// ...
}
That is because text will never be null, it will be empty so just change your check to :
if (tfNames[i].getText().isEmpty()) {
}
if using sdk lower than 1.6 than use the plain ole checking : tfNames[i].getText().equals("")

How to set defined value for each field present in PDF Form using PDFBOX

How to set defined value for each and every field present in PDF Form,
Consider that I have 5 fields in my PDF form say 2 Text box (First Name and Last Name) two Check box (Check_1,Check_2) , 2 Radio Button(Male,Female) and then at last i have another Text box (Address) , Now I have to define or set a value each and every field say 1 for First Name and 2 for Last Name , 3 for Check_1 and goes on till 7 for Address
Below is the piece of code to define the value for each field but I'm facing some issue while setting up value for Radio button fields
when it comes to Male and Female field it makes displays "1" for male and female for other fields it displays the correct value
Can any one help me out please ...
Thanks
List FieldTypes = form.getFields();
PDField pdfFields;
for (int i = 0; i < FieldTypes.size(); i++) {
pdfFields = (PDField) FieldTypes.get(i);
String type = null;
if (pdfFields instanceof PDTextbox) {
type = "TextBox";
String iAsString = Integer.toString(i);
pdfFields.setValue(iAsString);
System.out.println("Text" + " "+ pdfFields.getFullyQualifiedName());
} else if (pdfFields instanceof PDCheckbox) {
type = "CheckBox";
String iAsString = Integer.toString(i);
System.out.println(iAsString);
System.out.println("CheckBox" + " "+ pdfFields.getFullyQualifiedName());
} else if (pdfFields instanceof PDRadioCollection) {
List kids = pdfFields.getKids();
for (Object kid : kids) {
if (kid instanceof PDCheckbox) {
PDCheckbox checkbox = (PDCheckbox) kid;
String Name = checkbox.getOnValue();
String iAsString = Integer.toString(i);
System.out.println(iAsString);
type = "RadioButton";
System.out.println("RadioButton"+" "+checkbox.getOnValue());
}
}
}
}
Based on doc i think getOnValue() method returns only the status like checked or not checked. Can you try getPartialName(),getValue(),getFullyQualifiedName(),getAlternateFieldName() or any other methods.
With Radio Buttons you have to figure out what the "On Values" are, similar to a Checkbox. However there are multiple On values for Radio Buttons, so its a different method.
For example, if I run this against a Radio Button group where you have to choose from Checking Savings or Money Market, we would see this printed:
Type of Account (org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton)
Radio Button
[Savings, Checking, Money Market]
public void list(PDField field)
{
System.out.println(field.getFullyQualifiedName() + " (" + field.getClass().getName() +")");
if(field instanceof PDRadioButton){
System.out.println("Radio Button");
System.out.println(((PDRadioButton) field).getOnValues());
}
}
If I wanted to have the "Checking" button checked, I would set the field's value equal to "Checking".
Hopefully this helps.

Inserting a variable amidst another variable

I have three textboxes ct1,ct2,ct3. I have to use a for loop 1 to 3 and check whether the textboxes are empty. So, inside the for loop, how do I represent it? For example,
for(i=0;i<=3;i++)
{
if(ct+i.getText()) // I know I'm wrong
{
}
}
I have three textboxes ct1,ct2,ct3.
There's your problem to start with. Instead of using three separate variables, create an array or collection:
TextBox[] textBoxes = new TextBox[3];
// Populate the array...
Or:
List<TextBox> textBoxes = new ArrayList<TextBox>();
// Populate the list...
Then in your loop:
// Note the < here - not <=
for (int i = 0; i < 3; i++) {
// If you're using the array
String text = textBoxes[i].getText();
// or for the list...
String text = textBoxes.get(i).getText();
}
Or if you don't need the index:
for (TextBox textBox : textBoxes) {
String text = textBox.getText();
...
}
Use an array
TextBox[] boxes = new TextBox[]{ct1,ct2,ct3};
for(i=0;i<3;i++)
{
    boxes[i].getText(""); // I know I'm wrong
}
You can put your text boxes in a list and iterate over that list:
List<TextBox> ctList = new ArrayList<TextBox> ();
list.add(ct1);
list.add(ct2);
list.add(ct3);
for (TextBox ct : ctList) {
if(ct.getText().equals("expected text")) {
// do your stuff here
}
}

Categories