Results 1 to 20 of 20
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By JosAH

Thread: error when adding actionlistener

  1. #1
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default error when adding actionlistener

    I search over internet and found how to add actionlistener to array button...but it show this error...

    FileTreeFrame.java:75: addActionListener(java.awt.event.ActionListener) in java.
    awt.Button cannot be applied to (<anonymous javax.swing.event.TreeSelectionListe
    ner>)
    buttons[i].addActionListener(this);
    ^
    Is it will not able to function when it is TreeSelectionListener in the programme?

    below is my code:
    Java Code:
    import java.io.File;
    import java.util.Iterator;
    import java.util.Vector;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    import javax.swing.JTree;
    import javax.swing.event.TreeModelEvent;
    import javax.swing.event.TreeModelListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;
    
    public class FileTreeFrame extends JFrame implements ActionListener{
    	private JTree fileTree;
    
    	private FileSystemModel fileSystemModel;
    	
    	String path = "";
    	String path2 = "";
    	JLabel label = new JLabel(new ImageIcon(path));
    	private JTextArea fileDetailsTextArea = new JTextArea();
    	private Panel Panel1 = new Panel();
    	private Panel Panel2 = new Panel();
    
    	public FileTreeFrame(String directory) 
    	{
    		super("JTree FileSystem Viewer");
    		fileSystemModel = new FileSystemModel(new File(directory));
    		fileTree = new JTree(fileSystemModel);
    		fileTree.setEditable(true);
    		label.setHorizontalAlignment(JLabel.CENTER);
    		fileTree.addTreeSelectionListener(new TreeSelectionListener() 
    		{
    			public void valueChanged(TreeSelectionEvent event) 
    			{
    				
    				File file = (File) fileTree.getLastSelectedPathComponent();	
    				Panel1.removeAll();
    				Panel2.removeAll();
    				Panel1.setLayout(new FlowLayout());
    				Panel2.setLayout(new FlowLayout());
    				String files;
    				File folder = new File(file.getPath());
    				File[] listOfFiles = folder.listFiles(); 
    				Button[] buttons = new Button[listOfFiles.length];
    				path = getFileDetails(file)+"\\001.jpg";
    				path2 = path.replace('\\', '/');
    				label = new JLabel(new ImageIcon(path2));
    				Panel2.add(label);
    				fileDetailsTextArea.setText(path2);
    				Panel1.add(fileDetailsTextArea);
    				for (int i = 0; i < listOfFiles.length; i++) 
    				{
    					if (listOfFiles[i].isFile()) 
    					{
    						files = listOfFiles[i].getName();
    						if (files.endsWith(".avi") || files.endsWith(".AVI") || files.endsWith(".swf") || files.endsWith(".SWF") || 
    						files.endsWith(".mp3") || files.endsWith(".MP3") || files.endsWith(".MP4") || files.endsWith(".mp4") || 
    						files.endsWith(".mpeg") || files.endsWith(".MPEG") || files.endsWith(".mpg") || files.endsWith(".MPG") ||
    						files.endsWith(".tiff") || files.endsWith(".TIFF") || files.endsWith(".jpg") || files.endsWith(".JPG") || 
    						files.endsWith(".png") || files.endsWith(".PNG") || files.endsWith(".jpeg") || files.endsWith(".JPEG") || 
    						files.endsWith(".bmp") || files.endsWith(".BMP") || files.endsWith(".gif") || files.endsWith(".GIF"))
    						{
    							buttons[i] = new Button(files);
    							Panel1.add(buttons[i]);
    							buttons[i].addActionListener(this);
    							
    						}
    					}
    				}
    				Panel1.doLayout();
    				Panel2.doLayout();
    			}
    		});
    		JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(
    			fileTree), splitPane2);
    		splitPane2.setTopComponent(Panel1);
    		splitPane2.setBottomComponent(new JScrollPane(Panel2));
    		splitPane.setDividerLocation(300);
    		splitPane2.setDividerLocation(300);
    		getContentPane().add(splitPane);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(1200, 800);
    		setVisible(true);
    	}
      
    	private String getFileDetails(File file) 
    	{
    		if (file == null)
    			return "";
    		StringBuffer buffer = new StringBuffer();
    		buffer.append(file.getPath());
    		return buffer.toString();
    	}
    	public void actionPerformed(ActionEvent e) 
    	{ 
            //if (e.getSource()==Buttons)
    		//{
    			
    		//}
    	}
    
      public static void main(String args[]) {
        new FileTreeFrame("c:\\");
      }
    }
    
    class FileSystemModel implements TreeModel {
      private File root;
    
      private Vector listeners = new Vector();
    
      public FileSystemModel(File rootDirectory) {
        root = rootDirectory;
      }
    
      public Object getRoot() {
        return root;
      }
    
      public Object getChild(Object parent, int index) {
        File directory = (File) parent;
        String[] children = directory.list();
        return new TreeFile(directory, children[index]);
      }
    
      public int getChildCount(Object parent) {
        File file = (File) parent;
        if (file.isDirectory()) {
          String[] fileList = file.list();
          if (fileList != null)
            return file.list().length;
        }
        return 0;
      }
    
      public boolean isLeaf(Object node) {
        File file = (File) node;
        return file.isFile();
      }
    
      public int getIndexOfChild(Object parent, Object child) {
        File directory = (File) parent;
        File file = (File) child;
        String[] children = directory.list();
        for (int i = 0; i < children.length; i++) {
          if (file.getName().equals(children[i])) {
            return i;
          }
        }
        return -1;
    
      }
    
      public void valueForPathChanged(TreePath path, Object value) {
        File oldFile = (File) path.getLastPathComponent();
        String fileParentPath = oldFile.getParent();
        String newFileName = (String) value;
        File targetFile = new File(fileParentPath, newFileName);
        oldFile.renameTo(targetFile);
        File parent = new File(fileParentPath);
        int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
        Object[] changedChildren = { targetFile };
        fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);
    
      }
    
      private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
        TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
        Iterator iterator = listeners.iterator();
        TreeModelListener listener = null;
        while (iterator.hasNext()) {
          listener = (TreeModelListener) iterator.next();
          listener.treeNodesChanged(event);
        }
      }
    
      public void addTreeModelListener(TreeModelListener listener) {
        listeners.add(listener);
      }
    
      public void removeTreeModelListener(TreeModelListener listener) {
        listeners.remove(listener);
      }
    
      private class TreeFile extends File {
        public TreeFile(File parent, String child) {
          super(parent, child);
        }
    
        public String toString() {
          return getName();
        }
      }
    }
    It is an assignment for my course...thanks for reading this far.
    And please tell me where is it wrong...
    if can show how to solve it,i think it is better.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    FileTreeFrame.java:75: addActionListener(java.awt.event.ActionListener) in java.
    awt.Button cannot be applied to (<anonymous javax.swing.event.TreeSelectionListe
    ner>)
    In the valueChanged() method of the TreeSelectionListener you say

    Java Code:
    buttons[i].addActionListener(this);
    But "this" refers to a TreeSelectionListener instance, not an ActionListener. Perhaps you could say something like

    Java Code:
    buttons[i].addActionListener(FileTreeFrame.this);
    The expression FileTreeFrame.this is a way of getting at the enclosing FileTreeFrame instance of your TreeSelectionListener instance.

    -----

    But I think it would be much better not to use an anonymous inner class like this, rather declare an instance variable of type TreeSelectionListener. Basically your constructor is too long and too deeply nested.

    A few other things strike the eye:

    * You are mixing java.awt.Button and javax.swing.JButton which is not good
    * You are importing java.lang.* which is unnecessary
    * You have variables starting with an upper case letter
    nananya likes this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: error when adding actionlistener

    Just to make your code compile, change the offending line to this:

    Java Code:
    buttons[i].addActionListener(FileTreeFrame.this);
    ... but still then, I find that code very suspicious.

    kind regards,

    Jos
    nananya likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    Thanks both very much,it is working now!
    Beside that this is just the code that i use to modify and trying the function, i will write it again to delete more useless thing.
    File tree function is not written by me but just a sample code on the internet,so it is a bit complicated...

  5. #5
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    Hmm...but now actionPerformed function like no effect...if i change to:
    Java Code:
    buttons[i].addActionListener(FileTreeFrame.this);
    then actionPerformed function need to change anything?
    to make it work...
    now my code is like this:
    Java Code:
    public void actionPerformed(ActionEvent e) 
    { 
            for (int i = 0; i < n ; i++) 
    		{
    			if (e.getSource()==buttons[i])
    			{
    				Panel2.removeAll();
    				path = getFileDetails(file)+"\\"+buttons[i].getText();
    				path2 = path.replace('\\', '/');
    				label= new JLabel(new IamgeIcon(path2));
    				//JTextArea ta = new JTextArea();
    				//ta.setText();
    				Panel2.add(label);
    				Panel2.doLayout();
    			}
    		}
    	}
    Last edited by nananya; 12-12-2011 at 02:52 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    Does this code even compile? I ask because the only buttons[] array I see declared is a nonfinal local variable in the valueChanged() method of the other class.

  7. #7
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    Hmm..sorry now i change it to call another class when it is in valueChanged().
    Now my coding is like this...but donno why the word had been cut off half...
    Can i use add() for the panel and add in new class extends JPanel?
    Here is my code now:
    Java Code:
    import java.io.File;
    import java.util.Iterator;
    import java.util.Vector;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    import javax.swing.JTree;
    import javax.swing.event.TreeModelEvent;
    import javax.swing.event.TreeModelListener;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.TreeModel;
    import javax.swing.tree.TreePath;
     
    public class FileTreeFrame extends JFrame{
        private JTree fileTree;
     
        private FileSystemModel fileSystemModel;
    	//private Frame2 frame2;
        private JPanel Panel0 = new JPanel();
     
        public FileTreeFrame(String directory)
        { 
            super("JTree FileSystem Viewer");
            fileSystemModel = new FileSystemModel(new File(directory));
            fileTree = new JTree(fileSystemModel);
            fileTree.setEditable(true);
            fileTree.addTreeSelectionListener(new TreeSelectionListener()
            {
                public void valueChanged(TreeSelectionEvent event)
                {
                     
                    File file = (File) fileTree.getLastSelectedPathComponent();
    				//JTextArea ta = new JTextArea();
    				//ta.setText("aa");
    				Panel0.removeAll();
    				Panel0.add(new Frame2(file));
                    
    				Panel0.doLayout();
                }
            });
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    		splitPane.setDividerLocation(300);
            splitPane.setLeftComponent(new JScrollPane(fileTree));
    		splitPane.setRightComponent(Panel0);
            getContentPane().add(splitPane);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(1200, 800);
            setVisible(true);
        }
       
        private String getFileDetails(File file)
        {
            if (file == null)
                return "";
            StringBuffer buffer = new StringBuffer();
            buffer.append(file.getPath());
            return buffer.toString();
        }
     
      public static void main(String args[]) {
        new FileTreeFrame("c:\\");
      }
    }
    
    class Frame2 extends JPanel implements ActionListener
    {
    	private Panel Panel1 = new Panel();
        private Panel Panel2 = new Panel();
    	int n;
        String path = "";
        String path2 = "";
        JLabel label = new JLabel(new ImageIcon(path));
    	JButton[] buttons = new JButton[1];
    	
    	public Frame2(File file)
    	{
    				JLabel ta = new JLabel();
    				ta.setText("abcdefgadadsdadsad");
    				add(ta);
    				doLayout();
    				/*Panel1.removeAll();
                    Panel2.removeAll();
                    Panel1.setLayout(new FlowLayout());
                    Panel2.setLayout(new FlowLayout());
                    File folder = new File(file.getPath());
                    File[] listOfFiles = folder.listFiles();
    				n = listOfFiles.length;
    				String files;
                    JButton[] buttons = new JButton[listOfFiles.length];
                    path = folder.toString() +"\\001.jpg";
                    path2 = path.replace('\\', '/');
                    label = new JLabel(new ImageIcon(path2));
    				label.setHorizontalAlignment(JLabel.CENTER);
                    Panel2.add(label);
                    for (int i = 0; i < n; i++)
                    {
                        if (listOfFiles[i].isFile())
                        {
                            files = listOfFiles[i].getName();
                            if (files.endsWith(".avi") || files.endsWith(".AVI") || files.endsWith(".swf") || files.endsWith(".SWF") ||
                            files.endsWith(".mp3") || files.endsWith(".MP3") || files.endsWith(".MP4") || files.endsWith(".mp4") ||
                            files.endsWith(".mpeg") || files.endsWith(".MPEG") || files.endsWith(".mpg") || files.endsWith(".MPG") ||
                            files.endsWith(".tiff") || files.endsWith(".TIFF") || files.endsWith(".jpg") || files.endsWith(".JPG") ||
                            files.endsWith(".png") || files.endsWith(".PNG") || files.endsWith(".jpeg") || files.endsWith(".JPEG") ||
                            files.endsWith(".bmp") || files.endsWith(".BMP") || files.endsWith(".gif") || files.endsWith(".GIF"))
                            {
                                buttons[i] = new JButton(files);
                                Panel1.add(buttons[i]);
                                buttons[i].addActionListener(this);
                                 
                            }
                        }
                    }
                    Panel1.doLayout();
                    Panel2.doLayout();
    				add(Panel1);
    				add(Panel2);
    				//JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    				//splitPane2.setTopComponent(Panel1);
    				//splitPane2.setBottomComponent(new JScrollPane(Panel2));
    				//splitPane2.setDividerLocation(300);
    				//add(splitPane2);
    				doLayout();*/
    	}
    	
    	public void actionPerformed(ActionEvent e) 
    	{ 
            for (int i = 0; i < n; i++) 
    		{
    			if (e.getSource()==buttons[i])
    			{
    				/*Panel2.removeAll();
    				//String path = getFileDetails(file)+"\\"+buttons[i].getText();
    				//String path2 = path.replace('\\', '/');
    				label= new JLabel(new ImageIcon(path2));
    				Panel2.add(label);
    				Panel2.doLayout();*/
    			}
    		}
    	}
    }
     
    class FileSystemModel implements TreeModel {
      private File root;
     
      private Vector listeners = new Vector();
     
      public FileSystemModel(File rootDirectory) {
        root = rootDirectory;
      }
     
      public Object getRoot() {
        return root;
      }
     
      public Object getChild(Object parent, int index) {
        File directory = (File) parent;
        String[] children = directory.list();
        return new TreeFile(directory, children[index]);
      }
     
      public int getChildCount(Object parent) {
        File file = (File) parent;
        if (file.isDirectory()) {
          String[] fileList = file.list();
          if (fileList != null)
            return file.list().length;
        }
        return 0;
      }
     
      public boolean isLeaf(Object node) {
        File file = (File) node;
        return file.isFile();
      }
     
      public int getIndexOfChild(Object parent, Object child) {
        File directory = (File) parent;
        File file = (File) child;
        String[] children = directory.list();
        for (int i = 0; i < children.length; i++) {
          if (file.getName().equals(children[i])) {
            return i;
          }
        }
        return -1;
     
      }
     
      public void valueForPathChanged(TreePath path, Object value) {
        File oldFile = (File) path.getLastPathComponent();
        String fileParentPath = oldFile.getParent();
        String newFileName = (String) value;
        File targetFile = new File(fileParentPath, newFileName);
        oldFile.renameTo(targetFile);
        File parent = new File(fileParentPath);
        int[] changedChildrenIndices = { getIndexOfChild(parent, targetFile) };
        Object[] changedChildren = { targetFile };
        fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);
     
      }
     
      private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
        TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
        Iterator iterator = listeners.iterator();
        TreeModelListener listener = null;
        while (iterator.hasNext()) {
          listener = (TreeModelListener) iterator.next();
          listener.treeNodesChanged(event);
        }
      }
     
      public void addTreeModelListener(TreeModelListener listener) {
        listeners.add(listener);
      }
     
      public void removeTreeModelListener(TreeModelListener listener) {
        listeners.remove(listener);
      }
     
      private class TreeFile extends File {
        public TreeFile(File parent, String child) {
          super(parent, child);
        }
     
        public String toString() {
          return getName();
        }
      }
    }

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    but donno why the word had been cut off half...
    Can i use add() for the panel and add in new class extends JPanel?
    In general you can add a panel to another panel, yes. In this case the only thing that Panel0 contains is the panel that is added which seems slightly odd.

    (and it should be named panel0 in line with Java coding conventions. Also if you remove the commented code there's a greater chance people will read it.)

    You should not be calling doLayout(). See doLayout() API docs.

  9. #9
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    but if i take off the doLayout(),there is totally nothing show out...

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    Did you validate() the container as the docs say?

  11. #11
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    ops,sry i forgot to add in validate().
    Thanks a lot!

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    You're welcome.

  13. #13
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    Sorry i got problem again...
    I had add the button successfully,but there is no reaction when i click on the button?
    here is my code for the actionlistener path...
    Java Code:
    class Frame2 extends JPanel implements ActionListener
    {
    	private JPanel panel1 = new JPanel();
        private JPanel panel2 = new JPanel();
    	private JPanel panel3 = new JPanel(new BorderLayout());
    	File folder = new File("c:\\");
    	int n;
        String path = "";
        String path2 = "";
        JLabel label = new JLabel(new ImageIcon(path));
    	JButton[] buttons = new JButton[1];
    	
    	public Frame2(File file)
    	{
    		panel1.removeAll();
                    panel2.removeAll();
                    folder = new File(file.getPath());
                    File[] listOfFiles = folder.listFiles();
    		n = listOfFiles.length;
    		String files;
                    JButton[] buttons = new JButton[listOfFiles.length];
                    for (int i = 0; i < n; i++)
                    {
                        if (listOfFiles[i].isFile())
                        {
                            files = listOfFiles[i].getName();
                            if (files.endsWith(".avi") || files.endsWith(".AVI") || files.endsWith(".swf") || files.endsWith(".SWF") ||
                            files.endsWith(".mp3") || files.endsWith(".MP3") || files.endsWith(".MP4") || files.endsWith(".mp4") ||
                            files.endsWith(".mpeg") || files.endsWith(".MPEG") || files.endsWith(".mpg") || files.endsWith(".MPG") ||
                            files.endsWith(".tiff") || files.endsWith(".TIFF") || files.endsWith(".jpg") || files.endsWith(".JPG") ||
                            files.endsWith(".png") || files.endsWith(".PNG") || files.endsWith(".jpeg") || files.endsWith(".JPEG") ||
                            files.endsWith(".bmp") || files.endsWith(".BMP") || files.endsWith(".gif") || files.endsWith(".GIF"))
                            {
                                buttons[i] = new JButton(files);
                                panel1.add(buttons[i]);
                                buttons[i].addActionListener(Frame2.this);
                                 
                            }
                        }
                    }
    		add(panel1);
    		add(panel2);
    	}
    	
    	public void actionPerformed(ActionEvent e) 
    	{
    		panel2.removeAll();
            for (int i = 0; i < n; i++) 
    		{
    			if (e.getSource()==buttons[i])
    			{
    				path = folder.toString() + "\\" + buttons[i].getText();
    			}
    		}
            label.setText("aaaaaaaaaaaaaaaa");
            panel2.add(label);
    		panel2.validate();
    	}
    }
    the other path remain same...
    even the part in actionPerformed(which not inside for loop) there is nothing also...
    headache...

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    One problem is that you declare buttons twice: first as an instance variable, then you declare another buttons in the constructor. Remove the declaration in the constructor.

    even the part in actionPerformed(which not inside for loop) there is nothing also...
    Don't forget to check the console for exceptions. If something in the loop threw an exception then setting the text and adding the label will never happen.

  15. #15
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    Hmm...the first part i think i understand...in constructor is it change to:
    Java Code:
    buttons = new JButton[n];
    then it will be ok?

    but i really didn't understand second part.
    What is the exceptions? can give some more hints?
    I try to comment other command in actionPerformed and only left the setText and add label but there is still no working...
    Sorry i am so new to java...
    Thanks for your help.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    Java Code:
    for (int i = 0; i < n; i++) 
            {
                if (e.getSource()==buttons[i])
    I would have expected these lines to throw an ArrayIndexOutOfBounds exception before when you had two buttons arrays.

    I try to comment other command in actionPerformed and only left the setText and add label but there is still no working...
    Try putting a System.out.println() statement inside the actionPerformed() method to confirm that it is getting called.

  17. #17
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    it does call actionPerformed after i click the button,i put in:
    Java Code:
    public void actionPerformed(ActionEvent e) 
    	{
    		label.setText("aaaaaaaaaaaaaaaa");
    		panel1.add(label);
    		panel1.validate();
    		System.out.println("abc");		
    	}
    but why the label will not show out?
    it got print "abc" in command prompt...

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: error when adding actionlistener

    why the label will not show out?
    I don't know. Perhaps panel2 is not visible or has zero size. Try calling validate() rather than panel2.validate().

    -----

    I'm confused. You code up until now had used panel2 in actionPerformed().

  19. #19
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: error when adding actionlistener

    ok...i put the setVisible(true);then it came out.
    Thanks a lot...and sorry to make you confused.

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Need help adding an actionListener
    By HbJgd in forum New To Java
    Replies: 1
    Last Post: 06-12-2011, 06:33 PM
  2. Need some help with ActionListener error
    By DaBananaboat in forum New To Java
    Replies: 2
    Last Post: 05-04-2011, 02:29 PM
  3. adding a actionListener but not using inner class
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 10-08-2010, 08:24 AM
  4. exception error in actionlistener
    By aserothbw in forum AWT / Swing
    Replies: 1
    Last Post: 09-01-2009, 12:24 AM
  5. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 09:36 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
  •