tags:

views:

148

answers:

2

How can i add new characters in my JTextArea without duplication…it is when i pressed my add JButton using JAVA..here’s my code i made 2 classes (Form and FormRunner) looking forward for someone who could HELP thnx in advance… SORRY FOR long code -PaLoS(newbie)

in JButtons i really need the HelP most

public class Form extends JFrame{
    int validator = 0;
    public  String ID,Fname,Lname,Course,Year;

    private final static String newline = "\n";
    private final static String newtab = "\t";

    List<Form> myList = new ArrayList<Form>();

    private JTextArea summary,result;
    private JPanel labelPanel,summaryPanel,resultPanel;
    private JLabel iD,fName,lName,yearL,courseL,labelIcon,labelIconS,labelM;
    private JButton searchB,addB,editB,deleteB,updateB;
    private String labels[] = {"ID Number :","First Name :","Last Name :","Course :","Year :"};
    private String namesB[] = {"Search" , "Add" , "Edit" , "Delete" , "Update" };
    private JTextField formTitle,id,fname,lname,year,course;

     Form(){
      super("MAIN FORM: CASE STUDY");
      setupGUI();
    }

     public void setupGUI(){

      Container c =  getContentPane();
      c.setLayout(null);
      ActionEventHandler handler = new ActionEventHandler();

      Insets insets = c.getInsets();

      formTitle = new JTextField("FILE MAINTENANCE ",53);


      labelPanel = new JPanel();

//=====================  LABELS  ===========================
enter code here

//================================ Palos TEXT FIELDS ===================

    id = new JTextField(20);

    id.addKeyListener(new KeyAdapter()
     {
     public void keyTyped(KeyEvent ke){
      char char1 = ke.getKeyChar();
      if((!(Character.isDigit(char1))) && (char1 != '\b') ){ 
        ke.consume(); 
       }
      } 
     }); 

     id.addActionListener(handler);

     fname = new JTextField(20);

     fname.addKeyListener(new KeyAdapter()
      {
public void keyTyped(KeyEvent ke){
    char char1 = ke.getKeyChar();
     if((!(Character.isLetter(char1))) && (char1 != '\b') ) { 
     ke.consume(); 
     } 
    } 
     public void keyReleased(KeyEvent e){} 
     public void keyPressed(KeyEvent e){} 
}); 
    fname.addActionListener(handler);

    lname = new JTextField(20);

    lname.addKeyListener(new KeyAdapter()
    {
     public void keyTyped(KeyEvent ke){
      char char1 = ke.getKeyChar();
    if((!(Character.isLetter(char1))) && (char1 != '\b') ){ 
     ke.consume(); 
     } 
    } 
    public void keyReleased(KeyEvent e){} 
    public void keyPressed(KeyEvent e){} 
}); 
    lname.addActionListener(handler);

    year = new JTextField(20);
    year.addKeyListener(new KeyAdapter()
    {
public void keyTyped(KeyEvent ke){
     char char1 = ke.getKeyChar();
    if((!(Character.isDigit(char1))) &&  (char1 != '\b') ) { 
     ke.consume(); 
    }    
} 
     public void keyReleased(KeyEvent e){} 
     public void keyPressed(KeyEvent e){} 
        }); 
      year.addActionListener(handler);

      course = new JTextField(20);

      course.addKeyListener(new KeyAdapter()
      {
    public void keyTyped(KeyEvent ke){
      char char1 = ke.getKeyChar();     if((!(Character.isLetter(char1))) && (char1 != '\b') ) { 
      ke.consume(); 
      } 
     }
     public void keyReleased(KeyEvent e){} 
     public void keyPressed(KeyEvent e){} 
        }); 
      course.addActionListener(handler);
//=============================== Palos Buttons ===========================


     searchB = new JButton(namesB[0]);
    searchB.setHorizontalAlignment(JTextField.CENTER);
    **addB = new JButton(namesB[1]);
    addB.setHorizontalAlignment(JTextField.CENTER);
    addB.addActionListener(new ActionListener(){

 public void actionPerformed(ActionEvent e){
     id.selectAll();
      fname.selectAll();
      lname.selectAll();
      course.selectAll();
      year.selectAll();       
          String textID = id.getSelectedText();
          String textFName = fname.getSelectedText();
          String textLName = lname.getSelectedText();
          String textCourse = course.getSelectedText();
          String textYear = year.getSelectedText();

      for(int i = 0; i < 1;i++){ 
      for(int j = i;j < myList.size(); j++){
       myList.add(Form.generateForm(textID));
       if( !myList.contains(id.getSelectedText() )){
     summary.append("["+ j + "]"+ textID +newtab + textFName + newtab 
              + textLName + newtab + textCourse + newtab + textYear + newline);
      }
       else{

       continue; 
        }
      }     
      }

      summary.setCaretPosition(summary.getDocument().getLength());

      } 
    });
     editB = new JButton(namesB[2]);
     editB.setHorizontalAlignment(JTextField.CENTER);
     deleteB = new JButton(namesB[3]);
     deleteB.setHorizontalAlignment(JTextField.CENTER);
     updateB = new JButton(namesB[4]);
     updateB.setHorizontalAlignment(JTextField.CENTER);
     updateB.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){

    for(int i = 0; i < myList.size();i++ ){
     summary.add(myList.get(i));
    }

     }
});***

//=================== Palos TextArea ===========================
     summaryPanel = new JPanel();
     summary.setEditable(false);
     resultPanel = new JPanel();

     result = new JTextArea(4,30);
     result.setEditable(false);


     summaryPanel.add(new JScrollPane(summary));
     resultPanel.add(new JScrollPane(result));

//===================================== Palos Container ====================

some code here...

//================================== Palos Setting Of Insets ==================

      setSize(700,400);
      setVisible(true);
      show();;

     }
     public static Form generateForm(final String Id){
      Form stud = new Form();
      stud.setID(Id);
      return stud;   
     }


     private class ActionEventHandler implements ActionListener{
      public void actionPerformed(ActionEvent e){

       if(e.getSource() == addB){

       }
       else if(e.getSource() == updateB){

       }
      } 
     }

     public String getID() {
      return ID;
     }

     public void setID(String id) {
      ID = id;
     }


}
+1  A: 

Use a java.util.Set to keep track of uniques. Before adding a new whatever checkc if it already exists in the Set - if not add to the Set and continue.

mP
A: 

In the keyTyped method of your JTextArea , add the following:

char c = ke.getKeyChar(); 
if(yourTextArea.getText().contains(String.valueOf(c))){
      //duplicate found, consume the event 
}else{
      //no duplicates 
}
Suresh Kumar
Suresh i already tried your suggestion but i think i need to have a condition in my add JButton so that during the validation process in case there's no duplicate characters from my id JtextField will be append in my JTextArea...thnx
PaLoS