problems with the 2 options joption - java

I'm having problems with these 2 different options, the first one opens up just fine to a browser for future use, but the second option opens the browser and not the dialogue in which i'd like to have open. Also can someone tell me how to make the cancel button cancel out and not open the browser, thanks in advance here's the code.
public static void CheckForUpdates(){
Object[] possibleValues = { "Check for Updates", "Check for version ID" };
Object selectedValue = JOptionPane.showInputDialog(null,
"What would you like to do?", "",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
if ( JOptionPane.YES_NO_OPTION == JOptionPane.YES_OPTION) {
try {
Desktop.getDesktop().browse(new URI("www.google.ca"));
} catch (IOException | URISyntaxException e) {
}
if ( JOptionPane.YES_NO_OPTION == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(frame, "version ID: V1");
}
}
}

Comparing constants is deterministic, i.e. you don't get any variation in behavior.
You need to check the result of showInputDialog which is an Object value. The value is null when nothing (cancel) is selected.
if (selectedValue != null) { // anything selected?
// Check for version ID?
if (selectedValue.toString().equals(possibleValues[1])) {
JOptionPane.showMessageDialog(null, "version ID: V1");
} else {
try {
Desktop.getDesktop().browse(new URI("www.google.ca"));
} catch (IOException | URISyntaxException e) {
}
}
}
Aside: Java naming conventions show that method names start with an lowercase letter such as checkForUpdates.

Related

Printing a message that element is not present in catch block, selenium

This is my Feature file:
#Done #ProdSmoke #KioskSmoke
Scenario Outline: Add Items to the Cart and click Cancel
Given Open the application on Tablet device
When Click on the KeyInItem button
Then Enter Item Number "<upc>"
And Click Cancel
And Item "not added" in the cart
Examples:
| upctype | upc|
| upca | 8 |
This is my stepdef file:
#And("^Item \"([^\"]*)\" in the cart$")
public void itemAddedInTheCart(String action) throws Exception {
addItem.verify_itemadded(action);
}
And this is my function verify_itemadded(action):
public void verify_itemadded(String action) throws Exception {
try {
if (action.equals("added") && element("ItemVerify").isDisplayed()) {
Assert.assertTrue(element("ItemVerify").isDisplayed());
logger.info("Item Added");
}
}
catch (Exception ex)
{
if(action.equals("not added") && !element("ItemVerify").isDisplayed())
logger.info("Item not added");
}
}
Even though the test is passing, I am unable to get the message "item not added".
Any ideas?
You need to update the code as below. Let me know if you face any issue.
try
{
boolean isItemVerified = element("ItemVerify").isDisplayed());
if (action.equals("added") && isItemVerified )
{
logger.info("Item Added");
}
Assert.assertTrue(isItemVerified);
}
catch(AssertionError ae)
{
logger.info("Item not added");
}

Prepared statement inserting String " " into database instead of null

