Results 1 to 20 of 20
Thread: error when adding actionlistener
- 12-12-2011, 09:10 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
error when adding actionlistener
I search over internet and found how to add actionlistener to array button...but it show this error...
Is it will not able to function when it is TreeSelectionListener in the programme?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);
^
below is my code:
It is an assignment for my course...thanks for reading this far.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(); } } }
And please tell me where is it wrong...
if can show how to solve it,i think it is better.
- 12-12-2011, 09:28 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: error when adding actionlistener
In the valueChanged() method of the TreeSelectionListener you sayFileTreeFrame.java:75: addActionListener(java.awt.event.ActionListener) in java.
awt.Button cannot be applied to (<anonymous javax.swing.event.TreeSelectionListe
ner>)
But "this" refers to a TreeSelectionListener instance, not an ActionListener. Perhaps you could say something likeJava Code:buttons[i].addActionListener(this);
The expression FileTreeFrame.this is a way of getting at the enclosing FileTreeFrame instance of your TreeSelectionListener instance.Java Code:buttons[i].addActionListener(FileTreeFrame.this);
-----
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
- 12-12-2011, 09:30 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: error when adding actionlistener
Just to make your code compile, change the offending line to this:
... but still then, I find that code very suspicious.Java Code:buttons[i].addActionListener(FileTreeFrame.this);
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-12-2011, 10:23 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
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...
- 12-12-2011, 01:46 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Re: error when adding actionlistener
Hmm...but now actionPerformed function like no effect...if i change to:
then actionPerformed function need to change anything?Java Code:buttons[i].addActionListener(FileTreeFrame.this);
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 01:52 PM.
- 12-12-2011, 08:35 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 12-13-2011, 07:10 AM #7
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
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(); } } }
- 12-13-2011, 07:54 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: error when adding actionlistener
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.but donno why the word had been cut off half...
Can i use add() for the panel and add in new class extends JPanel?
(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.
- 12-13-2011, 08:10 AM #9
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Re: error when adding actionlistener
but if i take off the doLayout(),there is totally nothing show out...
- 12-13-2011, 08:25 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: error when adding actionlistener
Did you validate() the container as the docs say?
- 12-13-2011, 08:34 AM #11
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Re: error when adding actionlistener
ops,sry i forgot to add in validate().
Thanks a lot!
- 12-13-2011, 08:40 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: error when adding actionlistener
You're welcome.
- 12-13-2011, 10:27 AM #13
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
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...
the other path remain same...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(); } }
even the part in actionPerformed(which not inside for loop) there is nothing also...
headache...
- 12-13-2011, 11:06 PM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
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.even the part in actionPerformed(which not inside for loop) there is nothing also...
- 12-14-2011, 04:49 AM #15
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Re: error when adding actionlistener
Hmm...the first part i think i understand...in constructor is it change to:
then it will be ok?Java Code:buttons = new JButton[n];
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.
- 12-14-2011, 04:58 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: error when adding actionlistener
I would have expected these lines to throw an ArrayIndexOutOfBounds exception before when you had two buttons arrays.Java Code:for (int i = 0; i < n; i++) { if (e.getSource()==buttons[i])
Try putting a System.out.println() statement inside the actionPerformed() method to confirm that it is getting called.I try to comment other command in actionPerformed and only left the setText and add label but there is still no working...
- 12-14-2011, 05:10 AM #17
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Re: error when adding actionlistener
it does call actionPerformed after i click the button,i put in:
but why the label will not show out?Java Code:public void actionPerformed(ActionEvent e) { label.setText("aaaaaaaaaaaaaaaa"); panel1.add(label); panel1.validate(); System.out.println("abc"); }
it got print "abc" in command prompt...
- 12-14-2011, 05:26 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: error when adding actionlistener
I don't know. Perhaps panel2 is not visible or has zero size. Try calling validate() rather than panel2.validate().why the label will not show out?
-----
I'm confused. You code up until now had used panel2 in actionPerformed().
- 12-14-2011, 05:33 AM #19
Member
- Join Date
- Dec 2011
- Posts
- 20
- Rep Power
- 0
Re: error when adding actionlistener
ok...i put the setVisible(true);then it came out.
Thanks a lot...and sorry to make you confused.
- 12-14-2011, 05:44 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
Need help adding an actionListener
By HbJgd in forum New To JavaReplies: 1Last Post: 06-12-2011, 05:33 PM -
Need some help with ActionListener error
By DaBananaboat in forum New To JavaReplies: 2Last Post: 05-04-2011, 01:29 PM -
adding a actionListener but not using inner class
By hariza in forum AWT / SwingReplies: 2Last Post: 10-08-2010, 07:24 AM -
exception error in actionlistener
By aserothbw in forum AWT / SwingReplies: 1Last Post: 08-31-2009, 11:24 PM -
ActionListener Error
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-05-2009, 08:36 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks