Question

Sorry in advance if this has been answered elsewhere, i am probably just searching the wrong tags.

I wish to create a log file, of various variables, with the use of an anonymous inner class implementing ActionListener. This will be attached to a JButton.

Using the Formatter, gives me exactly what i require in a line, but i want to keep all previous logs of this event (I dont care if its before or after the last entry).

After various methods of me hitting a wall I found through some surfing of this site and others you can possibly do this with an append method in a constructor with Formatter.

  • Is it possible to use append while in an inner class with Formatter?

  • If not can you suggest another Java writer that will still meet my needs? I'm still a beginner so the less complicated the better...for now.

  • If it's possible within the inner class and with formatter without any additional imports/packages, please give us a hint or a link and i will keep searching.

I have attached a small compilable sample code, that may help if anyone is interested in having a play.

thanks, weekendwarrior84

import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class TestProgram extends JFrame{

private FlowLayout lay;

public TestProgram(){

super("Sample Program");
    lay = new FlowLayout();  
    setLayout(lay);

    final JLabel label1 = new JLabel("Label One");      
    add(label1);           
    final TextField field1 = new TextField(8);      
    add(field1);        
    final JLabel label2 = new JLabel("Exception Label");    
    add(label2);        
    final JButton button1 = new JButton
("Log Data");                           
    add(button1);
    button1.addActionListener(
        new ActionListener(){   
            public void actionPerformed(ActionEvent e){
                if(button1.isSelected());   
                try{
                Formatter fm = new Formatter("C:\\Test\\testlog.txt");                                                              
                fm.format("%s%s%s%s", "Sample Value: ",label1.getText(),
                        "  Sample Value2: ",field1.getText());
                fm.close();
                    }                                                                       
                    catch(Exception ee){                    
                    label2.setText("Make Sure Path exists, C:\\Test\\testlog.txt");                 
                    }
            }
    }
);          
}
}

Main

import javax.swing.JFrame;

public class TestMain{

public static void main  (String[] args){

    TestProgram ts = new TestProgram();

    ts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ts.setSize(1200,500);
    ts.setVisible(true);
}   
}
Was it helpful?

Solution 2

As Promised, it is still a WIP but it solved my current problem. It was mainly confusion on what was possible with the writers, and the belief i required the formatter.

if(button1.isSelected());               
            String path = "C:\\Test\\testlog.txt";
            String mylog = "\r\n"+"Sample Value: " + label1.getText()+"  Sample Value2: "+field1.getText();
            File file = new File(path);
            FileWriter writer;

            try {   
            writer = new FileWriter(file,true);             
            BufferedWriter buffer  = new BufferedWriter(writer);
            writer.append(mylog);                              
            buffer.close();

            }catch (IOException e1) {                           
            label2.setText("Make Sure Path exists, C:\\Test\\testlog.txt");

OTHER TIPS

Is there a specific reason why you want to do this with Formatter? It reads like you want to do some very basic logging, for which there are numerous implementations in Java to do the dirty work for you. Even the java.util.logging package would suffice to append log-lines to a file upon button-click. I'd personally suggest logback, but that's just a preference.

If you do insist on doing the file operations yourself this question's answers might be for you. The formatting can just be done with the MessageFormat-class before writing the complete line to the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top