CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    12

    Smile A little help please.

    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.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: A little help please.

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2010
    Posts
    12

    Re: A little help please.

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

    }
    Last edited by Bunty Ranu; April 3rd, 2010 at 12:07 AM.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: A little help please.

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured