package BinaryFileViewer;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author Bunty
*/
public class BinaryFileViewer extends JPanel implements ActionListener
{
private JTextArea field = new JTextArea(10, 30);
private JScrollPane scrollPane = new JScrollPane(field);
private JButton newButton = new JButton("New");
private JButton quitButton = new JButton("Quit");
JFileChooser chooser = new JFileChooser();
public BinaryFileViewer()
{
JPanel buttonPanel = new JPanel();
buttonPanel.add(newButton);
buttonPanel.add(quitButton);
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.NORTH);
this.add(scrollPane, BorderLayout.CENTER);
newButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
int state = chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
if(file != null && state == JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "Opening: " + file.getPath());
StringBuffer content = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
content.append(text).append(System.getProperty("line.separator"));
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
field.append(content.toString());
}
else if(state == JFileChooser.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null, "Canceled");
}
}
});
quitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0); // exit the application
}
});
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == newButton)
{
}
else if (e.getSource() == quitButton)
System.exit(0);
}
public static void main(String[] argv)
{
JFrame frame = new JFrame("Bunty's Binary File Viewer!");
frame.getContentPane().add(new BinaryFileViewer());
frame.pack();
frame.setSize(550, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}