/*trying to set the textfield to null when i input the numbers but the
zero still remains after i start typing.How to i get it to disapear when
i start typing */


package Vectors;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.*;
import javax.swing.*;


public class vectorRef extends JFrame implements ActionListener{

    JLabel label;
    JTextField textfield;
    JButton button,cancel;
    JFrame frame;
    String input = null;



    /*declaire a contructor for the class*/

    public vectorRef(){

        frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,5,10);


        label = new JLabel("Enter ID");
        gbc.gridx = 0;
        gbc.gridy = 1;
        frame.add(label,gbc);


        /*add a textfield to frame*/

        textfield = new JTextField(15);
        gbc.gridx = 1;
        gbc.gridy = 1;
        frame.add(textfield,gbc);
        textfield.setText("0");
        textfield.addActionListener(this);


/*add a button cancel to the frame*/

        cancel = new JButton("Cancel");
        gbc.gridx = 0;
        gbc.gridy = 2;
        frame.add(cancel,gbc);
        cancel.addActionListener(this);

        /*add a button to the frame*/

        button = new JButton("Submit");
        gbc.gridx = 1;
        gbc.gridy = 2;
        frame.add(button,gbc);
        button.addActionListener(this);

        frame.setSize(300,300);
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        if(arg0.getSource() == button){
            Submit();
            textfield.setText("0");
        }
        if(arg0.getSource() == cancel){
            textfield.setText("0");

        }
        // TODO Auto-generated method stub

    }

    Vector<Integer>v = new Vector<Integer>();
    public void Submit(){

        input = textfield.getText();
        int inputS = Integer.parseInt(input);
        v.add(inputS);



        Iterator itr = v.iterator();



        //use hasNext() and next() methods of Iterator to iterate through the elements

        System.out.println("Iterating through Vector elements...");

        while(itr.hasNext())

        System.out.println(itr.next());

        //System.out.println(inputS);



    }

    public static void main(String[] args){
        new vectorRef();
    }



}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

/*trying to set the textfield to null before i input the numbers but the
zero still remains after i start typing.How to i get it to disapear when
i start typing */


package Vectors;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.*;
import javax.swing.*;


public class vectorRef extends JFrame implements ActionListener{

    JLabel label;
    JTextField textfield;
    JButton button,cancel;
    JFrame frame;
    String input = null;



    /*declaire a contructor for the class*/

    public vectorRef(){

        frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,5,10);


        label = new JLabel("Enter ID");
        gbc.gridx = 0;
        gbc.gridy = 1;
        frame.add(label,gbc);


        /*add a textfield to frame*/

        textfield = new JTextField(15);
        gbc.gridx = 1;
        gbc.gridy = 1;
        frame.add(textfield,gbc);
        textfield.setText("0");
        textfield.addActionListener(this);


/*add a button cancel to the frame*/

        cancel = new JButton("Cancel");
        gbc.gridx = 0;
        gbc.gridy = 2;
        frame.add(cancel,gbc);
        cancel.addActionListener(this);

        /*add a button to the frame*/

        button = new JButton("Submit");
        gbc.gridx = 1;
        gbc.gridy = 2;
        frame.add(button,gbc);
        button.addActionListener(this);

        frame.setSize(300,300);
        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        if(arg0.getSource() == button){
            Submit();
            textfield.setText("0");
        }
        if(arg0.getSource() == cancel){
            textfield.setText("0");

        }
        // TODO Auto-generated method stub

    }

    Vector<Integer>v = new Vector<Integer>();
    public void Submit(){

        input = textfield.getText();
        int inputS = Integer.parseInt(input);
        v.add(inputS);



        Iterator itr = v.iterator();



        //use hasNext() and next() methods of Iterator to iterate through the elements

        System.out.println("Iterating through Vector elements...");

        while(itr.hasNext())

        System.out.println(itr.next());

        //System.out.println(inputS);



    }

    public static void main(String[] args){
        new vectorRef();
    }



}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

use the event when the text field gains focus so when you select it, the text is set to null.

private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {
        // TODO add your handling code here:
        jTextField1.setText("");
        //or jTextField1.setText(null);
    }
This article has been dead for over six months. Start a new discussion instead.