Results 1 to 2 of 2
  1. #1
    harshalforu is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default unable to clear text Field in Multi Screen JFrame GUI Application

    i am working with a Swing GUI project where I want to accept user input in a
    text field
    I have used singleton pattern which will create only one instance of object
    due to this when i move from one scree to another the input of textfield
    doesnt updated I have used setText method to clear the JTextField but it wont
    works

    Program one -- 1st screen

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import java.awt.event.*;
    import javax.swing.JTextField;

    public class Frame1 extends JFrame
    {
    public static Frame1 frame1;
    public JButton button;
    public JTextField input;

    public static Frame1 getInstance()
    {
    if(frame1==null)
    frame1 = new Frame1();
    return frame1;
    }

    public JButton getJButton(String mytext)
    {
    JButton button = new JButton();
    button.setText(mytext);
    button.setBounds(450,450,150,50);
    return button;
    }

    public void myGUI()
    {
    JPanel panel = new JPanel();
    panel.setLayout(null);

    button = getJButton("1st Frame");
    addActionListener(button);
    input = new JTextField(10);
    input.setBounds(200,300,100,30);
    panel.add(input);
    panel.add(button);
    add(panel);
    setUndecorated(true);
    setSize(1024, 768);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public void addActionListener(Object obj)
    {
    try{
    JButton button1 = (JButton)obj;
    button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent ae)
    {
    String text = "";
    input.insert("", 0);
    Frame2.getInstance().myGUI();
    Frame2.getInstance().repaint();
    dispose();
    }
    }

    );
    }
    catch(Exception e1)
    {
    System.out.println("Exception==> "+e1.toString());
    }
    }

    public static void main(String[] args)
    {
    Frame1.getInstance().myGUI();
    Frame1.getInstance().repaint();
    }
    }

    program 2 - 2nd screen
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import java.awt.event.*;
    import javax.swing.JTextField;

    public class Frame2 extends JFrame
    {
    static Frame2 frame2;
    public JButton BackButton;
    public JTextField input;

    public static Frame2 getInstance()
    {
    if(frame2==null)
    frame2 = new Frame2();
    return frame2;
    }
    public JButton getJButton(String mytext)
    {
    JButton button = new JButton();
    button.setText(mytext);
    button.setBounds(450,450,150,50);

    return button;
    }
    public void addActionListener(Object obj)
    {
    try{

    JButton BackButton1 = (JButton)obj;
    BackButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent ae)
    {
    input.setText("");
    Frame1.getInstance().myGUI();
    Frame1.getInstance().repaint();
    dispose();

    }
    }

    );
    }
    catch(Exception e2)

    {
    System.out.println("Exception==> "+e2.toString());
    }
    }

    public void myGUI()
    {

    JPanel panel2 = new JPanel();
    panel2.setLayout(null);

    BackButton = getJButton("2nd Screen");
    addActionListener(BackButton);
    input = new JTextField(10);
    input.setBounds(200,300,100,30);
    panel2.add(input);
    panel2.add(BackButton);
    add(panel2);
    setUndecorated(true);
    setSize(1024, 768);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    }

    In this application the status of TextField input remain same means it shows
    the earlier input in the textfield wnen I come to earlier or next screen.
    what is the solution to clear the TextField input while moving from 1st
    scrren to 2nd screen.

  2. #2
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

Similar Threads

  1. how to point cursor on the 1st text field in a jframe
    By VineetKumar in forum AWT / Swing
    Replies: 4
    Last Post: 04-17-2009, 02:58 PM
  2. clear screen prompt using java code
    By krunalpatel1410 in forum Advanced Java
    Replies: 9
    Last Post: 04-04-2009, 12:47 AM
  3. Need For Clear Screen
    By adithya4u4ever in forum New To Java
    Replies: 8
    Last Post: 03-08-2009, 08:41 AM
  4. launching Jframe from text field
    By crunchymonkey in forum AWT / Swing
    Replies: 4
    Last Post: 10-15-2008, 02:50 AM
  5. Clear screen in java
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •