8 Replies - 224 Views - Last Post: 09 March 2013 - 04:09 PM Rate Topic: -----

#1 rahman1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-March 13

My FTP GUI Frezzes when it can't connect to the host

Posted 08 March 2013 - 06:08 PM

got the JTextArea to show the message when the FTP connection is established, but when it's not connected to the ftp site such as my 4th ftp site which is notes5.bna.com it freezes the program. Also a small problem is if you have the program FTP to a site like this "shlfsdklaflkhasdlhfas". It returns the ftp site is not found only after the JOptionPane shows that FTP is connected. I am not sure what's wrong with it.



Here is my main Class:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import java.io.*;
import java.lang.Process.*;

public class FTP {



  public static void main (String []args)
  {
    Runnable runner = new Runnable(){



      public void run()
      {

        LookAndFeel nimbusLook = new LookAndFeel();
        nimbusLook.NimbusLookAndFeel();

        JFrame frame = new JFrame("BNA FTP Diagnose");
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setSize(540, 420);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(150, 150);


        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        final JMenuItem exitMenuItem = new JMenuItem("Exit");
        fileMenu.add(exitMenuItem);


        JMenu helpMenu = new JMenu("Help");
        menuBar.add(new JPanel());
        menuBar.add(helpMenu);
        final JMenuItem aboutMenuItem = new JMenuItem("About");
        helpMenu.add(aboutMenuItem);



        JPanel titlePanel = new JPanel(new BorderLayout());
        frame.add(titlePanel, BorderLayout.NORTH);

        JLabel titleLabel = new JLabel("FTP Diagnose", JLabel.CENTER);
        titleLabel.setFont(new Font(null, Font.BOLD, 14));
        titleLabel.setForeground(Color.BLUE);
        titlePanel.add(titleLabel);

        JPanel gridPanel = new JPanel(new GridLayout(1, 1));
        frame.add(gridPanel);

        JPanel vendorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        gridPanel.add(vendorPanel);

        final String vendor [] = {"LLesiant" ,"WK-CCH", "Proquest", "Notes", "Research Institute of America", "Thomson", 
          "BNAI PDF Processing", " TM Portfolios to Indexing", "Postscript to PRODLOGIN1", "www.firstdoor.net", "psp.bna.com", "WEST", "LexisNexis", "McArdle Printing Company", 
          "Breaking News Email", "Ex Libris", "Pandora", "Bloomberg Law", "Acquire Media Site 1", "Acquire Media Site 2", "Quicksite", "QA Quicksite"};
        final JComboBox vendorList = new JComboBox(vendor);
        vendorPanel.add(vendorList);

        JPanel diagnoseButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        gridPanel.add(diagnoseButtonPanel);
        final JButton diagnoseButton = new JButton("Diagnose");
        diagnoseButtonPanel.add(diagnoseButton);


        JPanel centerPanel = new JPanel(new BorderLayout());
        frame.add(centerPanel, BorderLayout.SOUTH);
        JPanel commandPanel = new JPanel(new GridLayout(1, 0));
        centerPanel.add(commandPanel);


        final JTextArea commandResultArea = new JTextArea(7, 0);
        JScrollPane scroll = new JScrollPane(commandResultArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        commandPanel.add(scroll);
        commandResultArea.setEditable(false);




        ActionListener buttonListener = new ActionListener(){

          public void actionPerformed(ActionEvent ae)

          {
            int selectedIndex = vendorList.getSelectedIndex();

            String llesiant = "ftp.llesiant.com";
            String wkCCH = "FTP1.cch.com";
            String proquest = "ftp.proquest.com";
            String notes = "notes5.bna.com";


            //String lineThree = null;

            CommandClass readCommand = new CommandClass();

            if (selectedIndex == 0)
            {
              commandResultArea.setText(readCommand.getCommand(llesiant)); //these return strings

            }
            else if (selectedIndex == 1)
            {
              commandResultArea.setText(readCommand.getCommand(wkCCH));
            }
            else if (selectedIndex == 2)
            {
              commandResultArea.setText(readCommand.getCommand(proquest));
            }
            else if (selectedIndex == 3)
            {
              commandResultArea.setText(readCommand.getCommand(notes));
            }

          }

        };
        diagnoseButton.addActionListener(buttonListener);

        ActionListener exitListener = new ActionListener (){

          public void actionPerformed(ActionEvent el)
          {

            if (el.getSource()== exitMenuItem)
            {
              JOptionPane.showMessageDialog(null, "FTP Program will exit now!");
              System.exit(0);
            }

          }

        };

        exitMenuItem.addActionListener(exitListener);

        ActionListener aboutListener = new ActionListener()
        {
          public void actionPerformed(ActionEvent al)
          {

            if (al.getSource()== aboutMenuItem)

            {
              JOptionPane.showMessageDialog(null, "This Software was made for Editors to. \nDiagnose BNA Bloomberg client FTP site.");

            }
          }

        };
        aboutMenuItem.addActionListener(aboutListener);             
      }

    };
    EventQueue.invokeLater(runner);

  }

}


Here is my Look and feel class:
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class LookAndFeel {


    public void NimbusLookAndFeel()
    {

        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }


    }




}



