Hi,
I need help to connect my GUI with my application. Both works well individually. When the user clicks on the load function in the GUI the user is should be able to load any txt file in to the system (which is working). The problem is executing the function which is in another java file. How do i go about it? Can someone help? This is the source code:
GUI
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.text.*; import javax.swing.JSeparator; public class FileDialogDemo extends JFrame implements ActionListener{ private JMenuItem jmiOpen; private JMenuItem jmiSave; private JMenuItem jmiExit; private JMenuItem jmiAbout; private JTextArea jta = new JTextArea(); private JLabel jlblStatus = new JLabel(); private JFileChooser jFileChooser = new JFileChooser(); public static void main(String [] args){ FileDialogDemo frame = new FileDialogDemo(); frame.setLocation(100, 200); frame.setTitle("Test JFileChooser"); frame.setSize(300,150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); JPanel jsp1 = new JPanel(); JPanel jsp2 = new JPanel(); JLabel j1 = new JLabel("Original"); JTextArea edit = new JTextArea(10, 60); edit.setLineWrap(true); edit.setWrapStyleWord(true); JLabel j2 = new JLabel("Summarized"); JTextArea edit1 = new JTextArea(10, 60); edit.setLineWrap(true); edit.setWrapStyleWord(true); jsp1.add(j1); jsp1.add(edit); jsp2.add(j2); jsp2.add(edit1); } public FileDialogDemo(){ JMenuBar mb = new JMenuBar(); setJMenuBar(mb); JMenu fileMenu = new JMenu("File"); mb.add(fileMenu); JMenu helpMenu = new JMenu("Help"); mb.add(helpMenu); fileMenu.add(jmiOpen = new JMenuItem("Open")); fileMenu.add(jmiSave = new JMenuItem("Save")); fileMenu.addSeparator(); fileMenu.add(jmiExit = new JMenuItem("Exit")); helpMenu.add(jmiAbout = new JMenuItem("About")); jFileChooser.setCurrentDirectory(new File(".")); getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER); getContentPane().add(jlblStatus, BorderLayout.SOUTH); jmiOpen.addActionListener(this); jmiSave.addActionListener(this); jmiExit.addActionListener(this); jmiAbout.addActionListener(this); } public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (e.getSource() instanceof JMenuItem){ if ("Open" .equals(actionCommand)){ open(); } else if ("Save" .equals(actionCommand)){ save(); } else if ("About" .equals(actionCommand)){ JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs", "About this Demo", JOptionPane.INFORMATION_MESSAGE); } else if ("Exit" .equals(actionCommand)){ System.exit(0); } } } private void open(){ if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){ open(jFileChooser.getSelectedFile()); } } private void open(File file){ try{ BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte [] b = new byte[in.available()]; in.read(b, 0, b.length); jta.append(new String(b, 0, b.length)); in.close(); jlblStatus.setText(file.getName() + " opened"); } catch(IOException ex){ jlblStatus.setText("Error opening file " + file.getName()); } } private void save(){ if (jFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){ save(jFileChooser.getSelectedFile()); } } private void save(File file){ try{ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); byte [] b = (jta.getText()).getBytes(); out.write(b, 0, b.length); out.close(); jlblStatus.setText(file.getName() + " saved "); } catch (IOException ex){ jlblStatus.setText("Error saving " + file.getName()); } } }
Document.java
import java.io.*; import java.util.Scanner; import java.util.TreeSet; import java.util.Iterator; public class Documents { public static void main(String[]args) throws Exception { TreeSet<String>stopList=new TreeSet<String>(); //loadStopWords("StopWords.txt", stopList); //display the whole article's content loadArticle("Legal.txt",stopList); //System.out.println(stopList.toString()); //process one by one //printSet(stopList); } //end main private static void printSet(TreeSet<String>list) { Iterator iter = list.iterator(); System.out.println("Stop List"); System.out.println("-----------"); while(iter.hasNext()) { System.out.println(iter.next()); } } private static void loadStopWords(String filename,TreeSet<String>list) { Scanner src =null; try { src = new Scanner(new FileReader(filename)); while(src.hasNext()) { list.add(src.next()); } } catch (Exception ex) { System.out.print(ex.getMessage()); } src.close(); } //end loadStopWords method private static void loadArticle(String filename, TreeSet<String>list) { Scanner sc=null; int size=0; try { sc=new Scanner(new FileReader(filename)); String word=""; while(sc.hasNext()) { //retrieve a single word word=sc.next(); if(!list.contains(word)) { System.out.print(sc.next()+" "); size ++; //list.add(sc.next()); } } } catch(Exception artLoad) { System.out.println(artLoad.getMessage()); } System.out.println("Total Words: " + size); sc.close(); }//end loadArticle method }