Jump to content

instanceof?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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 :)

#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
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?
Posted Image

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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?

#4
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

chili5 said:

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...
Posted Image