Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Connecting GUI with application

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connecting GUI with application

    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
    }


  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello... Sorry, I am using a mobile app to access this forum, and I am not in the 'mood'to go to my desktop to analyze your source, so could you please clarify what you are intending to do, so I can help direct you in getting a solution.

    Thanks,

    Michael.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Connecting GUI with application

    My aim to extract and print out sentences from the loaded txt file. The document.java is able to do this but I have pre programmed one txt in it for testing(Legal.txt), which works perfectly fine. The GUI( FileDialogDemo.java) can load any document inside as it has JFileChooser and it work fine.But i need both of this work together. For example, I load any txt file inside the GUI it should be able to preform the works of document.java when i click a button in the GUI. How do i go about it?

Similar Threads

  1. help in connecting to server
    By bothina in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: February 18th, 2012, 11:40 AM
  2. Connecting to VPN
    By raviteja84 in forum Java Networking
    Replies: 0
    Last Post: October 18th, 2011, 05:53 PM
  3. Replies: 1
    Last Post: December 2nd, 2009, 04:01 AM
  4. java application connecting to a server
    By wildheart25c in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2009, 07:22 AM
  5. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM