help

D

dino2747

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!
 
B

Bjorn Abelli

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
{
JOptionPane.showMessageDialog( this, name + " does not exist.",
"ERROR", JOptionPane.ERROR_MESSAGE );
}
}
}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top