Results 1 to 4 of 4
  1. #1
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default ItemStateChanged Interface Implementation

    Can I use a nested if/else statement instead of using different Inner classes for an event? What I am trying to do is a program that allows a user to select a shape with a radio button and if they want the shape to be filled or not when it is painted.

    Java Code:
    public void itemStateChanged(ItemEvent event)
    	{
    		if(event.getSource() == rectangleRadioButton)
    		{
    			if ( event.getSource() == fill )
    			{
    				fillPanel.setCurrentShapeFilled( true );
    			}
    			else
    			{
    				fillPanel.setCurrentShapeFilled( false );
    			}
    			shape = CustomPanel.RECTANGLE;
    		}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes. When you click something like check boxes and radio buttons it fires an item event. The listener will listen for item events being fired. You can use the fired item event to query where the event was fired from and do the appropriate action based on where it comes from.

    A lot of people do things similar to this, especially with button clicks.

    For instance, some people will make the class implement the interface and then use the fired event to query where it came from.

  3. #3
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    Does it matter if:

    shape = CustomPanel.RECTANGLE;

    is before or after the 1st if "filled" stament?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by collwill View Post
    Can I use a nested if/else statement instead of using different Inner classes for an event? What I am trying to do is a program that allows a user to select a shape with a radio button and if they want the shape to be filled or not when it is painted.

    Java Code:
    public void itemStateChanged(ItemEvent event)
    	{
    		if(event.getSource() == rectangleRadioButton)
    		{
    			if ( event.getSource() == fill )
    			{
    				fillPanel.setCurrentShapeFilled( true );
    			}
    			else
    			{
    				fillPanel.setCurrentShapeFilled( false );
    			}
    			shape = CustomPanel.RECTANGLE;
    		}
    Your code doesn't make sense as it checks if the source is the rectangleRadioButton and then if true, checks if the source is fill, and I doubt that fill and rectangleRadioButton ever refer to the same object, so that code will always be false.

    I would add an anonymous inner action listener to your button, that way you don't even need to check the source since you know it will always be that button, and in the action listener, check the JRadioButton's isSelected state in an if block.

    For more on how to write listeners that are anonymous inner classes, please check out this tutorial: writing listeners, general rules

Similar Threads

  1. 2 Items returned on itemStateChanged() Test???
    By AcousticBruce in forum New To Java
    Replies: 1
    Last Post: 01-08-2011, 05:10 PM
  2. Interface Implementation
    By Samyx in forum New To Java
    Replies: 3
    Last Post: 12-02-2009, 01:20 PM
  3. Interface and implementation erorr
    By mihael in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2008, 12:06 PM

Posting Permissions

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