Samantha Cool
Samantha Cool

Reputation: 69

why my swing just hangs when I click button?

I have a form coded in Swing with a JButton which I click to get an action done. But when I click it, my frame hangs. I can't exit it until I end it using the taskbar. What is the problem? Here is my code:

jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
    }
});

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String[] stopwords = {"a", "sometime", "sometimes", "somewhere", "still",
            "such", "system", "take", "ten", "than", "that", "the", "their", 
            "them", "themselves", "then", "thence", "there", "thereafter",
            "thereby", "therefore", "therein", "thereupon", "these", "they",
            "thickv", "thin", "third", "this", "those", "though", "three",
            "through", "throughout", "thru", "thus", "to", "together", "too",
            "top", "toward", "towards", "twelve", "twenty", "two", "un",
            "under", "until", "up", "upon", "us", "very", "via", "was", "we",
            "well", "were", "what", "whatever", "when", "whence", "whenever",
            "where", "whereafter", "whereas","whereby", "wherein", "whereupon",
            "wherever", "whether", "which", "while","whither", "who", "whoever",
            "whole", "whom", "whose", "why", "will", "with", "within", "without",
            "would", "yet", "you", "your", "yours", "yourself", "yourselves",
            "contact", "grounds", "buyers",  "tried", "said,", "plan", "value",
            "principle.", "forces", "sent:", "is,", "was", "like", "discussion",
            "tmus", "diffrent.", "layout", "area.", "thanks", "thankyou",
            "hello", "bye", "rise","fell","fall","psqft.","http://","km","miles"};
    try {
        Scanner fip1 = new Scanner(new File(selectedFile));
        FileOutputStream out=new FileOutputStream("d:/StopWords.txt");
        while(fip1.hasNext()) {
            int flag=1;
            String s1=fip1.next();
            s1=s1.toLowerCase();
            for(int i=0;i<stopwords.length;i++){
                if(s1.equals(stopwords[i])) {
                    flag=0;
                }
            }
            if(flag!=1) {
             // System.out.println(s1);
             // jTextArea1.append("\n"+s1);
             PrintStream p=new PrintStream(out);
             p.println(s1);  
             p.close();
         }
     }
         JOptionPane.showMessageDialog(null,"STOP WORD REMOVAL IS DONE");        
     } catch(Exception e){
         System.err.println("cannot read file");
     }
}                                        

Upvotes: 3

Views: 242

Answers (2)

iGili
iGili

Reputation: 883

I think it's because of the scanner usage. why not using outputStream? regardless of the problem- you should use SwingWorker to do the dirty IO work, and keep the UI responsive.

Upvotes: 1

Peter Wooster
Peter Wooster

Reputation: 6089

That sounds like a loop, Java has very good debugging services, often built into your IDE. You should try those to see if you can track down what's happening. Put a stop where you process the button and follow the code fom here.

You could use a Hashmap for your list of stop words as well, but that's not your bug, just an improvement.

Edit: as @robin pointed out Swing actions must end quickly.

Upvotes: 1

Related Questions