Click to See Complete Forum and Search --> : A little help please.


Bunty Ranu
April 1st, 2010, 03:16 PM
Hey everybody, I'm having a bit of a challenge here with making a program. I have to create a Java Swing GUI program that opens a file as a binary stream and displays the values of each byte in Hex and as readable text. When I click the new button in my GUI it should allow me to browse for a text file, read in the characters, the display the characters alongside their Hex conversion in the text field. The format should be 16 Byte values displayed as 2 digit hex values separated by 2 spaces, then a separator | and then the text representation of these same characters. For the text representation, I need a period or dot for non-printing characters (like a carriage return) and just display the printable ones. An example of how it should be is:

AD EF 00 10 46 56 23 50 AD EF 00 10 46 56 23 50 |..aBntefg.?>..

This is my code so far:


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");

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)
{

}
});

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);
}

}


Thanks in advance for any help guys.

keang
April 2nd, 2010, 07:37 AM
So what is the problem? What does your code not do that it is supposed to do?

If you ask a specific question you are more likely to get the help you are after.

Bunty Ranu
April 2nd, 2010, 07:44 PM
Well my "new" button opens a Jfilechooser which is suppose to be a file and starts reading it in. I need it to the convert bytes to hex and append them to me Jfield as it goes on. Here is my updated code:


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);
}

}

keang
April 3rd, 2010, 05:32 AM
Thanks for trying to post your code correctly but you need to use the code tags and not the quote tags when posting code. The code tags preserve the indentation which makes it a lot easier to read your code.

And please do a little research before posting: Google "java display file as hex" to see several code examples of how to do this sort of thing.