Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Help-Action Listener/File Writing/Arraylist

    I am having problems with this method in my java program.
    Code:
    public void actionPerformed(ActionEvent event)
    	{
    		switch(event.getSource())
    		{
    			case add:
    			data.add(name.getText());
    	    	data.add(lname.getText());
    	    	data.add(phonenum.getText());
    	    	b=b+3;
    			break;
    			case next:
    			if (a!=b)
    			{
        			a=a+3;
    	    	}
    			cur = ""+data.get(a);
    	    	name.setText(cur);
    			cur = ""+data.get(a+1);
    	    	lname.setText(cur);
    			cur = ""+data.get(a+2);
    	    	phonenum.setText(cur);
    	    	break;
    	    	case back:
    	    	if(a!=0)
    	    	{
        			a=a-3;
        		}
    			cur = ""+data.get(a);
    	    	name.setText(cur);
    			cur = ""+data.get(a+1);
    	    	lname.setText(cur);
    	    	cur = ""+data.get(a+2);
    			phonenum.setText(cur);
    			break;
    			case edit:
    			data(a,name.getText());
    			FileWriter writer = new FileWriter("foo.txt",false);
    			for(int i=0;i<=b;i++)
    			{
    				cur = data.get(i)+"\n";
    				writer.write(cur);
    			}
    			writer.close();
    		}
    	}
    It keeps coming up with errors on the switch.
    Also i can't figure out how to set data,which is a arraylist, to where at the part a of data to something else.
    The next post is the whole file.

  • #2
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Contacts implements ActionListener {
    	JFrame contactsFrame;
        JPanel contactsPanel;
        JTextField name, lname, phonenum;
        JLabel names, lnames, phonenums;
        JButton next, back, add, edit, save;
        private static ArrayList data;
        String cur;
        private static int a=0, b=0;
        
            public Contacts() {
            //Create and set up the window.
            contactsFrame = new JFrame("Contacts Manager Program");
            contactsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            contactsFrame.setSize(3020, 40);
    
            //Create and set up the panel.
            contactsPanel = new JPanel(new GridLayout(5, 2));
    
            //Add the widgets.
            addWidgets();
    
            //Add the panel to the window.
            contactsFrame.getContentPane().add(contactsPanel, BorderLayout.CENTER);
    
            //Display the window.
            contactsFrame.pack();
            contactsFrame.setVisible(true);
        }
        
        private void addWidgets() {
            //Create widgets.
    		cur = ""+data.get(0);
            name = new JTextField(cur);
    		cur = ""+data.get(1);
            lname = new JTextField(cur);
    		cur = ""+data.get(2);
            phonenum = new JTextField(cur);
            names = new JLabel("Name:");
            lnames = new JLabel("Last Name:");
            phonenums = new JLabel("Phone Number:");
            next = new JButton("Next");
            back = new JButton("Back");
            add = new JButton("Add new Contact");
            edit = new JButton("Save Changes");
    
            //Listen to events from the Convert button.
            next.addActionListener(this);
            back.addActionListener(this);
            add.addActionListener(this);
            edit.addActionListener(this);
    
            //Add the widgets to the container.
            contactsPanel.add(names);
            contactsPanel.add(name);
            contactsPanel.add(lnames);
            contactsPanel.add(lname);
            contactsPanel.add(phonenums);
            contactsPanel.add(phonenum);
            contactsPanel.add(back);
            contactsPanel.add(next);
            contactsPanel.add(add);
            contactsPanel.add(edit);
        }
        
        
    	public void actionPerformed(ActionEvent event)
    	{
    		switch(event.getSource())
    		{
    			case add:
    			data.add(name.getText());
    	    	data.add(lname.getText());
    	    	data.add(phonenum.getText());
    	    	b=b+3;
    			break;
    			case next:
    			if (a!=b)
    			{
        			a=a+3;
    	    	}
    			cur = ""+data.get(a);
    	    	name.setText(cur);
    			cur = ""+data.get(a+1);
    	    	lname.setText(cur);
    			cur = ""+data.get(a+2);
    	    	phonenum.setText(cur);
    	    	break;
    	    	case back:
    	    	if(a!=0)
    	    	{
        			a=a-3;
        		}
    			cur = ""+data.get(a);
    	    	name.setText(cur);
    			cur = ""+data.get(a+1);
    	    	lname.setText(cur);
    	    	cur = ""+data.get(a+2);
    			phonenum.setText(cur);
    			break;
    			case edit:
    			data(a,name.getText());
    			FileWriter writer = new FileWriter("foo.txt",false);
    			for(int i=0;i<=b;i++)
    			{
    				cur = data.get(i)+"\n";
    				writer.write(cur);
    			}
    			writer.close();
    		}
    	}
        	
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            Contacts converter = new Contacts();
        }
    
        public static void main(String[] args)
        throws Exception
        {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            read();
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
        public static void read()
        throws Exception
        {
        	data = new ArrayList();
        	BufferedReader reader= new BufferedReader(new FileReader("foo.txt"));
    		String textline = null;
    		while((textline = reader.readLine()) != null)
    		{
    			data.add(textline);
    			setB(b++);
    	    }
    		reader.close();
        }
        public static void setB(int e)
        {
        	b = e;
        }
    }

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Figure the problem with the arraylist tried something and it worked. Just now with the switch problem. Is it possible to use a switch for see if a button was pushed using action listener?

  • #4
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow fast...decided to use if statments but still one problem
    I made another method to write to the file but now it says "unreported exception java.lang.Exception; must be caught or declared to be thrown" for where it goes to the method but method actionPerformed can not have a throws exception. Help?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •