Page 5 of 5 FirstFirst ... 345
Results 81 to 87 of 87
  1. #81
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    So waiting for me. :confused: I'm really happy that if you have tried something.

    Anyway, I'll give the code. As much as possible I'll try to keep your requirements as much as possible. And also try to keep the code more simple, violating some OO concepts too. Have to change some parameters also, like the read file name(actually the path). You should give 100 mark for this:D

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.ArrayList;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class SarinamDemo extends Dialog {
    
        private Button ok, cancel, add, delete;
        private TextField textField;
        private List list;
        private ArrayList<String> data;
        private boolean isFind;
        
        public SarinamDemo(Frame perent) {
            super(perent, "Demo" , true);
            
            readFile();
            
            ok = new Button("Ok");
            cancel = new Button("Cancel");
            add = new Button("<<Add");
            delete = new Button("Delete");
            textField = new TextField(15);
            list = new List(10, true);
            
            setLayout(new GridBagLayout());
            addToContainer(this, list, 0, 0, 1, 3, 1,0);
            addToContainer(this, add, 1, 0, 1, 1, 0, 0);
            addToContainer(this, textField, 2, 0, 2, 1, 0,0);
            addToContainer(this, delete, 1, 1, 1, 1, 0, 0);
            addToContainer(this, cancel, 2, 4, 1, 1, 0, 0);
            addToContainer(this, ok, 3, 4, 1, 1, 0, 0);
            
            populateData();        
            pack();
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            setResizable(true);
            setVisible(true);
    
        }
    
        private void addToContainer(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);
        }
        
        @Override
        public boolean action(Event evt, Object arg) {
            if(evt.target == add) {
                addUserData();
                return true;
            }
            else if(evt.target == delete) {
                removeUserData();
            }
            else if(evt.target == ok) {
                
            }
            else {
                System.exit(0);
                return true;
            }
            return false;
        }
        
        private void readFile() {
            data = new ArrayList<String>();
            FileInputStream fileIn = null;
            String line;
    
            try {
                fileIn = new FileInputStream("files/demo.txt");
                InputStreamReader inReader = new InputStreamReader(fileIn);
                BufferedReader bufReader = new BufferedReader(inReader);
                
                while((line = bufReader.readLine()) != null) {
                    recordData(line);
                }
                bufReader.close();
            }
            catch(IOException ex) {
                ex.printStackTrace();
            }
        }
        
        private void writeFile() {
            FileWriter fw = null;
            try {
                fw = new FileWriter("files/demo.txt");
                BufferedWriter bufWriter = new BufferedWriter(fw);
                
                for(int i = 0; i < data.size(); i++) {
                    bufWriter.write(data.get(i).toString());
                    bufWriter.newLine();
                }
                bufWriter.close();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
        
        private void recordData(String str) {
            if(data.isEmpty()) {
               data.add(str);
            }
            else if(isDuplicate(str)) {
                data.add(str);
            }
        }
        
        private boolean isDuplicate(String str) {
            String temp;
            for(int i = 0; i < data.size(); i++) {
                temp = data.get(i).toString();
                if(!str.equals(temp)) {
                    isFind = true;
                }
                else {
                    return false;
                }
            }
            return isFind;
        }
        
        private void populateData() {
            for(int i = 0; i < data.size(); i++) {
                list.add(data.get(i));
            }
        }
        
        private void addUserData() {
            list.removeAll();
            populateData();
            String temp = textField.getText();
            if(!temp.isEmpty()) {
                list.add(temp);
                recordData(temp);
            }
            writeFile();
        }
        
        private void removeUserData() {
            String temp = list.getSelectedItem();
            list.removeAll();
            data.remove(temp);
            populateData();
            writeFile();
        }
        
        public static void main(String[] args) {
            Frame f = new Frame();
            new SarinamDemo(f);
        }
    }
    At the same time one advice, next time in such applications don't use AWT. It's out dated. Use Swing for GUI developments.

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

    Default

    Quote Originally Posted by Eranga View Post
    So waiting for me. :confused: I'm really happy that if you have tried something.

    Anyway, I'll give the code. As much as possible I'll try to keep your requirements as much as possible. And also try to keep the code more simple, violating some OO concepts too. Have to change some parameters also, like the read file name(actually the path). You should give 100 mark for this:D

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.ArrayList;
    
    /**
     *
     * @author Eranga Tennakoon
     */
    public class SarinamDemo extends Dialog {
    
        private Button ok, cancel, add, delete;
        private TextField textField;
        private List list;
        private ArrayList<String> data;
        private boolean isFind;
        
        public SarinamDemo(Frame perent) {
            super(perent, "Demo" , true);
            
            readFile();
            
            ok = new Button("Ok");
            cancel = new Button("Cancel");
            add = new Button("<<Add");
            delete = new Button("Delete");
            textField = new TextField(15);
            list = new List(10, true);
            
            setLayout(new GridBagLayout());
            addToContainer(this, list, 0, 0, 1, 3, 1,0);
            addToContainer(this, add, 1, 0, 1, 1, 0, 0);
            addToContainer(this, textField, 2, 0, 2, 1, 0,0);
            addToContainer(this, delete, 1, 1, 1, 1, 0, 0);
            addToContainer(this, cancel, 2, 4, 1, 1, 0, 0);
            addToContainer(this, ok, 3, 4, 1, 1, 0, 0);
            
            populateData();        
            pack();
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            setResizable(true);
            setVisible(true);
    
        }
    
        private void addToContainer(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);
        }
        
        @Override
        public boolean action(Event evt, Object arg) {
            if(evt.target == add) {
                addUserData();
                return true;
            }
            else if(evt.target == delete) {
                removeUserData();
            }
            else if(evt.target == ok) {
                
            }
            else {
                System.exit(0);
                return true;
            }
            return false;
        }
        
        private void readFile() {
            data = new ArrayList<String>();
            FileInputStream fileIn = null;
            String line;
    
            try {
                fileIn = new FileInputStream("files/demo.txt");
                InputStreamReader inReader = new InputStreamReader(fileIn);
                BufferedReader bufReader = new BufferedReader(inReader);
                
                while((line = bufReader.readLine()) != null) {
                    recordData(line);
                }
                bufReader.close();
            }
            catch(IOException ex) {
                ex.printStackTrace();
            }
        }
        
        private void writeFile() {
            FileWriter fw = null;
            try {
                fw = new FileWriter("files/demo.txt");
                BufferedWriter bufWriter = new BufferedWriter(fw);
                
                for(int i = 0; i < data.size(); i++) {
                    bufWriter.write(data.get(i).toString());
                    bufWriter.newLine();
                }
                bufWriter.close();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
        
        private void recordData(String str) {
            if(data.isEmpty()) {
               data.add(str);
            }
            else if(isDuplicate(str)) {
                data.add(str);
            }
        }
        
        private boolean isDuplicate(String str) {
            String temp;
            for(int i = 0; i < data.size(); i++) {
                temp = data.get(i).toString();
                if(!str.equals(temp)) {
                    isFind = true;
                }
                else {
                    return false;
                }
            }
            return isFind;
        }
        
        private void populateData() {
            for(int i = 0; i < data.size(); i++) {
                list.add(data.get(i));
            }
        }
        
        private void addUserData() {
            list.removeAll();
            populateData();
            String temp = textField.getText();
            if(!temp.isEmpty()) {
                list.add(temp);
                recordData(temp);
            }
            writeFile();
        }
        
        private void removeUserData() {
            String temp = list.getSelectedItem();
            list.removeAll();
            data.remove(temp);
            populateData();
            writeFile();
        }
        
        public static void main(String[] args) {
            Frame f = new Frame();
            new SarinamDemo(f);
        }
    }
    At the same time one advice, next time in such applications don't use AWT. It's out dated. Use Swing for GUI developments.
    Sir i am giveing 200%

    it is working..But when i give input in textfiled and click on add button then item is insert in listbox but again i click on add button then also insert in listbox.

    Second thing sir i want to add only integer item..

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

    Default

    So before each click of add button check that is in the list or not. You can deal with integers too.

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

    Default

    Quote Originally Posted by Eranga View Post
    So before each click of add button check that is in the list or not. You can deal with integers too.
    Sir i deal with integer and i am try to do add click problem

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

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

    Default

    Quote Originally Posted by Eranga View Post
    What you are talking about. I'm not clear you.
    Sir now i am abel to enter only integer value.Sir problem is when i click on add button twice then enter two time same value.So i want to remove this..

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

Page 5 of 5 FirstFirst ... 345

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
  •