2 Replies Latest reply: Jan 27, 2007 1:41 PM by 807599 RSS

    working with forms, buttons and text files

    807599
      please am new to java am working on the project. please see sample below, can anyone tell me how i can get the button in form1 to open form2 and also how the information entered in the textfield can be written to a text file. THANKS

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      public class Form1 extends JFrame
      {
           static JFrame frame;
           static JPanel panel;
      JLabel label;
           JTextField text;
           JButton btn;
           GridBagLayout g;
      GridBagConstraints gbc;
           
           public Form1()
           {
                label = new JLabel ("Label");
                gbc.gridx = 0;
      gbc.gridy = 1;
      g.setConstraints(label, gbc);
      panel.add(label);

                text = new JTextField();
      gbc.gridx= 1;
      gbc.gridy = 1;
      g.setConstraints(text, gbc);
      panel.add(text);

                btn = new JButton ("CONTINUE");
                gbc.gridx = 0;
                gbc.gridy = 2;
                g.setConstraints(btn, gbc);
                panel.add(btn);
           }
      }



      import java.awt.event.*;
      public class Form2 extends JFrame
      {
           static JFrame frame;
           static JPanel panel;
      JLabel label;
           JTextField text;
           JButton btn;
           GridBagLayout g;
      GridBagConstraints gbc;
           
           public Form2()
           {
                label = new JLabel ("Label2");
                gbc.gridx = 0;
      gbc.gridy = 1;
      g.setConstraints(label2, gbc);
      panel.add(label2);

                text2 = new JTextField();
      gbc.gridx= 1;
      gbc.gridy = 1;
      g.setConstraints(text2, gbc);
      panel.add(text2);

           }
      }
        • 1. Re: working with forms, buttons and text files
          807599
          please don't cross post
          • 2. Re: working with forms, buttons and text files
            807599
            please am new to java am working on the project.
            please see sample below, can anyone tell me how i can
            get the button in form1 to open form2
            For opening form2 from form1 you can add a actionListener to the button.When the button is pressed instantiate the other class and render it visible which i leave it to you to implement.
            and also how
            the information entered in the textfield can be
            written to a text file. THANKS
            Use getText ( ) method of JTextField to retrieve the text from it and then it's ordinary file io operations for which i guide you to the tutorials here

            http://java.sun.com/docs/books/tutorial/essential/io/index.html