my problem here is that if i try to leave blank spaces in my app, this code inserts an empty string into database on lines txtIme.getText() and txtPrezime.getText() and for that reason my if statement doesn't recognize .executeUpdate() as 0 and therefore doesn't print that nothing has been changed. Can I somehow change it so the app can reach my if statement if blank spaces are left in JTextField? Thank you.
private void btnDodajActionPerformed(java.awt.event.ActionEvent evt) {
try {
izraz = veza.prepareStatement("insert into autor (ime,prezime)" + "value (?,?)");
izraz.setString(1, txtIme.getText());
izraz.setString(2, txtPrezime.getText());
if (izraz.executeUpdate()== 0) {
JOptionPane.showMessageDialog(getRootPane(), "Nothing was changed.");
} else {
ucitajIzBaze();
ocistiPolja();
}
izraz.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
In case you dont want to use any lib
private void btnDodajActionPerformed(java.awt.event.ActionEvent evt) {
try {
if(txtIme.getText() == null || "".equals(txtIme.getText())) return;
if(txtPrezime.getText() == null || "".equals(txtPrezime.getText())) return;
izraz = veza.prepareStatement("insert into autor (ime,prezime)" + "value (?,?)");
izraz.setString(1, txtIme.getText());
izraz.setString(2, txtPrezime.getText());
if (izraz.executeUpdate()== 0) {
JOptionPane.showMessageDialog(getRootPane(), "Nothing was changed.");
} else {
ucitajIzBaze();
ocistiPolja();
}
izraz.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
But I recommend use any library for string related operations like
if(StringUtils.isBlank(txtIme.getText())) return;
if(StringUtils.isBlank(txtPrezime.getText())) return;
It in apache common lang package.

JTable Printing dialog with Locale

im working with Java Swing.
Im trying with print method of Jtable...
public void actionPerformed(java.awt.event.ActionEvent ignore) {
MessageFormat header = new MessageFormat("Page {0,number,integer}");
try {
table.print(JTable.PrintMode.FIT_WIDTH, header, null);
} catch (java.awt.print.PrinterException e) {
System.err.format("Cannot print %s%n", e.getMessage());
}
}
To show a printing dialog . Its work fine ..
The printing dialog
But i want to change the text dialog language to Spanish with a Locale class , how can i do it ???
Thanks!
#Diego
I copied your solution here so it can be more easily read.
It was inspire by the old forum entry here: https://forums.oracle.com/thread/1287832
---- Begin ----
Just adding reflection to change the ResourceBlunde before Jtable.print() method...
try {
Class cl = Class.forName("sun.print.ServiceDialog");
if (cl != null) {
Field fld = cl.getDeclaredField("messageRB");
if (fld != null) {
fld.setAccessible(true);
fld.set(cl, ResourceBundle.getBundle("sun.print.resources.serviceui_es"));
}
}
} catch (Exception ex11) {
ex11.printStackTrace();
}
---- End ----
I may want to search and find it someday.

programmatically sending text message - set expiration

Is it possible to set a text message's (SMS/MMS) expiration date programmatically, or is there a way to send a message that automatically deletes itself through java on android?
I can't seem to find any examples other than applications/websites that work as a middle man, and I would prefer for the end user (receiver) not be required to have the application installed.
For anyone else that gets here --- it looks like the only way that it is possible is to have a middle man that actually manages the messages
There's no such feature as a text-SMS which delete himself afer a certain amount of time.
If you send text-SMS it is very likely to appear in the user inbox, and will only be deleted manually by the user.
If you want your SMS to delete themselves, you'll need to make an application for that.
public boolean deleteSms(String smsId) {
boolean isDelSms = false;
try {
mActivity.getContentResolver().delete(
Uri.parse("content://sms/" + smsId), null, null);
isDelSms = true;
} catch (Exception ex) {
isDelSms = true;
}
return isDelSms;
}
public boolean deleteSms(String smsId) {
boolean isDelSms = false;
try {
mActivity.getContentResolver().delete(
Uri.parse("content://sms/" + smsId), null, null);
isDelSms = true;
Toast.makeText(getApplicationContext(), countryCode,
Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
isDelSms = true;
}
return isDelSms;
}

How can I launch an Eclipse command from Java?

How do I launch a find and replace command without the user clicking edit->find replace or ctrl+F? I need to do this with a plug-in written in Java.
The action that launches the dialog is FindInFileActionDelegate (it has a few sister types for different scopes), this is found in the org.eclipse.search plugin.
The delegates all inherit from a common parent called RetrieverAction. The RetrieverAction's run() method shows the dialog and runs the query. You can take the relevant processing from this method. You may need to register as an ISelectionListener to track the active selection.
public void run() {
IWorkbenchPage page= getWorkbenchPage();
if (page == null) {
return;
}
TextSearchQueryProvider provider= TextSearchQueryProvider.getPreferred();
String searchForString= getSearchForString(page);
if (searchForString.length() == 0) {
MessageDialog.openInformation(getShell(), SearchMessages.RetrieverAction_dialog_title, SearchMessages.RetrieverAction_empty_selection);
return;
}
try {
ISearchQuery query= createQuery(provider, searchForString);
if (query != null) {
NewSearchUI.runQueryInBackground(query);
}
} catch (OperationCanceledException ex) {
// action cancelled
} catch (CoreException e) {
ErrorDialog.openError(getShell(), SearchMessages.RetrieverAction_error_title, SearchMessages.RetrieverAction_error_message, e.getStatus());
}
}

Categories