Search code examples
javapdfactionlistenerjtextfield

Java : Any way to save a string input into pdf


So I have to take a string input from GUI using Textfield and make it show up in a pdf document, when I use ActionListener :

b1[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) 
{
    s=tf1[0].getText();
}
});

When I run the gui and type in the tf1 and press b1, it should save it in s right? But it isn't doing that, however if I just hardcode s="string" it shows up in the pdf. Any ideas?

Forgot to mention I am using iText so the only problem i am having is that the String s is not getting saved from the TextField

Also the code for the actionlistener class if you want it:

public class GUI {
public  static String s;
public static void  gui(){

    {   
        try{
        String File_Name="C:/Users/Ray/Desktop/test.txt";
        ReadFile rf=new ReadFile(File_Name);
        JFrame f1=new JFrame("Maintest");
        JPanel p1=new JPanel();
        JPanel p2=new JPanel();
        final String[] aryLines=rf.OpenFile();
        final JTextField tf1[]=new JTextField[22];
        JButton []b1=new JButton[6];
        String bNames="OK";
        final JTextField tf2[]=new JTextField[aryLines.length];
        f1.setSize(200,450);


        JLabel l1[]=new JLabel[20];

        for ( int i=0; i < aryLines.length; i++ )
        {
            b1[i]=new JButton(bNames);
            l1[i]=new JLabel("Enter Serial# for "+ aryLines[i]);

            p1.add(l1[i]);p1.add(tf1[i] = new JTextField());p1.add(b1[i]);
        }


            p1.setLayout(new BoxLayout(p1,BoxLayout.PAGE_AXIS));

            f1.add(p1,BorderLayout.WEST);



                b1[0].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) 
                {
                    s=""+tf1[0].getText();
                    System.out.println(s);
                }
                });

            f1.show();
        }

            catch(Exception e)
            {
                System.out.print(e);
            }
    }
 }
}

Solution

  • Sorry that I can't answer in more detail, but you could check if the tf1[0].getText() works. Just put a System.out.println("Text: "tf1[0].getText()); into your ActionListener. I'm pretty sure it will be null or "Text: ". For further help you'll have to provide more code.

    Please take a look at this link