I am using swing(JFrame) as UI for my app. I need to display exceptions faced by the program using try/catch to the JFrame’s label with red color.
Currently i’m doing this:
some class extends JFrame
{
JLabel label=new JLabel("");
add(label);
findFile()
{
try{
//some code
}
catch(IOException e)
{
label.setText(e.toString());
}
}
So wanna know if the one line code written with catch block is whether correct or not?
If (yes=ok) thx
else pls show me the way to print the Exception as a label on the JFrame.
Thx in advance..
The above code might not work since it does not seem that the update code is running within the Event Dispatcher Thread (EDT). This thread takes care of updating your UI, responding to events, etc.
You will most likely have to use the SwingUtilities.invokeLater(Runnable run) to update the text.
That being said, most Swing applications (in my knowledge) usually tend to use JOptionPanes to display error message and/or any other notifications to the user.
You should have no problem finding example of both online.