Page 3 of 5 FirstFirst 12345 LastLast
Results 41 to 60 of 87
  1. #41
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What you have tried up to now?
    sir i am just abel to delete item from list box through delete button through this code

    Java Code:
    private void delFromList()
    	{
    		int[] itemIndexes = SERVERS.getSelectedIndexes();
    		
    		for (int i = itemIndexes.length-1; i>=0; i--)
    		{
    			SERVERS.delItem(itemIndexes[i]);
    			
    		}
    		
    		
    	}

  2. #42
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Are you working on a text area or a list box?

    And also I gave the code to write data to a file. What happened there?

  3. #43
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Are you working on a text area or a list box?

    And also I gave the code to write data to a file. What happened there?
    i am working on list box..

    And how i add these code..

  4. #44
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Are you working on a text area or a list box?

    And also I gave the code to write data to a file. What happened there?
    This is my full code

    Java Code:
    import java.awt.*;
    import java.awt.List;
    import java.io.*;
    
    import javax.swing.*;
    import java.util.*;
    import java.io.FileWriter;
    import java.util.ArrayList;
    
    public class IPBlockDlg extends Dialog
    {
    
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private Frame      parent;
    	
    	private Button     OK;
    	private Button     CANCEL;
    	private List       SERVERS;	
    	private TextField  ADDSERVER;	
    	private Button	   ADD;
    	private Button     DELET;
    	private ArrayList<String> data;
        private Scanner dataScan;
        private String strLine;
    	private ArrayList<String>	arp = new ArrayList<String>();
    
    
    	@SuppressWarnings("deprecation")
    	public IPBlockDlg (Frame parent) 
    	{
    		super(parent, "IP Block", true);
    
    		 data = new ArrayList<String>();
    				
    		OK = new Button("OK");
    		CANCEL = new Button("CANCEL");
    		SERVERS = new List(10, true);
    		ADDSERVER = new TextField(15);	
    		ADD = new Button("< Add");
    		DELET = new Button("Delete");
    		
    	 	setLayout(new GridBagLayout());
    		addToBag(this, SERVERS,    0, 0, 1, 3, 1.0,0.0);
    		addToBag(this, ADD,        1, 0, 1, 1, 0.0,0.0);
    		addToBag(this, ADDSERVER,  2, 0, 2, 1, 0.0,0.0);
    		addToBag(this, DELET,     1, 1, 1, 1, 0.0,0.0);
    		addToBag(this, CANCEL,	   2, 4, 1, 1, 0.0,0.0);
    		addToBag(this, OK,         3, 4, 1, 1, 0.0,0.0);
    		
    		
    		pack();
    
    		Point location = parent.location();
    		Point fatherLocation = location;
    		move(fatherLocation.x + 100,fatherLocation.y + 100);
    		show();
    
    	}
    
    
    	private void addToBag(Container container, Object arg, int x, int y, int w, int h,
    							double weightx, double weighty)
    	{
    		GridBagLayout gbl = (GridBagLayout)container.getLayout();
    		GridBagConstraints c = new GridBagConstraints();
    		Component component;
    
    		c.fill = GridBagConstraints.BOTH;
    		c.gridx = x;
    		c.gridy = y;
    		c.gridwidth = w;
    		c.gridheight = h;
    		c.weightx = weightx;
    		c.weighty = weighty;
    
    		component = (Component)arg;
    
    		container.add(component);
    		gbl.setConstraints(component, c);
    	}
    
    
    	@SuppressWarnings("deprecation")
    	public boolean action(Event evt, Object arg) 
    	{
    		
    		if (evt.target instanceof Button) {
    			if (evt.target == OK) {
    				return updateDB();
    			}
    			else if (evt.target == CANCEL) {
    				hide();
    				dispose();
    				return true;
    			}
    			else if (evt.target == ADD) {
    				if (ADDSERVER.getText() != "")  
    					
    				//	SERVERS.addItem(ADDSERVER.getText());
    				
    				try {
            			
        				String str ="xeee.txt";
        				File f = new File(str);
        				if(f.exists()){
        					  FileWriter fstreama = new FileWriter("xeee.txt", true);
        					  BufferedWriter out = new BufferedWriter(fstreama);
        					if(ADDSERVER.getText().equals("")){
        						ADDSERVER.requestFocus();
        					}
        					else{
        						out.write(ADDSERVER.getText());
        						if(f.canWrite()){
        					   
        							ADDSERVER.setText("");
        										    
        							 out.newLine();
        						    
        						    
        						}
        						
        						else{
        						
        						}
        						out.close();
        						}
       					 try{
    
    							FileInputStream fstream = new FileInputStream("xeee.txt");
    
    							DataInputStream in = new DataInputStream(fstream);
    					        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    							
    							 while ((strLine = br.readLine()) != null) 	{
    								 arp.add(strLine);
    								
    						
    							 }	
    							in.close();
    							}catch (Exception e){
    								System.err.println("Error: " + e.getMessage());
    							}
    
    							
    								
    					}
    					else{
    						}
        				
    				}
    			
    			catch(Exception x){
    				x.printStackTrace();
    			}
    		
    			for(int i=0;i<arp.size();i++)
    			 {
    		    	
    				 SERVERS.addItem(arp.get(i).toString());
    
    			 }
    
    				return true;
    			}
        				
    			else if (evt.target == DELET) {
    				delFromList();
    				return true;
    			}
    		
    		}
    		return false;
    	}
    
    
    
    	private void delFromList()
    	{
    		int[] itemIndexes = SERVERS.getSelectedIndexes();
    		
    		for (int i = itemIndexes.length-1; i>=0; i--)
    		{
    			SERVERS.delItem(itemIndexes[i]);
    			
    		}
    		
    		
    	}
    
    
    
    	@SuppressWarnings("deprecation")
    	private boolean updateDB()
    	{
    		
    		hide();
    		dispose();
    		return true;
    	}
    
    		
    	public boolean handleEvent (Event event) 
    	{
    		switch (event.id) {
    		case Event.WINDOW_DESTROY:
    			dispose();
    			return true;
    		default:
    			return super.handleEvent(event);
    		}
    	}
    
    
    
    }

  5. #45
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Add a separate function as I send you in your code. Then call the function with array list which populated with removing duplicates.

  6. #46
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Add a separate function as I send you in your code. Then call the function with array list which populated with removing duplicates.
    ok i am going to use your code then i will told you what a ouput..

  7. #47
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Add a separate function as I send you in your code. Then call the function with array list which populated with removing duplicates.
    Sir when i use your code then nothing happen.Even data not write in file,i am sending code here plz check it where i am wrong

    Java Code:
    public class IPBlockDlg extends Dialog
    {
    
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	private Frame      parent;
    	
    	private Button     OK;
    	private Button     CANCEL;
    	private List       SERVERS;	
    	private TextField  ADDSERVER;	
    	private Button	   ADD;
    	private Button     DELET;
    	private ArrayList<String> data;
        private Scanner dataScan;
        private String strLine;
    	private ArrayList<String>	arp = new ArrayList<String>();
    	
    	 
    
    	@SuppressWarnings("deprecation")
    	public IPBlockDlg (Frame parent) 
    	{	
        
      
    		super(parent, "IP Block", true);
    
    		 data = new ArrayList<String>();
    				
    		OK = new Button("OK");
    		CANCEL = new Button("CANCEL");
    		SERVERS = new List(10, true);
    		ADDSERVER = new TextField(15);	
    		ADD = new Button("< Add");
    		DELET = new Button("Delete");
    		
    	 	setLayout(new GridBagLayout());
    		addToBag(this, SERVERS,    0, 0, 1, 3, 1.0,0.0);
    		addToBag(this, ADD,        1, 0, 1, 1, 0.0,0.0);
    		addToBag(this, ADDSERVER,  2, 0, 2, 1, 0.0,0.0);
    		addToBag(this, DELET,     1, 1, 1, 1, 0.0,0.0);
    		addToBag(this, CANCEL,	   2, 4, 1, 1, 0.0,0.0);
    		addToBag(this, OK,         3, 4, 1, 1, 0.0,0.0);
    		
    		pack();
    
    		Point location = parent.location();
    		Point fatherLocation = location;
    		move(fatherLocation.x + 100,fatherLocation.y + 100);
    		show();
    
    	}
    
    
    	private void addToBag(Container container, Object arg, int x, int y, int w, int h,
    							double weightx, double weighty)
    	{
    		GridBagLayout gbl = (GridBagLayout)container.getLayout();
    		GridBagConstraints c = new GridBagConstraints();
    		Component component;
    
    		c.fill = GridBagConstraints.BOTH;
    		c.gridx = x;
    		c.gridy = y;
    		c.gridwidth = w;
    		c.gridheight = h;
    		c.weightx = weightx;
    		c.weighty = weighty;
    
    		component = (Component)arg;
    
    		container.add(component);
    		gbl.setConstraints(component, c);
    	}
    
    
    	@SuppressWarnings("deprecation")
    	public boolean action(Event evt, Object arg) 
    	{
    		
    		if (evt.target instanceof Button) {
    			if (evt.target == OK) {
    				return updateDB();
    			}
    			else if (evt.target == CANCEL) {
    				hide();
    				dispose();
    				return true;
    			}
    			else if (evt.target == ADD) {
    				if (ADDSERVER.getText() != "")  
    					
    					SERVERS.addItem(ADDSERVER.getText());
    				
    
    				FileInputStream fis = null;
    		        try {
    		            fis = new FileInputStream("xeee.txt");
    		            
    		            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    		            
    		            String line = null;
    		            while((line = br.readLine()) != null){
    		                System.out.println(line);
    		                data.add(line);
    		            }
    		        } 
    		        catch (IOException ex) {
    		            System.out.println(ex.getMessage());
    		        }        
    		        finally {
    		            try {
    		                fis.close();
    		            } 
    		            catch (IOException ex) {
    		                System.out.println(ex.getMessage());
    		            }
    		        }
    			    
    				return true;
    			}
        				
    			else if (evt.target == DELET) {
    				delFromList();
    				 removeData();
    				return true;
    			}
    		
    		}
    		return false;
    	}
    
    	  private void removeData() {
    	        dataScan = new Scanner(System.in);
    	        System.out.println("Which line you want to remove: ");
    	        int i = Integer.parseInt(dataScan.nextLine());
    	        data.remove(i - 1);
    	        writeDataBack();
    	    }
    	
    	  private void writeDataBack() {
    	        FileWriter fw = null;
    	        int x = 0;
    	        try{
    	            fw = new FileWriter("xeee.txt");
    	            BufferedWriter out = new BufferedWriter(fw);
    
    	            while(x < data.size()) {
    	                out.write(data.get(x));
    	                out.newLine();
    	                x++;
    	            }
    
    	            out.close();
    	        }
    	        catch(IOException ioex){
    	            System.out.println(ioex.getLocalizedMessage());
    	        }        
    	    }
    
    	private void delFromList()
    	{
    		int[] itemIndexes = SERVERS.getSelectedIndexes();
    		
    		for (int i = itemIndexes.length-1; i>=0; i--)
    		{
    			SERVERS.delItem(itemIndexes[i]);
    			
    		}
    		
    		
    	}
    
    
    
    	@SuppressWarnings("deprecation")
    	private boolean updateDB()
    	{
    		
    		hide();
    		dispose();
    		return true;
    	}
    
    		
    	public boolean handleEvent (Event event) 
    	{
    		switch (event.id) {
    		case Event.WINDOW_DESTROY:
    			dispose();
    			return true;
    		default:
    			return super.handleEvent(event);
    		}
    	}
    
    
    
    }

  8. #48
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  9. #49
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Tell me what happened on your code. I can't test this at the moment.
    First problem on the add button have write in file

  10. #50
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #51
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    That's right, tell me what happened when you click the Add button.
    when i click on add button then item show in list box but it is not write in file..

  12. #52
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    That's right. In Add click you don't have write data to file. Without implementing that how it works? So you have to implemented it in proper place.

    You want to add duplicate removed data, so simply call the method I explain you that used to write data.

  13. #53
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    That's right. In Add click you don't have write data to file. Without implementing that how it works? So you have to implemented it in proper place.

    You want to add duplicate removed data, so simply call the method I explain you that used to write data.
    sir i am try once again

  14. #54
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    That's right. In Add click you don't have write data to file. Without implementing that how it works? So you have to implemented it in proper place.

    You want to add duplicate removed data, so simply call the method I explain you that used to write data.
    Sir it is wokring but problem is that when i click on delele button then it ask through command line button but i want to delete through list box selection...

    Plz check it

    Java Code:
     private void removeData() {
    		 int[] itemIndexes = SERVERS.getSelectedIndexes();
    			
    			
    			for (int i = itemIndexes.length-1; i>=0; i--)
    			{
    				SERVERS.delItem(itemIndexes[i]);
    				
    			}
    	        dataScan = new Scanner(System.in);
    	        System.out.println("Which line you want to remove: ");
    	        int i = Integer.parseInt(dataScan.nextLine());
    
    	        arp.remove(i - 1);
    	        writeDataBack();
    	    }

  15. #55
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  16. #56
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Use the selected line index to remove from the list.
    Sir i am useing

    Java Code:
     arp.remove(SERVERS.getSelectedIndexes());
    but not working.I mean no delete item from file..

  17. #57
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    This is not deleted the file content at all. This is involve only with the list and the list box. So what you have to do effective those in to the file? Write the list content to the file again. On each modification you have to do this, if you wants to make effect on the file.

  18. #58
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    This is not deleted the file content at all. This is involve only with the list and the list box. So what you have to do effective those in to the file? Write the list content to the file again. On each modification you have to do this, if you wants to make effect on the file.

    But you code delete content from file. Any way sir give me some code for this help plz ..

  19. #59
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm not delete content on the file directly. Delete content from the list and write data from list to the file. That's way you have to follow.

  20. #60
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    I'm not delete content on the file directly. Delete content from the list and write data from list to the file. That's way you have to follow.

    You give me some basic code for do that

Page 3 of 5 FirstFirst 12345 LastLast

Similar Threads

  1. [SOLVED] Delete Current line from file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 04-06-2012, 08:00 AM
  2. Can we lift the images restriction?
    By Cyorxamp in forum Suggestions & Feedback
    Replies: 30
    Last Post: 07-01-2008, 08:32 AM
  3. Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  4. problem when I try to delete a file
    By tommy in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 02:44 PM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 08:11 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
  •