Rohith
Rohith

Reputation: 1

Issues in Java Swing

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.

Upvotes: 0

Views: 258

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

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.

Upvotes: 5

Related Questions