GUI Swing with arraylist

Code under jButton for remove and update

Page 1 of 1

7 Replies - 6682 Views - Last Post: 12 January 2011 - 05:20 PM Rate Topic: -----

#1 Guest_abdalla1*


Reputation:

GUI Swing with arraylist

Posted 11 January 2011 - 02:07 PM

public class DAOVideo {
    
    private ArrayList<Video>videos;
    private String title;
    private Object firstNode;
    private int antal;
    public DAOVideo()
    {
        videos=new ArrayList<Video>();
    }
    public void create (String title, String genre,String id)
    {
        videos.add(new Video(title,genre,id));
    }
    
    public void remove (Video id)
    {
        videos.remove(id);
    }
    
    public Video read (String title)
    {
        Video videon = null;
        for (Video v:videos)
        {
            if (v.title.equalsIgnoreCase(title))
            {
                videon =v;
                break;
            }
        }
        return videon;
    }
    
    public void delet (String title)
    {
        for (Video v:videos)
        {
            if (v.title.equalsIgnoreCase(title))
            {
                videos.remove(v);
            }
        }
    }
    public void update (Video video)
    {
        
        int counter = 0;
        for (Video v:videos)
        {
            if (v.title.equalsIgnoreCase(video.title))
            {
                videos.remove(counter);
                videos.add(counter,video);
                break;
            }
            counter ++;
        }
    }
   public Video []getVideosByGenre(String genre)
   {
       Video[]filmer = new Video [videos.size()];
       int counter = 0;
       for (Video v:videos)
       {
          filmer[counter]=v; 
       }
       counter ++;
         return filmer;
   }

    void delet(int index) {
         for (Video v:videos)
        {
            if (v.title.equalsIgnoreCase(title))
            {
                videos.remove(v);
            }
        }
    }







 public String title;
    public String genre;
    public String id;

    public Video (String title, String genre,String id)
    {
        this.title = title;
        this.genre = genre;
        this.id = id;
    }

   

    
    

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    public void removAll()
    {
        
    }
@Override public String toString()
 {
  String mystring = title+ genre + id  ; 
  return mystring;
 }







private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

   //int index = Integer.parseInt(jTextField1.getText());
    dao.create(jTextField1.getText(), jTextField2.getText(), jTextField3.getText());
    Video[]filmer=dao.getAllVideos();
    jTextArea1.setText(null);
    jTextArea1.append(""+filmer.length+"\n");
    for (Video v:filmer)
    {
        jTextArea1.append(v.title+";"+v.genre+v.id+"\n");
    }




The question here : under JButton1 I can add. but under jButton2 for remove I cant remove. And if you can help also under jButton3 for serach and under juButton 4 for update and juBtton 5 for size.
Note: I used ready Swing pallete.

Thank you again!.
Abdalla1.

This post has been edited by g00se: 11 January 2011 - 02:26 PM


Is This A Good Question/Topic? 0

Replies To: GUI Swing with arraylist

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: GUI Swing with arraylist

Posted 11 January 2011 - 02:23 PM

?
Was This Post Helpful? 0
  • +
  • -

#3 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: GUI Swing with arraylist

Posted 11 January 2011 - 02:23 PM

please next time don'tt write the question inside the code tags.
As for the problem, what do you mean by 'can't remove'?
do you get any error messages? if yes post them.
Also, where is the code for the remove\add\ methods for buttons1 and 2?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: GUI Swing with arraylist

Posted 11 January 2011 - 07:50 PM

import java.util.*;

public class Remove {
    int x;
    
    Remove(int x) {
    	this.x = x;
    }
    public String toString() {
    	return "" + x;
    }
	
    public static void main(String[] args) {
    	ArrayList<Remove> al = new ArrayList<Remove>();
    	for(int i = 0; i <= 5; ++i)
    		al.add(new Remove(i));
    	
    	// you can't remove from an Iterator while it is in progress
    	// it will egenerate a ConcurrentModificationException
    	for(Remove r : al) {
 //   		if(r.x == 3)
 //   			al.remove(r);   <--- won't work
    	}
    	
    	// you\ll have to use the old way
    	for(int i = 0; i < al.size(); ++i) {
    		if(al.get(i).x == 3)
    			al.remove(i);
    	}
    	for(Remove r : al)
    		System.out.println(r);
    	
    	
    }
}



       	// and actuially if you have more than one to remove better to do it in reverse order
    	for(int i = al.size() - 1; i >= 0; --i) {
    		if(al.get(i).x == 3)
    			al.remove(i);
    		if(al.get(i).x == 4)
    			al.remove(i);
    	}


Was This Post Helpful? 0
  • +
  • -

#5 abdalla1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-December 10

Re: GUI Swing with arraylist

Posted 12 January 2011 - 05:49 AM

OK good but as i told you I have jTextField1,jTextField2,jTExtField3, and the jButtons and jTextArea1. and the juBtton remove or search it should be like this but in right way? I need the right code include the jTextFields.as the folowing:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

// this code for add, but I want another for remvo or delet or serach in the same way                                        

   //int index = Integer.parseInt(jTextField1.getText());
    dao.create(jTextField1.getText(), jTextField2.getText(), jTextField3.getText());
   
    Video[]filmer=dao.getAllVideon();
   jTextArea1.setText(null);
    jTextArea1.append(""+filmer.length+"\n");
    for (Video v:filmer)
    {
        jTextArea1.append(v.title+";"+v.genre+v.id+"\n");
    }



Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: GUI Swing with arraylist

Posted 12 January 2011 - 06:15 AM

Sp what's wrong with it ?
Was This Post Helpful? 0
  • +
  • -

#7 abdalla1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 21-December 10

Re: GUI Swing with arraylist

Posted 12 January 2011 - 06:46 AM

ok good but my question how can orgnize this under jButton. I dont how can i but this code under jButton, I need it like this. I have made code for add and I want it for remove also as you said but under jButton with jTextField. as my orginal cod as fowlowing:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

   

   
     dao.create(jTextField1.getText(), jTextField2.getText(), jTextField3.getText());  

    Video[]filmer=dao.getAllVideos();  

     jTextArea1.setText(null);  

    jTextArea1.append(""+filmer.length+"\n");  

    for (Video v:filmer)  

     {  

        jTextArea1.append(v.title+";"+v.genre+v.id+"\n");  
     } 




Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: GUI Swing with arraylist

Posted 12 January 2011 - 05:20 PM

View Postabdalla1, on 12 January 2011 - 07:46 AM, said:

ok good but my question how can orgnize this under jButton. I dont how can i but this code under jButton, I need it like this. I have made code for add and I want it for remove also as you said but under jButton with jTextField. as my orginal cod as fowlowing:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

   

   
     dao.create(jTextField1.getText(), jTextField2.getText(), jTextField3.getText());  

    Video[]filmer=dao.getAllVideos();  

     jTextArea1.setText(null);  

    jTextArea1.append(""+filmer.length+"\n");  

    for (Video v:filmer)  

     {  

        jTextArea1.append(v.title+";"+v.genre+v.id+"\n");  
     } 




AND WHAT IS THE PROBLEM WITH IT ? I HAVE ALREADY REPLIED TO THAT IN ONE OF YOUR MULTIPLE POSTS/THREADS
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1