NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

TreeSelectionListener has a syntax error

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
aschwarz1309



Joined: 23 Oct 2011
Posts: 20

PostPosted: Thu Mar 02, 2017 10:12 pm    Post subject: TreeSelectionListener has a syntax error Reply with quote

If this is not the right place for a question like this, please tell me to get off your turf (and if possible) where i can get answers.

I'm working through the code fragment below. The error message is: TestTree is not abstract and does not override abstract method valueChanged(TreeSelectionEvent) in TreeSelectionListener

I've looked at the Java API and it looks as if I have the correct listener and the correct overridden function. I'm stumped. So, what am I missing?

FYI: this is an implementation of an example in Java Swing v1.4, ch17.
----

Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame implements TreeSelectionListener {
   TreeSelectionListener listener = new TreeSelectionListener( ) {
      final JLabel messageLabel = new JLabel("Nothing selected.");
      public void valueChanged(TreeSelectionEvent tse) {
         TreePath tp = tse.getNewLeadSelectionPath( );
         messageLabel.setText("Selected: " + tp.getLastPathComponent( ));
      }
    };
}
Back to top
Sean Carrick
Posted via mailing list.





PostPosted: Fri Mar 03, 2017 12:27 pm    Post subject: TreeSelectionListener has a syntax error Reply with quote

You need to put the @Override annotation above your method.

Quote:
Quote:
import java.awt.*;
import java.awt.event.*;import javax.swing.event.*;
Quote:
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame implements TreeSelectionListener {
TreeSelectionListener listener = new TreeSelectionListener( ) {
final JLabel messageLabel = new JLabel("Nothing selected.");
@Override
Quote:
Quote:
public void valueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getNewLeadSelectionPath( );
messageLabel.setText("Selected: " + tp.getLastPathComponent( ));
}
};
}


--
[img]https://s3.amazonaws.com/ucwebapp.wisestamp.com/13aab24c-4877-4ae8-97d8-e5b95d5767a8/IntegritySolutionsSplash.format_png.resize_200x.png#logo[/img]
Sean Carrick
VP Software Development, Integrity Solutions
address-removed ([email]address-removed[/email]) [url=http://facebook.com/PekinSOFT][/url]



On Thu, 2017-03-02 at 22:12 +0000, aschwarz1309 wrote:
Quote:
Quote:
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame implements TreeSelectionListener {
TreeSelectionListener listener = new TreeSelectionListener( ) {
Back to top
markwade



Joined: 04 Dec 2010
Posts: 342

PostPosted: Fri Mar 03, 2017 1:32 pm    Post subject: TreeSelectionListener has a syntax error Reply with quote

Remove the "implements TreeSelectionListener" from your TestTree class
declaration. TestTree isn't implementing TreeSelectionListener, you've
declared the listener as an anonymous inner class. Either way works,
just not both at the same time.


On 03/02/2017 05:12 PM, aschwarz1309 wrote:
Quote:
If this is not the right place for a question like this, please tell me to get off your turf (and if possible) where i can get answers.

I'm working through the code fragment below. The error message is: TestTree is not abstract and does not override abstract method valueChanged(TreeSelectionEvent) in TreeSelectionListener

I've looked at the Java API and it looks as if I have the correct listener and the correct overridden function. I'm stumped. So, what am I missing?

FYI: this is an implementation of an example in Java Swing v1.4, ch17.
----


Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame implements TreeSelectionListener {
TreeSelectionListener listener = new TreeSelectionListener( ) {
final JLabel messageLabel = new JLabel("Nothing selected.");
public void valueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getNewLeadSelectionPath( );
messageLabel.setText("Selected: " + tp.getLastPathComponent( ));
}
};
}







Back to top
markwade



Joined: 04 Dec 2010
Posts: 342

PostPosted: Fri Mar 03, 2017 2:05 pm    Post subject: TreeSelectionListener has a syntax error Reply with quote

Quote:
you've declared the listener as an anonymous inner class

Actually, let me take that back. It's an innerclass but it isn't
anonymous because it's named "listener". I'm fuzzy on this stuff. Either
way, it builds okay if you remove the "implements TreeSelectionListener".


On 03/03/2017 08:30 AM, Mark Wade wrote:
Quote:
Remove the "implements TreeSelectionListener" from your TestTree class
declaration. TestTree isn't implementing TreeSelectionListener, you've
declared the listener as an anonymous inner class. Either way works,
just not both at the same time.


On 03/02/2017 05:12 PM, aschwarz1309 wrote:
Quote:
If this is not the right place for a question like this, please tell
me to get off your turf (and if possible) where i can get answers.

I'm working through the code fragment below. The error message is:
TestTree is not abstract and does not override abstract method
valueChanged(TreeSelectionEvent) in TreeSelectionListener

I've looked at the Java API and it looks as if I have the correct
listener and the correct overridden function. I'm stumped. So, what
am I missing?

FYI: this is an implementation of an example in Java Swing v1.4, ch17.
----


Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame implements TreeSelectionListener {
TreeSelectionListener listener = new TreeSelectionListener( ) {
final JLabel messageLabel = new JLabel("Nothing selected.");
public void valueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getNewLeadSelectionPath( );
messageLabel.setText("Selected: " +
tp.getLastPathComponent( ));
}
};
}









Back to top
markwade



Joined: 04 Dec 2010
Posts: 342

PostPosted: Fri Mar 03, 2017 6:06 pm    Post subject: TreeSelectionListener has a syntax error Reply with quote

One last thing because I've been thinking about it and to put this on
topic which is using NetBeans, all you needed to do was click on the
icon on the left of the light bulb with a red ball and exclamation point
and select "Implement all abstract methods" and NetBeans would have
generated a valueChanged method for you. When I said not both at the
same time that was not correct.

Oh, and I guess what that is is a named instance of an anonymous inner
class.


On 03/03/2017 09:04 AM, Mark Wade wrote:
Quote:
Quote:
you've declared the listener as an anonymous inner class

Actually, let me take that back. It's an innerclass but it isn't
anonymous because it's named "listener". I'm fuzzy on this stuff.
Either way, it builds okay if you remove the "implements
TreeSelectionListener".


On 03/03/2017 08:30 AM, Mark Wade wrote:
Quote:
Remove the "implements TreeSelectionListener" from your TestTree
class declaration. TestTree isn't implementing TreeSelectionListener,
you've declared the listener as an anonymous inner class. Either way
works, just not both at the same time.


On 03/02/2017 05:12 PM, aschwarz1309 wrote:
Quote:
If this is not the right place for a question like this, please tell
me to get off your turf (and if possible) where i can get answers.

I'm working through the code fragment below. The error message is:
TestTree is not abstract and does not override abstract method
valueChanged(TreeSelectionEvent) in TreeSelectionListener

I've looked at the Java API and it looks as if I have the correct
listener and the correct overridden function. I'm stumped. So, what
am I missing?

FYI: this is an implementation of an example in Java Swing v1.4, ch17.
----


Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame implements TreeSelectionListener {
TreeSelectionListener listener = new TreeSelectionListener( ) {
final JLabel messageLabel = new JLabel("Nothing selected.");
public void valueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getNewLeadSelectionPath( );
messageLabel.setText("Selected: " +
tp.getLastPathComponent( ));
}
};
}











Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo