Home Ask Login Register

Developers Planet

Your answer is one click away!

Ram Ki February 2016

Button action event in swing java not working?And code queries

Please have patience to read my query Thank you:)

This my code below that have to be generated in GUI form(Using Swing awt) My code work is to read text files from a folder and get repeated words count and save it to a specified folder. It will save files as .xls

import java.io.*; 
import java.nio.charset.Charset; 
import java.nio.charset.StandardCharsets; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
import java.util.StringTokenizer; 
import java.util.Map.Entry; 
public class maxoccurrence2 { 
final static Charset ENCODING = StandardCharsets.UTF_8; 
public Map<String, Integer> getWordCount(String fileName) { 
FileInputStream fis = null; 
DataInputStream dis = null; 
BufferedReader br = null; 
Map<String, Integer> wordMap = new HashMap<String, Integer>(); 
try { 
fis = new FileInputStream(fileName); 
dis = new DataInputStream(fis); 
br = new BufferedReader(new InputStreamReader(dis)); 
String line = null; 
while ((line = br.readLine()) != null) { 
StringTokenizer st = new StringTokenizer(line, " "); 
while (st.hasMoreTokens()) { 
 String tmp = st.nextToken().toLowerCase(); 
 if (wordMap.containsKey(tmp)) { 
 wordMap.put(tmp, wordMap.get(tmp) + 1); 
 } else { 
  wordMap.put(tmp, 1); 
 } catch (FileNotFoundException e) { 
} catch (IOException e) { 
} finally { 
try { 
if (br != null) br.close(); 
} catch (Exception ex) { 
 return wordMap; 
public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap) { 
Set<Entry<String, Integer>> set = wordMap.entrySet(); 
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); 
Collections.sort(list, new Comparator<Map.Entry<String        


justcurious February 2016

This is because you have already overridden the action performed method at the "ChooseDirectory From" at the place you have declared this button but you are expecting it to behave the way you have defined later.

Change the following code:

btnChoosedirectoryfrom = new JButton("ChooseDirectory From");
btnChoosedirectoryfrom.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    FileDialog fd = new FileDialog( f, "Open Box", FileDialog.LOAD );
    btnChoosedirectoryfrom.addActionListener( this );


btnChoosedirectoryfrom = new JButton("ChooseDirectory From");

and it should work. Also note, your button has extra trailing space at the end while the equals command in actionperformed method doesn't have this extra space your that check would fail. You would need to remove this extra space.

Post Status

Asked in February 2016
Viewed 2,453 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life