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.
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:
Quote:
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);
}
}
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.