Java SWT InterruptedException - java

I'm new to java but want to learn and not be spoon-fed so please keep that in mind :)
I'm working with swing to make a GUI for sending mass messages using a Java API for Skype. I've figured out my methods and have working code for the mass messenger and all and now i'm with swing. I've made a few buttons and worked out the front end of the GUI now I need to implement my method.
Here is my code so far:
private void createContents()
throws SkypeException, InterruptedException {
final Mass objCL = new Mass();
objCL.skype();
shell = new Shell();
shell.setSize(450, 300);
shell.setText("Test");
text = new Text(shell, SWT.BORDER);
text.setToolTipText("Your message to send");
text.setBounds(121, 166, 249, 38);
Button btnSend = new Button(shell, SWT.NONE);
btnSend.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
objCL.skype();
}
});
btnSend.setBounds(203, 210, 83, 29);
btnSend.setText("SEND");
Link link = new Link(shell, SWT.NONE);
link.setBounds(10, 250, 83, 15);
link.setText("<a>Our Website</a>");
}
(Code tag left out a '}' Don't worry it is not the problem)
Now the error I have is swing will not allow me to put a
public void widgetSelected(SelectionEvent e) *InterruptedException* {
objCL.skype();
}
Eclipse outputs the error as
Unhandled exception type InterruptedException
Any ideas guys?

Wrap objCL.skype(); in try-catch clause, it is not handled by the outer methods as they have no contextual relationship to each other (the createContents method would have been called, exited and the program moved on a long time before widgetSelected is called)...
try {
objCL.skype();
} catch (InterruptedException exp) {
exp.printStackTrace();
}
See the section on Catching and Handling Exceptions for more details

Related

Java Swing JTextField.getText is not returning a value

I currently am working on a program that inserts into a SQL database. My insert works fine. I created a window to open up with 8 JTextFields for the user to enter their information. However, I am having trouble getting the information out of the JTextField. I get blank values back when I try to print, for instance,var1. Is my syntax wrong? -postToTable is a static method in another class that adds a user to the database.
private void initialize() {
textField_FName = new JTextField();
textField_FName.setBounds(239, 32, 130, 26);
frame.getContentPane().add(textField_FName);
textField_FName.setColumns(10);
vari0 = textField_FName.getText();
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
JOptionPane.showMessageDialog(null, "('"+vari0+"','"+vari1+"','"+vari2+"','"+vari3+"','"+vari4+"','"+vari5+"','"+vari6+"','"+vari7+"')");
DB_Jpanel.postToTable(vari0,vari1,vari2,vari3,vari4,vari5,vari6,vari7);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnSubmit.setBounds(143, 249, 117, 29);
frame.getContentPane().add(btnSubmit);
}
}
You're extracting the JTextField's text by calling getText() on it immediately after you have created the JTextField, and well before the user has had any time to place text within it. That's not how Swing GUI's or any event-driven GUI works. The key is to understand how Event-Driven programming works, and instead extracting the text, e.g., call getText(), from the JTextField in response to an event, here within your button's ActionListener. This code will then be called when the user presses the button, and hopefully after he has placed pertinent text within the JTextField.
So change like:
private void initialize() {
textField_FName = new JTextField();
// ....
// vari0 = textField_FName.getText(); // ****** remove this *****
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
vari0 = textField_FName.getText(); // ***** add this ******
try {
JOptionPane.showMessageDialog(null, "('"+vari0+"','"+vari1+"','"+vari2+"','"+vari3+"','"+vari4+"','"+vari5+"','"+vari6+"','"+vari7+"')");
DB_Jpanel.postToTable(vari0,vari1,vari2,vari3,vari4,vari5,vari6,vari7);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
// ...
}
Notes:
You can add ActionListeners to more than JButtons, and in fact you can add them to JTextFields. Here the listener is activated when the text field has the user's focus and the user presses the ENTER key.
Your code looks to be using null layouts and absolute positioning. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

Java loop only if checkbox is selected

private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 148, 120);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
while(chckbxNewCheckBox.isSelected()){
try {
Robot auto = new Robot();
auto.delay(2300);
auto.mouseMove(377, 182);
auto.mousePress(InputEvent.BUTTON3_DOWN_MASK);
auto.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
//
auto.delay(1000);
auto.mouseMove(466, 293);
auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
//
auto.delay(1000);
auto.mouseMove(1061, 217);
auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
//
auto.delay(8000);
auto.mouseMove(601, 493);
auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
//
auto.delay(60000);
auto.mouseMove(387, 355);
auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
//
auto.delay(8000);
auto.mouseMove(705, 652);
auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
} catch (AWTException e) {
e.printStackTrace();
}
}
} });
btnStart.setBounds(10, 47, 89, 23);
frame.getContentPane().add(btnStart);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
chckbxNewCheckBox.setBounds(2, 7, 97, 23);
frame.getContentPane().add(chckbxNewCheckBox);
}
I'd like some advice on putting the commands for my robot in a loop and the loop only execute when a checkbox is selected. I've tried a few different ways of doing it but none seem to be working. I'm sure I'm missing something simple but I can't seem to find what it was. Although doing it for me would help, explaining it would be great too. I am using eclipse, and the windowbuilder to add items.
Use threads:
You can run your commands in a different thread. The UI will be running in its own thread.
you could write you code in you new thread as ;
Class Robot implements Runnable{
public void run(){
while(programRunning)){
if(checkBox.isSelected())
{
//Perform commands.
}
else {// you may choose to sleep this thread here as well in case of not selected}
}
}
}
programRunning is another variable you can choose so as to keep the loop running even if the checkbox is not selected and the UI is program/application is still running.
You can do something like this:
while(CheckBox.isSelected()) {
// do the stuff
}
Update: I now understand the question. It is good to run long period task in a separate thread, so here you can use SwingWorker to perform the task. If you don't use thread, then the UI became hang by the while loop

Java SWT: Widgets(buttons, labels) used properly

It's my first question here but I'm really stuck. Maybe it's just my extreme fatigue for last few days, but I looked up google for few hours now and couldn't find any close to good answer.
I know SWT is event driven like all GUI's I can think of and when creating widgets I should keep in mind that they need to be able to reach the ones they are supposed to modify/interact with. But what I need to know is if my thinking is right and if not what should I improve.
Let's say I start with eclipse+windowbuilder+swt/jface java project. Then I add button and clabel +click listener for button(SelectionListener), so the generated code looks more or less like(only main method, above there is only Main class and imports)
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
btnNewButton.setBounds(51, 31, 75, 25);
btnNewButton.setText("New Button");
CLabel lblOneTwo = new CLabel(shell, SWT.NONE);
lblOneTwo.setBounds(180, 119, 61, 21);
lblOneTwo.setText("one two");
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
As I know and it's probably obvious to most of you I can't just go and add to
btnNewButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
code like lblOneTwo.setText("three") and from what I know and use sometimes I just declare all stuff beforehand, as static widget widgetName and then I can access them from basically everywhere, so code like
static Button btnNewButton;
static CLabel lblOneTwo;
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
lblOneTwo.setText("three");
}
});
(...)
works just fine. But I guess and think it's not the best practice and way of doing it, isn't it? So please, help me, point me in the right direction so I can stop coding in a sin. Thanks in advance! indirect answers with links to articles/tutorials would be great, but I like examples people are putting here because of their clear way of showing things.
There are multiple ways to do this.
Make the widgets field of your class. Don't make them static unless necessary.
private Label l;
private Button b;
public static void main(String[] args)
{
...
b.addListener(...);
Define all your widgets first (have to be final if you want to use them in the Listener), after that add your Listeners.
final Label l = ...;
final Button b = ....;
b.addListener(...);
If you want to change the widget itself in the Listener you can use Event#widget to get the source of the event.
b.addListener(SWT.Selection, new Listener()
{
public void handleEvent(Event e)
{
Button button = (Button) e.widget;
}
});

Update jTextArea using System.out when program is running

So I've been trying to work with the code in this post for making a console window in a JTextArea. That code seems to be functioning with mine, but I'm running into an odd problem.
My program: I'm basically building a quick and dirty gui for a command line tool I made recently. The only thing that the gui contains is a button that says "Start Automation Engine" and then it has a JTextArea that is supposed to display any text my program sends to System.out.println().
At the moment it displays nothing, though the program itself is running and working (and should be displaying output as a result.) I have noticed that when I click the button on my gui the button stays depressed while the program runs. This has lead me to believe that the JFrame is not updating while the program is running, thus the JTextArea, as it's child, is not updating. This is not so good...
Is there a way to get that JTextArea to update while the program is running in the background?
Here's my code for the JFrame, btw, if you'd like to look at it to get a better idea of what I'm talking about. It was mostly constructed in WindowBuilder in Eclipse. The only thing I did was add a button listener to startAutmoatorEngineButton and then add the last few lines of the initalize() method to set the JTextArea (engineOutput) as the System.out.
public class EngineGUI {
private JFrame frmAutomatorEngine;
private File logPath = new File("<redacted>", "<redacted>");
private File masterFile = new File("<redacted>", "<redacted>");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EngineGUI window = new EngineGUI();
window.frmAutomatorEngine.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public EngineGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmAutomatorEngine = new JFrame();
frmAutomatorEngine.setType(Type.UTILITY);
frmAutomatorEngine.setResizable(false);
frmAutomatorEngine.setTitle("Automator Engine");
frmAutomatorEngine.setBounds(100, 100, 636, 335);
frmAutomatorEngine.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frmAutomatorEngine.setJMenuBar(menuBar);
JMenu mnEngine = new JMenu("Engine");
menuBar.add(mnEngine);
JMenuItem mntmLoadMasterFile = new JMenuItem("Load Master File...");
mnEngine.add(mntmLoadMasterFile);
JMenuItem mntmExit = new JMenuItem("Exit");
mnEngine.add(mntmExit);
frmAutomatorEngine.getContentPane().setLayout(null);
JTextArea engineOutput = new JTextArea();
engineOutput.setBounds(10, 48, 600, 217);
frmAutomatorEngine.getContentPane().add(engineOutput);
JButton startAutomatorEngineButton = new JButton("Start Automator Engine");
startAutomatorEngineButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MasterFile master = null;
try {
master = new MasterFile(masterFile, logPath);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AutomationLoop theLoop = new AutomationLoop(master);
theLoop.startLoop();
}
});
startAutomatorEngineButton.setBounds(441, 11, 169, 23);
frmAutomatorEngine.getContentPane().add(startAutomatorEngineButton);
//Set up textArea to be used as output stream for program
TextAreaOutputStream outputStream = new TextAreaOutputStream(engineOutput);
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);
//System.setErr(printStream);
}
}
It's hard to tell for sure because I don't know what your AutomationLoop and TextAreaOutputStream classes do, but it sounds like a threading problem.
All your Swing code needs to execute in the Event Dispatch Thread. If you have a long running code that is not updating the GUI, then you probably want it running another thread, otherwise the GUI doesn't get a chance to update. From your behavior, it sounds like theLoop.startLoop() is running in the Event Dispatch Thread, and so the GUI never gets a chance to update itself.
Does theLoop.startLoop() start a new thread? If not it probably should; otherwise until that code finishes executing, your GUI will not update.

