Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

help

Thread view: 
dino2747@gmail.com - 08 Apr 2006 02:14 GMT
I have some code that does a little bit of compress in a console but i
want to convert to a GUI. I am having trouble doing that. could some
one look over this code and let me know where i am going wrong.

CODE ::

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.awt.Container;
import java.io.IOException;
import java.lang.Exception;
import java.io.FileNotFoundException;
import java.io.File;
import java.lang.String;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;

public class SmallFileFrame extends JFrame {
   private JTextField textField;
   private JTextField textField1;
   private JList textArea;

   JButton compressJButton;
   JButton decompressJButton;

   static void CopyStream(InputStream in, OutputStream out) throws
IOException {
       while(true) {
           int ch = in.read();
           if(ch == -1) {
               in.close();
               out.close();
               return;
           } // if
           out.write(ch);
       } // while
   } // CopyStream

   public SmallFileFrame() {
       super("Mini Press");
       getContentPane().setLayout(new FlowLayout());
       textField = new JTextField("   Target File   ");
       textField.setFont(new Font("Serif", Font.PLAIN, 14));
       getContentPane().add(textField);

       textField1 = new JTextField(" File destination");
       textField1.setFont(new Font("Serif", Font.PLAIN, 14));
       getContentPane().add(textField1);

       compressJButton = new JButton("Compress");
       decompressJButton = new JButton("Decompress");
       getContentPane().add(compressJButton);
       getContentPane().add(decompressJButton);

       SmallFileHandler handler = new SmallFileHandler();
       compressJButton.addActionListener(handler);
       decompressJButton.addActionListener(handler);

       textArea = new JList();
       textArea.setVisibleRowCount( 5 );
       textArea.setFixedCellWidth( 200 );
       textArea.setFixedCellHeight( 15 );
       getContentPane().add(new JScrollPane( textArea ));
   }
   private class SmallFileHandler implements ActionListener {
       private int valCompress; // = (Options.equals("c"));
       private int valDecompress; // = (Options.equals("d"));
       private int valCompressOrDecompress;

       public void actionPerformed(ActionEvent event) {

           if (event.getSource() == compressJButton) {
               {
                   try {
                       InputStream in = new BufferedInputStream( new
FileInputStream( textField.getText() ) );
                       OutputStream out = new BufferedOutputStream(new
FileOutputStream( textField1.getText() ) );
                       CompressedOutputStream Compressed=new
CompressedOutputStream(out);
                       CopyStream( in, Compressed );
                   } catch ( Exception e ) {
                   }
               }
      // analyzePath();
           }
           if (event.getSource() == decompressJButton) {
               {
                   try {
                       InputStream in = new BufferedInputStream( new
FileInputStream( textField.getText() ) );
                       OutputStream out = new BufferedOutputStream(new
FileOutputStream( textField1.getText() ) );
                       DecompressedInputStream Decompressed=new
DecompressedInputStream(in);
                       CopyStream( Decompressed, out );
                   } catch ( Exception e ) {
                   }
               }
           }
           textField.setFont(new Font("Serif", valCompress +
valDecompress, 14));        // ???
       }
   }
    public void analyzePath()
    {
        File name = getFile();

        if ( textField.exists() )
        {
        textArea.setText(String.format(
        "%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",
        textField.getName(), " exists",
        ( textField.isFile() ? "is a file" : "is not a file" ),
        "Length: ", textField.length(),
        textField1.getName(), " created",
        "Length: ", textField1.length() ) );
        /*if ( name.isDirectory() )
        {
        for ( String directoryName : directory )
        name.append("\n\nDirectory contents:\n");

        for ( String directoryName : directory )
            name.append( directoryName + "\n" );
        }*/
        }
        else
        {
        JOptionPane.showMessageDialog( this, name + " does not exist.",
            "ERROR", JOptionPane.ERROR_MESSAGE );
        }
    }
}

Thanks alot Dino!
Bjorn Abelli - 08 Apr 2006 12:12 GMT
<dino2747@gmail.com> wrotw...

> I have some code that does a little bit of compress in a
> console but i want to convert to a GUI.

Have you tested it properly so you know that
your console version really worked?

> I am having trouble doing that. could some
> one look over this code and let me know where
> i am going wrong.

It would compile and the GUI itself could show, if it weren't for the method
analyzePath.

> public void analyzePath()
> {

You haven't declared any method "getFile" in SmallFileFrame.

>      File name = getFile();

JTextField doesn't have any method "exists"

>      if ( textField.exists() )
>      {

JList doesn't have any method "setText"

> textArea.setText(String.format(
> "%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",

JTextField.getName does probably not do what you expect.

> textField.getName(), " exists",

JTextField doesn't have any method "isFile"

> ( textField.isFile() ? "is a file" : "is not a file" ),

JTextField doesn't have any method "length"

> "Length: ", textField.length(),

JTextField.getName does probably not do what you expect.

> textField1.getName(), " created",

JTextField doesn't have any method "length"

> "Length: ", textField1.length() ) );

>      }
>      else
[quoted text clipped - 4 lines]
> }
> }

I think you want something like...

   public boolean analyzePath(String infilename, String outfilename)
   {
       File infile  = new File(infilename);
       File outfile = new File(outfilename);

       if ( infile.exists() )
       {
          ...

          return true;
       }
       else
       {
          ...

          return false;
       }
   }

...and corresponding changes within the method.

Note that I suggest that you use the strings as arguments, instead of trying
to retrieve them from the GUI *within* the method. And actually I would
suggest that you try to avoid to put the results to the GUI from within the
method as well.

You could e.g. use a boolean return value in order to see if the operation
worked or not...

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.