0

i have created a Java program to save MAC Addresses to an external File. Below is the code:

import java.io.*;
public class MAC{
public static void main(String[] args) throws IOException{
 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter the MAC Addres : ");
 File file = new File("mac.txt");
 FileWriter fstream = new FileWriter("mac.txt",true);
 BufferedWriter out = new BufferedWriter(fstream); 
 out.write(in.readLine());
 out.newLine();
 out.close(); 
 }
}

I have also created a Swing Application. I am done with the Front End, but now I can't save MAC address to a external file using swing.

In my ActionListener, I am getting the values, but I have no idea how to save the details to a external file.

I am able to print the ActionListener values to the screen, but I want it to be saved in the external file.

 import java.awt.BorderLayout;
 import java.awt.FlowLayout;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import java.io.*;
 public class TextForm extends JPanel {
 private JTextField[] fields;
 // Create a form with the specified labels, tooltips, and sizes.
 public TextForm(String[] labels, int[] widths) {
 super(new BorderLayout());
 JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
 JPanel fieldPanel = new JPanel(new GridLayout(labels.length, 1));
 add(labelPanel, BorderLayout.WEST);
 add(fieldPanel, BorderLayout.CENTER);
 fields = new JTextField[labels.length];
 for (int i = 0; i < labels.length; i += 1) {
 fields[i] = new JTextField();
 if (i < widths.length)
 fields[i].setColumns(widths[i]);
 JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
 lab.setLabelFor(fields[i]);
 labelPanel.add(lab);
 JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
 p.add(fields[i]);
 fieldPanel.add(p);
 }
 }
 public String getText(int i) {
 return (fields[i].getText());
 }
 public static void main(String[] args) {
 String[] labels = { "Enter MAC Address : "};
 int[] widths = { 17 };
 final TextForm form = new TextForm(labels, widths);
 JButton submit = new JButton("Submit");
 submit.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 System.out.println(form.getText(0));
 }
 });
 JFrame f = new JFrame("Text Form Example");
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.getContentPane().add(form, BorderLayout.NORTH);
 JPanel p = new JPanel();
 p.add(submit);
 f.getContentPane().add(p, BorderLayout.SOUTH);
 f.pack();
 f.setVisible(true);
 }
 }

Thank you.

john_science
6,5976 gold badges45 silver badges62 bronze badges
asked Apr 22, 2012 at 18:19
2
  • BTW 1) there is no need to extend panel in this case. 2) This GUI would be better suited to being shown in a JOptionPane.showInputDialog() 3) Don't forget to ask a question. Commented Apr 22, 2012 at 18:33
  • 1
    Your thread title should sum up your current problem in a nutshell, not be a generic request for help. Sure you need help, everyone who posts a question in here needs help, but why not tell us in your question title exactly what the problem is. For instance "How to save MAC address to file" or some such. Do this, and you'll perk the interest of folks who are experts on your question with the result that you'll likely get better help quicker. Note also that you don't even have to mention "Java" or "Swing" since the java and swing tags take care of that for you. Luck. Commented Apr 22, 2012 at 18:54

1 Answer 1

5

Copy the working method into the actionPerformed method and swap.

out.write(in.readLine());

For:

out.write(form.getText(0));

Then wrap the lot of it in a try/catch.

answered Apr 22, 2012 at 18:32
Sign up to request clarification or add additional context in comments.

1 Comment

@Rohith "all are welcome to comment on this & support me..." Well, I'd say you have been given first-class treatment here. So how about returning the favor and accepting this succint and to-the-point answer? Or at least comment if you think there's anything wrong with it?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.