Handling java errors - java

I linked sqlite to java. I want to show a pop up frame to user if there is an error with database but all errors appear in console! how can I manage them?
try
{
DataBase.databaseConnect("Test");
DataBase.databaseDataQuery(data);
DataBase.databaseDisconnect();
}catch(SQLException e)
{
JOptionPane a=new JOptionPane();
a.showMessageDialog(getParent(), "error");
}

catch SQLException
use JOptionPane.showMessageDialog(..) to show a message
Note that you should only tell the end-user that a problem exists. He doesn't care whether that you have a constraint violation, or a missing column.
The whole stacktrace should be logged to a file, or sent directly to you (the vendor)

Try using JOptionPane.showMessageDialog(....)
http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html

Related

Execute a form after System.exit (0);

I'm trying to close all open forms with System.exit (0); And then call the loggin but I close the execution of the program.
Is there any way to close everything and then call the loggin?
Pd: translated with google translator xD
regards
private void cerrarSesion() {
JOptionPane.showMessageDialog(null, "Se cerrara el sistema", "Advertencia", JOptionPane.PLAIN_MESSAGE, warning);
System.exit(0);
frmLoggin lo = new frmLoggin();
lo.setVisible(true);
}
You should manually keep track of all "forms" in a list somewhere, then just iterate over that list and close them.
If for whatever reason this is not possible you could use JFrame.getWindows() which accesses all Windows in the application
for (Window each : JFrame.getWindows()) {
each.setVisible(false);
}
Note: This may not properly shutdown the forms, e.g. remove listeners/stop threads the forms may be using...

Sending Skype messages in Java, using the java-skype api by taskan