Here is my CommandClass:
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;


public class CommandClass {



    public String getCommand(String ftpSite){
        String command = "ftp ";
        StringBuffer output = new StringBuffer();
        try{
             Process p = Runtime.getRuntime().exec(command +ftpSite);
            InputStreamReader ir = new InputStreamReader (p.getInputStream());
            int outputChar = 0;
            while((outputChar = ir.read()) != -1){
                output.append((char)outputChar);
            if(!ir.ready()){ // If the next read is not guarenteed, come out of loop.
                break;
            }
                }
              ir.close();
             JOptionPane.showMessageDialog(null, "FTP is connected");

        }catch (IOException e){
            e.printStackTrace();
        }
        return output.toString();
    }


}


This post has been edited by Atli: 08 March 2013 - 06:10 PM
Reason for edit:: Use [code] tags around code snippets.


Is This A Good Question/Topic? 0
  • +

Replies To: My FTP GUI Frezzes when it can't connect to the host

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 08 March 2013 - 06:16 PM

Make your connection asynchronously in another thread.
Was This Post Helpful? 0
  • +
  • -

#3 rahman1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-March 13

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 08 March 2013 - 06:20 PM

View Postfarrell2k, on 08 March 2013 - 06:16 PM, said:

Make your connection asynchronously in another thread.

Can you give me an example or show me in my code how to do that. I am a beginner in Java
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 08 March 2013 - 08:09 PM


volatile boolean connected = false;

Thread connection = new Thread() {
    public void run() {
        try {
             //whatever code used to connect.
             connected = true;
        }
        catch(UnknownHostException | IOException e) {
            //error happened.
        }
    }
};

connection.start();




This spawns a new thread to make the connection, so that you event thread of the ui doesn't become unresponsive.
Was This Post Helpful? 0
  • +
  • -

#5 rahman1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-March 13

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 09 March 2013 - 11:45 AM

Rather then writing a void run class can I just add the lines to my getcommand would that work?
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 09 March 2013 - 12:14 PM

You can put it into getCommand(), but you need to execute getCommand from run(). All Threads execute a run() when they are started. There's no way around that.
Was This Post Helpful? 0
  • +
  • -

#7 rahman1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-March 13

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 09 March 2013 - 12:51 PM

Do I call the run class just like I would call the getcommand class in my ain method

Plus run is void and I am tying to return a toting in my getcommand how would all this be possible
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 09 March 2013 - 03:48 PM

I'd advise you to look at the very well documented class javax.swing.SwingWorker
Was This Post Helpful? 1
  • +
  • -

#9 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: My FTP GUI Frezzes when it can't connect to the host

Posted 09 March 2013 - 04:09 PM

Crap! I always forget about SwingWorker!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1