1

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) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 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, Integer>>() { 
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { 
return (o2.getValue()).compareTo(o1.getValue()); 
} 
}); 
 return list; 
} 
public static void main(String a[]) throws IOException { 
String path = "E:\\testfolder\\"; 
String fileNameIn; 
File folder = new File(path); 
File[] listOfFiles = folder.listFiles(); 
for (int i = 0; i < listOfFiles.length; i++) { 
if (listOfFiles[i].isFile()) { 
fileNameIn = path + listOfFiles[i].getName(); 
maxoccurrence2 mdc = new maxoccurrence2(); 
Map<String, Integer> wordMap = mdc.getWordCount(fileNameIn); 
List<Entry<String, Integer>> list = mdc.sortByValue(wordMap); 
String fileNameOutput = path +  "\\output\\"+        
listOfFiles[i] 
.getName() 
.substring(0, listOfFiles[i].getName().length() - 4)+ "output.csv"; 
 try 
 (BufferedWriter writer = Files .newBufferedWriter(Paths.get(fileNameOutput), ENCODING)) { 
    for (Map.Entry<String, Integer> entry : list) { 
    writer.write(entry.getKey() + " =" + entry.getValue()); 
    writer.newLine(); 
} 
} 
} }
} 

From this code output:

ஒரு 10 This is mypresent output.

Now We should save as Unicode format from code to excel. output file example(test.xls)

Inside excel columns,

Serial no word count (static columns)

1 ஒரு 10

Like this i needed.

IN GUI(swing)

1)

In Source path-we should select any folder or particular files and those selections should be displayed in a textarea that shows list of files selected and it should have a check box ""Select all"" and check box for particular files which we are going to select.

2) In destination path (another Jfile chooser) we should set the folder destination path where the output are generated.

If we are generating the same files again and again in that destination, it should prompt saying that ""whether to overwrite or save as another copy".

3) IF we select a folder,the files which are inside the folder should be displayed in the textarea in form of check box.

And we should show the count (no of files ticked). So we can know what we ticked and generate only those files which is needed or the entire files.

After generation we should give acknowledgement message that the process is completed.

Here is my swing program

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;

public class swingpgm3 extends Thread implements ActionListener
{
 JFrame f;

JTextField tf,text1;

JTextArea ta;

JLabel lab1;

String str;

JScrollPane scrol;
 File fl;
private JCheckBox chckbxSelectAll;
private JTextField textField;
private JLabel lblSourceFolderfiles;
private JButton btnChoosedirectoryfrom;
private JButton btnDisplay;
private JLabel lblListFilesBelow;

swingpgm3()
{
f = new JFrame( "Search box" );
f.getContentPane().setLayout( null );
f.setSize( 820, 700 );

tf = new JTextField();
tf.setBounds( 25, 50, 750, 40 );
tf.setFont( new Font( "Latha", Font.BOLD, 20 ) );
tf.setHorizontalAlignment( JTextField.CENTER );
f.getContentPane().add( tf );

ta = new JTextArea();
scrol = new JScrollPane( ta );
ta.setFont( new Font( "Latha", Font.BOLD, 20 ) );
//JScrollPane.setPreferredSize(750,450);
scrol.setBounds( 25, 100, 750, 450 );

f.getContentPane().add( scrol );

chckbxSelectAll = new JCheckBox("Select All");
chckbxSelectAll.setBounds(25, 557, 97, 23);
f.getContentPane().add(chckbxSelectAll);

JButton btnGenerate = new JButton("Generate");
btnGenerate.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {


    }
});
btnGenerate.setBounds(316, 604, 89, 23);
f.getContentPane().add(btnGenerate);

textField = new JTextField();
textField.setBounds(268, 558, 86, 20);
f.getContentPane().add(textField);
textField.setColumns(10);

JLabel lblNoOfFiles = new JLabel("NO of Files Selected");
lblNoOfFiles.setBounds(141, 561, 139, 14);
f.getContentPane().add(lblNoOfFiles);

JLabel lblDestinationFolderTo = new JLabel("Destination PathTo Generate Files");
lblDestinationFolderTo.setBounds(553, 561, 226, 14);
f.getContentPane().add(lblDestinationFolderTo);

JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent arg0) {


    }
});
btnBrowse.setBounds(553, 583, 89, 23);
f.getContentPane().add(btnBrowse);

lblSourceFolderfiles = new JLabel("Source Folder/ Files");
lblSourceFolderfiles.setBounds(6, 17, 138, 14);
f.getContentPane().add(lblSourceFolderfiles);

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.setBounds(141, 9, 170, 30);
f.getContentPane().add(btnChoosedirectoryfrom);

btnDisplay = new JButton("Select To Display");
btnDisplay.setEnabled(false);
btnDisplay.setBounds(534, 9, 180, 30);
btnDisplay.addActionListener( this );

f.getContentPane().add(btnDisplay);

lblListFilesBelow = new JLabel("List files Below to choose ");
lblListFilesBelow.setBounds(344, 17, 180, 14);
f.getContentPane().add(lblListFilesBelow);
f.setVisible( true );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


}

public void actionPerformed( ActionEvent ae )
{
if ( ae.getActionCommand().equals( "ChooseDirectory From" ) )
{
  FileDialog fd = new FileDialog( f, "Open Box", FileDialog.LOAD );


  fd.setSize( 300, 300 );
  fd.setVisible( true );
  str = fd.getDirectory();

  if ( str != null && !str.trim().equals( "" ) )
  {
    tf.setText( str );

    // Enable the search button
    btnDisplay.setEnabled( true );
  }
  else
  {
      btnDisplay.setEnabled( false );
  }
 }

if ( ae.getActionCommand().equals( "btnDisplay" ) )
{
  fl = new File( str );
  File[] flist = fl.listFiles();

  for ( int i = 0; i < flist.length; i++ )
  {
    String newline = System.getProperty( "line.separator" );
    String nm = text1.getText();
    if ( flist[i].getName().toLowerCase().endsWith( nm.toLowerCase() ) )
    {
      if ( flist[i].isFile() )
      {
          ta.setText( ta.getText()+flist[i].getName() + newline );

      }
    }
  }
}
 }

public static void main( String args[] )
 {
new swingpgm3();
 }
}

So far I have done the GUI framework, In my Swing program "ChooseDirectory From" Does not opening file browser.

Before It does open and display file names in the text area.

Can anyone guide from this step

I am a beginner to java Thanks in Advance.

I have attached my java application output image: output image

1
  • Please indent your code properly. Commented Feb 8, 2016 at 6:59

1 Answer 1

1

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 );
}
});

to

btnChoosedirectoryfrom = new JButton("ChooseDirectory From");
btnChoosedirectoryfrom.addActionListener(this);

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.

Sign up to request clarification or add additional context in comments.

4 Comments

ya It worked and "select to display "button should display the files in the text area!.But it doesnt appear now!.
thats because the condition you are checking is ae.getActionCommand().equals( "btnDisplay" )....but the action coomand would be button name i.e. "Select to Display"
I did that too before but Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at swingpgm3.actionPerformed(swingpgm3.java:141) error occurs
Tried debugging your code? It would lead you to answers. For now, look at text1.getText(); where are you populating text1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.