I need help with my java project. I'm currently trying to send a message in a Skype conversation when a specific action happens.
For this, I am using the java-skype API v1.4 by taskan.
Here's my code:
try {
for (Group group : Skype.getContactList().getAllGroups()) {
if ((group.getDisplayName()).equals("Nameofthegroup")) { //Whatever the group name is
String id = group.getId();
Skype.chat(id).send(ep.getDisplayName() + " joins !");
ep.sendMessage("Die ID: "+ id);
}
}
} catch (Exception e3) {
e3.printStackTrace();
}
I've also tried:
try {
String id = Skype.getContactList().getGroup("Groupname").getId();
Skype.chat(id).send(p + "joins!");
} catch (SkypeException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
My problem is that Skype registers that a external program tries to do something, but after I allow access for Java, nothing else happens. No messages are sent.
Sorry for the late answer but assuming you haven't yet picked an answer the problem is still open.
I was trying to get groups the same way with you but unfortunately it doesn't work like this. I do not if this is API problem or just because microsoft dropped support from third party APIs some of its features not working.
I managed to work this around by searching for chats not for groups. Also it would be much easier if you just bookmark (add at favorites) the chat (group) you want to find.
Chat group = null;
for ( Chat c : Skype.getAllBookmarkedChats() ){
group = c;
}
I just have the group chat in my favorites so it is super easy to retrieve it! If you have more chats and you need a more general way to find a specific one there are also several ways to do this.
for (Chat c : Skype.getAllChats()){
c.getAllMembers();
c.getId();
c.getWindowTitle();
}
group = c;
But this would be harder. The getId() way may be look easier but I didn't manage to get it working. Don't know again if it was my problem or just the API but whatever I tried simple just didn't work.
And do not forget to print your results at console to ease yourself.
In the end if you manage to get your group chat it is really easy to send a message:
group.send("Hi chat! This is java!!");
EDIT
This api works only for p2p chats. If you want to create a p2p chat you need to use the /createmoderatedchat command in any chat and it will create a new empty p2p chat. Any other group will be automatic cloud-based.
Also check this
SECOND EDIT
API is completely dead
I don't know too much about the Skype API, but you can check the samples for help. If you want to send a chat message when someone sends you a chat message you can use the AutoAnswering example:
package com.skype.sample;
import com.skype.ChatMessage;
import com.skype.ChatMessageAdapter;
import com.skype.Skype;
import com.skype.SkypeException;
public class AutoAnswering {
public static void main(String[] args) throws Exception {
Skype.setDaemon(false); // to prevent exiting from this program
Skype.addChatMessageListener(new ChatMessageAdapter() {
public void chatMessageReceived(ChatMessage received) throws SkypeException {
if (received.getType().equals(ChatMessage.Type.SAID)) {
received.getSender().send("I'm working. Please, wait a moment.");
}
}
});
}
}
Your code has an undefined variable ep in it and I can't give you a better answer because of that. I would've made a comment asking about it, but Stack Overflow doesn't let new people make comments.

ContentProposalAdapter prevent new line on select proposal

I use the ContentProposalAdapter to provide content assist to my StyledText fields. I wrote an adapter that implements IControlContentAdapter, IControlContentAdapter2 to support the StyledText. My problem is that, when I press return to insert the proposal the return key is inserted into the StyledText and after that the proposal is inserted.
Why are the UP and DOWN arrows not traversed, but the return key is.
How to prevent the return key from begin inserted into the StyledText field when used to select a proposal.
maybe the question is old, but as I googled and this Post nearly covered my problem, but without a solution, I thought to provide my solution I found now.
My Problem was exactly the same but the newline got inserted after the selected proposal.
Selecting the proposal via double click works just fine so I agreed with you that it´s probably the StyledTextWidget that gets notified about the Enter...
First I tried setPropagateKeys(false) on my ContentProposalAdapter, as the doc says it "indicates whether key events (including auto-activation characters) received by the content proposal popup should also be propagated to the adapted control when the proposal popup is open". But this does not work either.
What actually worked for me is the following:
I added an VerifyKeyListener to the StyledTextWidget and just filtered the Enter Event when the ProposalPopup is open. I thought that maybe wouldn´t work as the newline gets inserted after the proposal but on my program it works fine so it seems the closure of the proposal popup is done after the Enter Key is passed to the StyledTextWidget.
Heres the code:
styledText.addVerifyKeyListener(new VerifyKeyListener() {
#Override
public void verifyKey(VerifyEvent arg0) {
try {
KeyStroke k = KeyStroke.getInstance("Enter");
if(k.getNaturalKey() == arg0.keyCode && contentProposalAdapter.isProposalPopupOpen()) {
arg0.doit = false;
}
} catch (ParseException e) {
e.printStackTrace();
}
} });
I don't know how did you implement IControlContentAdapter, IControlContentAdapter2 in your code. Did you try this? I use that in my custom StyledText implementation. But all of them are SWT.SINGLE Text fields. I hope it may help you.

SWT - MessageDialog - Shells

I have an operations class that has no gui. The class basically does data management. The class is called from a method in my Main GUI. The problem I am having is with displaying messages to the user if something fails. I am using MessageDialog, but it keeps failing at runtime. I think the issue is with Shell. When I try to use null as the shell.
MessageDialog.openError(null, "Printer Error Message", "Error getting print reply file.");
The error is null pointer exception
MessageDialog.openError(Display.getCurrent().getActiveShell() etc
The error is null pointer exception
MessageDialog.openError(Display.getDefault().getActiveShell()
The error is invalid thread access
Being this is not a GUI class, do I have to pass in the shell from the GUI parent?
Can I just create a shell in the class and then use that?
You can fix the ERROR_THREAD_INVALID_ACCESS error with Display.syncExec or Display.asyncExec . Try with:
Display.syncExec(new Runnable() {
void run() {
MessageDialog.openError(Display.getDefault().getActiveShell()...
}
}
This will do what you want:
MessageDialog.openError(new Shell(), "Printer Error Message", "Error getting print reply file.");
Just create a new Shell and pass it to the MessageDialog.
Few important points to consider.
First of all, do not mix Data Management classes(models) with UI.
Have a utility class and methods to show errors/info messages.
always access UI widgets in UI thread. Use Display.getDefault().asyncExec() or syncExce()
Check Display.getDefault().getActiveShell() to pass it to the
dialog first, if it is null, create one and pass it.

How to set alert instead of showing unhandled exception in J2ME?

I am designing an application which shows unhandled exception due to lot of reason. So I want my application to show alert in catch block instead.
You could do something like this:
private Alert alert;
private Display display;
// Obtain display with display = Display.getDisplay(this); in consturctor
catch(Exception e) {
alert = new Alert("Error occurred", "Message: " + e.getMessage(), null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, form);
}
Hope this helps.
I think you can just put the alert handling in the catch block:
catch(Exception e) {
// create new alert and
}
The problem I think this guy is having, is that the exceptions are appearing seemingly randomly, i.e. he won't know which piece of code is throwing it.
Some J2ME handsets (e.g. Nokias) have a habit of showing the exception name to the user in an alert, while others (e.g. Sony Ericssons) silently swallow them.
You need to identify all the points at which code can be executed (i.e. all the threads you're creating, and all the MIDP lifecycle methods that the framework could be calling for you), and wrap all of those in try/catch blocks, to ensure that no exceptions can be shown in this way.
That will probably slow your code down a lot though, so you should get to the bottom of what causes these exceptions to appear, and fix the problem!

Categories