JOptionPane input problems - joptionpane

Ok, so I understand that if you want to test what the user clicks in a JOptionPane, you would do something like this:
final int option = JOptionPane.showConfirmDialog(null, "That file name does not exist.", "File Name", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.CANCEL_OPTION)
{
g++;
}
However, what if I wanted to set a String = to a JOptionPane like this?
String fileName = JOptionPane.showInputDialog("File Name") + ".txt";
How am I supposed to compare what the user clicked then?

you can use as you mentioned
String fileName = JOptionPane.showInputDialog("File Name");
Default input dialog has OK button and cancel buttons. Therefore if you press cancel button
your string value will be null
Otherwise it is the value you entered on textfield
so you can have a if(fileName == null)
to check whether user clicked cancel or OK button
String fileName = JOptionPane.showInputDialog("Enter file name");
if (fileName == null) {
System.out.println("User pressed cancel");
//your logic
}else{
System.out.println(fileName);
//your logic
}

When you're using showInputDialog() you want to get user input from a textfield, not depend on what the user clicked.
You can use either a JOptionPane with an input field
String s = JOptionPane.showInputDialog(null, "Enter a message:);
Or you can use a combobox style JOptionPane
Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
null,
"Complete the sentence:\n"
+ "\"Green eggs and...\"",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"ham");
Edit:
String s = JOptionPane.showInputDialog(null, "Enter a message:);
if (s != null){ // OK is pressed with a value in the field
// do something with s
}

Related

How to perform click on a string value?

I want to click a variable value which is stored in a string and below is my code.
OrderConfirmationData orderConfirmationData= (OrderConfirmationData)Serenity.sessionVariableCalled("OrderConfirmationData");
int maxtries=0;
System.out.println("Order is available in NTI" + EfloristConstants.lastname.get());
List<WebElement> recordcount = getDriver().findElements(By.xpath("//table[contains(#id,'ctrlDWController1_DWDetails1_gvNTI')]/*/tr[not(contains(#style,'bold'))]"));
int j=16;
for(int i=1;i<=recordcount.size();i++) {
j=j*i;
String record = "(//table[contains(#id,'ctrlDWController1_DWDetails1_gvNTI')]/*/tr[not(contains(#style,'bold'))]/td)["+j+"]";
String recipientname= element(By.xpath(record)).getTextValue();
if(recipientname.equals(orderConfirmationData.getShipFirstName() != null)) {
System.out.println("Order is available ");
}
j=16;
}
want to click on the recipientname when the, if the condition matches i.e want to perform click inside the If condition.
Add the below line in if condition:
element(By.xpath(record)).click()

I have 5 fields in a form which are pre populated. Using Selenium web driver how can I verify which one of the field is empty

I have a test case to verify a form. There are 5 fields which will be prepopulated. If any one of the field is empty, after submitting error message will come. I am fetching the text from all the fields and asserting if they are not null, asserting is failing since one field is null in these. But how to find out dynamically which field is empty.
There might be a possibility to take each field and assert each field if empty or not. but I guess that will make the code clumsy.
Any other ways by which I can do this?
Following is my code :
String sender = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$textsendername1")).Text;
String country = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$ddlCountry")).Text;
String address1 = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$txtsenderaddress1")).Text;
String city = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$txtcity")).Text;
String state = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$ddlstate")).Text;
String zip = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$txtzip")).Text;
try
{
Assert.IsNotEmpty(sender, country, address1, city, state, zip);
}
catch (Exception e)
{
String excep = e.ToString();
Console.WriteLine(excep);
}
I would create a function that determines if any of the fields are empty since you will likely be using this repeatedly.
public bool HasEmptyField()
{
string sender = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$textsendername1")).GetAttribute("value");
string country = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$ddlCountry")).GetAttribute("value");
string address1 = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$txtsenderaddress1")).GetAttribute("value");
string city = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$txtcity")).GetAttribute("value");
string state = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$ddlstate")).GetAttribute("value");
string zip = driver.FindElement(By.Name("ctl00$ContentPlaceHolder$txtzip")).GetAttribute("value");
return string.IsNullOrEmpty(sender) ||
string.IsNullOrEmpty(country) ||
string.IsNullOrEmpty(address1) ||
string.IsNullOrEmpty(city) ||
string.IsNullOrEmpty(state) ||
string.IsNullOrEmpty(zip);
}
Your script would then look something like
bool expectError = HasEmptyField();
driver.FindElement(submitButtonLocator).Click(); // click submit button
if (expectError)
{
Assert.True(driver.FindElements(errorMessageLocator).Any(), "Verify error message exists");
// do something to address the error message
}
// continue the script

Java: How edit button positions in JOptionPane.showInputDialog?

How do you edit the button positions in the JOptionPane.showInputDialog pop up?
Are there arguments that you can put into the parameters?
String inputValue = JOptionPane.showInputDialog(frame.getFrame(), "Please enter a value from 1 to 9");
if (inputValue == null) { // Cancel button
return 0;
}
Apparently I just had to add extra arguments to make it a plain message, but how do you reposition the buttons?
String inputValue = JOptionPane.showInputDialog(frame.getFrame(), "Please enter a value from 1 to 9","", JOptionPane.PLAIN_MESSAGE);

how to remove NullExceptionError from Session Variable

I have a varible sem,which takes inputfrom the textbox but if the textbox is kept empty i want it to return "Please Enter a Semester"
int sem;
int parsevalue;
if (int.TryParse(TextBox2.Text, out parsevalue))
{
sem = parsevalue;
Session["Sem"] = sem;
}
else
{
Literal2.Text = "Please Enter a Semester";
}
But If the Textbox is empty the Session["Sem"] returns NullExceptionError in the .aspx file where i have used it.
I searched for the proper conversion using tryparse but could not understand it clearly as to how to print the above mentioned error message.
please help
Thank you in advance
The problem here is that you're assigning the Session variable only when there is a correct value, but you're always trying to access it. This will fail if the value is incorrect (and the Session variable is not set).
Here's your corrected code:
int sem;
int parsevalue;
if (int.TryParse(TextBox2.Text, out parsevalue))
{
sem = parsevalue;
}
else
{
Literal2.Text = "Please Enter a Semester";
}
//Always set the Session variable when it's used somewhere else
Session["Sem"] = sem;
Try:
HttpContext.Current.Session["Sem"] = sem;
Actually:
Session.Add("Sem", sem);
and
Session["Sem"] = sem;
is a same
Q1 Hi , I have a varible sem,which takes inputfrom the textbox but if the textbox is kept empty i want it to return "Please Enter a Semester"
int sem;
int parsevalue;
var txt = TextBox2.Text
if (!String.IsNullOrEmpty(text) && int.TryParse(text, out parsevalue))
{
sem = parsevalue;
}
else
{
Literal2.Text = "Please Enter a Semester";
}
Session["Sem"] = sem;

Insert text into flex 3 textarea

I have a textArea and a list. When a user double clicks a list item, the label of the selected item should be inserted into the textarea. When a text is selected in the textArea, it should be replaced, otherwise the text just needs to be inserted into the existing text at the caret point.
I've managed to get the text and everything, I just can't manage to insert it at the caret point. Does anyone know how to do this?
It's actually not JavaScript but Adobe Flex 3. Thanks for the help though, it did push me in the right direction. This is the way its done in Flex 3:
var caretStart:int = textArea.selectionBeginIndex;
var caretEnd:int = textArea.selectionEndIndex;
textArea.text = textArea.text.substring(0,caretStart)
+ newText
+ textArea.text.substr(caretEnd);
The accepted answer works great if you do not have existing HTML formatting. In my case, I inserted a new button into the editor that the user could click to put in a key word. I kept losing all HTML formatting until I dug around in the actual class and sided with a TextRange object:
public function keyWord_Click(event:Event) : void
{
var caretStart:int = txtEditor.textArea.selectionBeginIndex;
var caretEnd:int = txtEditor.textArea.selectionEndIndex;
var newText : String = "[[[KEYWORD]]]";
var tf:TextRange = new TextRange(txtEditor,true,caretStart,caretEnd);
tf.text = newText;
}
The nice thing about this approach is, you can also apply conditional formatting to that TextRange object as needed.
You can use txtarea.selectionStart and txtarea.selectionEnd to get Selected text position.
After that, You delete txt and add new selected text.
I don't known much about Javascript, so I wrote it for U.
You can search on google with keywords:
"Javascript Selected Text TextArea"
"Javascript add text at position"
Sample code:
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
caretPos = doGetCaretPosition(myField);
alert(caretPos);
setCaretPosition(myField,caretPos-3);
}