expected ), illegal start of expression

I can't get my program to compile!
i think im missing a curly brace but can't for the life of me see where!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.net.*;
import java.util.Scanner;
public class AddressBook extends JFrame
{
FlowLayout leftLayout;
JFrame frame;
JPanel panel;
JTextField txtname,txtsurname, txtphone, txtmobile, txtaddress, txtpostcode;
JButton btnadd, btnnext, btnprevious, btnsave, btndelete;
JLabel jlbname, jlbsurname, jlbphone, jlbmobile, jlbaddress, jlbpostcode;
String fileInput,readline;
ArrayList<String> arrayOfFile = new ArrayList<String>();
ArrayList<Contact> records = new ArrayList<Contact>();
int index = 0;
public static void main(String[] args) throws IOException
{
new AddressBook();
}
public AddressBook()
{
//sets window
frame = new JFrame();
frame.setTitle("Bournemouth University Address Book");
frame.setSize(760, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//sets up panel
panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
//Labels
jlbname = new JLabel("Name:");
jlbname.setBounds(10, 50, 100, 20);
panel.add(jlbname);
jlbsurname = new JLabel("Surname:");
jlbsurname.setBounds(350, 50, 100, 20);
panel.add(jlbsurname);
jlbphone = new JLabel("Home Number:");
jlbphone.setBounds(10, 90, 150, 20);
panel.add(jlbphone);
jlbmobile = new JLabel("Mobile:");
jlbmobile.setBounds(350, 90, 150, 20);
panel.add(jlbmobile);
jlbaddress = new JLabel("Address:");
jlbaddress.setBounds(10, 130, 200, 20);
panel.add(jlbaddress);
jlbpostcode = new JLabel("PostCode:");
jlbpostcode.setBounds(10, 170, 250, 20);
panel.add(jlbpostcode);
//Text Fields
txtname = new JTextField("");
txtname.setBounds(120, 50, 200, 20);
panel.add(txtname);
txtsurname = new JTextField("");
txtsurname.setBounds(440, 50, 200, 20);
panel.add(txtsurname);
txtphone = new JTextField("");
txtphone.setBounds(120, 90, 200, 20);
panel.add(txtphone);
txtmobile = new JTextField("");
txtmobile.setBounds(440, 90, 200, 20);
panel.add(txtmobile);
txtaddress = new JTextField("");
txtaddress.setBounds(120, 130, 520, 20);
panel.add(txtaddress);
txtpostcode = new JTextField("");
txtpostcode.setBounds(120, 170, 250, 20);
panel.add(txtpostcode);
//Buttons
btnadd = new JButton("Add", new ImageIcon("../files/add.png"));
btnadd.setBounds(330, 320, 100, 50);
btnadd.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btnadd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
txtname.setText("Add new details here");
txtsurname.setText("");
txtphone.setText("");
txtmobile.setText("");
txtaddress.setText("");
txtpostcode.setText("");
}
});
panel.add(btnadd);
btndelete = new JButton("Delete", new ImageIcon("../files/delete2.png"));
btndelete.setBounds(390, 250, 100, 50);
btndelete.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btndelete.setForeground(Color.red);
// btndelete.addActionListener(this);
panel.add(btndelete);
btnsave = new JButton("Save", new ImageIcon("../files/save.png"));
btnsave.setBounds(490, 250, 100, 50);
btnsave.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btnsave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
BufferedWriter fileOut = new BufferedWriter(new FileWriter("../files/contacts.buab", true));
fileOut.append(txtname.getText());
fileOut.append("\n");
fileOut.append(txtsurname.getText());
fileOut.append("\n");
fileOut.append(txtphone.getText());
fileOut.append("\n");
fileOut.append(txtmobile.getText());
fileOut.append("\n");
fileOut.append(txtaddress.getText());
fileOut.append("\n");
fileOut.append(txtpostcode.getText() + "\r");
fileOut.close();
}
catch (IOException ioe)
{
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
}
});
panel.add(btnsave);
btnprevious = new JButton("Prev", new ImageIcon("../files/left.png"));
btnprevious.setBounds(280, 250, 100, 50);
btnprevious.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btnprevious.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
index--;
displaycontact();
}
});
panel.add(btnprevious);
btnnext = new JButton("Next", new ImageIcon("../files/right.png"));
btnnext.setBounds(180, 250, 100, 50);
btnnext.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
btnnext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
index ++;
displaycontact();
}
});
panel.add(btnnext);
frame.setVisible(true);
panel.setVisible(true);
JMenuBar mb = new JMenuBar();
frame.setJMenuBar(mb);
JMenu insert = new JMenu("Import");
mb.add(insert);
JMenuItem imp = new JMenuItem("Add New Contacts");
insert.add(imp);
imp.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent event)
{
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION)
{
try {
BufferedReader fileStream = new BufferedReader(new FileReader("src/contacts.buab"));
while (true)
{
String fileInput = fileStream.readLine();
if(fileInput==null)
break;
Contact a = new Contact();
a.setname(fileInput);
a.setsurname(fileStream.readline());
a.setphone(fileStream.readLine());
a.setmobile(fileStream.readLine());
a.setaddress(fileStream.readLine());
a.setpostcode(fileStream.readline());
Contacts.add(a);
System.out.println(a.getname());
}
fileStream.close();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
displaycontact();
}});
}
public void displaycontact()
{
txtname.setText(contacts.get(index).name);
txtsurname.SetText(contacts.get(index).surname);
txtphone.setText(contacts.get(index).phone);
txtmobile.setText(contacts.get(index).mobile);
txtAddress.setText(contacts.get(index).address);
}
}
}
please help i've been here for 3 hours!!
You are missing a curly brace here:
displaycontact();
}}}); // <- HERE
This is nearly impossible to see with your code because it is formatted very poorly. You should use a text editor which highlights matching braces. This lets you see quickly what that closing brace closes.
I advise you to reformat the code so that there is proper indentation. You have quite a lot of indentation so you may want to consider using two spaces or a tab size with the tab width set to two. Proper indentation lets you scan the code veritcally to see where the braces are closing things.
I reformatted your code in Eclipse and the ActionListener which is causing your problem now looks like this:
imp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
try {
BufferedReader fileStream = new BufferedReader(new FileReader("src/contacts.buab"));
while (true) {
String fileInput = fileStream.readLine();
if (fileInput == null)
break;
Contact a = new Contact();
a.setname(fileInput);
a.setsurname(fileStream.readline());
a.setphone(fileStream.readLine());
a.setmobile(fileStream.readLine());
a.setaddress(fileStream.readLine());
a.setpostcode(fileStream.readline());
Contacts.add(a);
System.out.println(a.getname());
}
fileStream.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
displaycontact();
}
}
});
Notice that the last curly brace blob is now in three lines and that each line is less indented than the previous? This would make it immediately obvious where you were missing a curly brace.
new should be all lower-case
ioe should be ex
Remove the bottom-most curly-bracket
Add one more closing curly bracket after JOptionPane.showMessageDialog(null, ioe.getMessage()); displaycontact();
Use the prescribed coding conventions, especially the indentation ones
You appear to have a spurious open curly brace in front of public void actionPerformed.
But really, you need to be able to sort these out yourself. You can't post your code to SO every time it doesn't compile... Something that may help here is an editor that can do folding (just about every IDE would do this), or even the functionality that vi has when you press % on a brace/bracket/quote etc. (which jumps to the closing symbol letting you match up start and end).
Plus, whenever you ask a question, always provide the diagnostic information - which in this case would be the compiler output. It's basically rude to expect people to help you while withholding pertinent information.
You're short one closing curly brace after the last displaycontact();
You then need to remove the final curly brace.
addiosamigo, I would really recommend that you be able to deal with these kinds of errors youself, since it will help you much more in the future.
To help in that, here are the steps that I would take to solve this kind of thing:
Delete all the code from the file, except the minimum necessary to compile. See if this compiles (if not, you have a serious problem that's not part of the code. Perhaps a problem with a project definition or something).
Start adding code back in very small chunks.
Each time you add code back, make sure it compiles.
Eventually, you'll add something that doesn't compile, and you'll know the bad code is there.
Please note that doing this is not always so simple, since sometimes you need to add code in a certain order for it to compile.
For example, adding the code for function Foo, which calls function Bar, without adding the code for Bar will obviously cause a compile error. In this kind of case, you'll have to do smart tricks (like adding Bar back in, without any actual implementation, etc).
All in all, the strategy is the same as for every bug: find the smallest case that reproduces it, and work from there.
Hope this helps
The if statement
if (ret == JFileChooser.APPROVE_OPTION)
is missing it's closing brace, you must add it just after
displaycontact();
at the end of the source is an extra brace.
A few hints to help yourself in the future:
make sure your code indenting is correct, fixing your whitespacing and indenting would have lead you to the culprit
anonymous classes can make code unreadable if they consist of more than a few lines, your action listener is a fine example of this. code becomes much more readable if you create a separate nested class for it instead of inserting large anonymous classes.
Edit: to get started have a look at Sun's Java code conventions like and its indenting chapter. IDE's like eclipse supoort formatting and indenting of your code too.

Categories