Java: How edit button positions in JOptionPane.showInputDialog? - input

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);

Related

New line "\n" does not work yet no errors are given

I have a program that loads a checked list box, then the user selects the items they want and selects a button to say they are done. The checked items are read in a contiguous string with a newline "\n" at the end of each string added. My problem is everything works ok except the newline "\n", and I don't know why.
Code
private: System::Void bntSelected_Click(System::Object^ sender, System::EventArgs^ e) {
int numSelected = this->checkedListBox1->CheckedItems->Count;
if (numSelected != 0)
{
// Set input focus to the list box.
//SetFocus(lstReceiver);
String^ listBoxStr = "";
// If so, loop through all checked items and print results.
for (int x = 0; x < numSelected; x++)
{
listBoxStr = listBoxStr + (x + 1).ToString() + " = " + this->checkedListBox1->CheckedItems[x]->ToString() + "\n";
}
lstReceiver->Items->Add(listBoxStr);
}
}
The string is being formed correctly, but the ListBox control doesn't show newlines in its list items - each item is pushed onto a single line. You can see this by debugging, or by adding a Label to the form with AutoSize set to false - the label will show the newline(s) in the string properly.
Related (C#): How to put a \n(new line) inside a list box?
Instead of ā€˜\nā€™, try ā€˜\r\nā€™. This may be a windows thing.

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()

JOptionPane input problems

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
}

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);
}