Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

instanceof?


  • Please log in to reply
3 replies to this topic

#1 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 21 June 2009 - 02:29 AM

Yesterday I was working on creating a JMenuBar where you can click on a menu item and go to that form. However, only if you are not already on that form. At first I had no idea how to do it, then it struck me to use the instanceof operator.

Two of my branchces are shown below. Now I was curious if there is a better way to do this. Each if structure seems to be very similar to the last one. Can anybody help me come up with something that would clean this up a bit?

} else if (e.getActionCommand().equals("add_product")) {
            // if the user is not already on the addProduct form
            // take them there
            if (f instanceof addProduct == false) {
                // only close this form if it is not another instance of this form
                new addProduct().setVisible(true);
                f.dispose();
            }
        } else if (e.getActionCommand().equals("edit_product")) {
            // if the user is not already on the editProduct form
            // take them there
            if (f instanceof editProduct == false) {
                new editProduct().setVisible(true);
                f.dispose();
            }
        }

Thanks :)
  • 0

#2 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 21 June 2009 - 05:39 AM

Looks, clean and simple enough, hence you done it right, I mean to check current position if true, and create a new situation if not true is as you have done...
So I would say you have done it very clean. Since your using a Listener right?
  • 1

#3 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 21 June 2009 - 05:43 AM

Yea, I'm using a listener, though is it possible to create a generic method that does this:

// if the user is not already on the editProduct form
            // take them there
            if (f instanceof editProduct == false) {
                new editProduct().setVisible(true);
                f.dispose();
            }

So I wouldn't have to do so much copy and pasting?
  • 0

#4 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 21 June 2009 - 06:22 AM

Yea, I'm using a listener, though is it possible to create a generic method that does this:

// if the user is not already on the editProduct form
            // take them there
            if (f instanceof editProduct == false) {
                new editProduct().setVisible(true);
                f.dispose();
            }

So I wouldn't have to do so much copy and pasting?


Yes it's possible !

Heck, I need to check it out myself, never used instanceof !
Have you a small demo of what your after to do or so, I feel really insecure with answering...
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download