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.iutputStream;
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!
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.iutputStream;
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!