Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

question about tag

Hi,

I wonder if in Java Swing, I can use tags??  In C++, I can use tags to go back or go to somewhere I want to.  How can I do this in Java Swing??


Xenia
0
xenia27
Asked:
xenia27
  • 6
  • 4
  • 3
2 Solutions
 
shji1Commented:
As far as I know, there is no built-in mechanism in swing that is similar to the tags in C++ or VB.
You will probably have to build one on your own.
--
0
 
shji1Commented:
You can try and use the setName() and getName() methods from the Component class as your tagging system.
Then, you will need to code some 'findComponentByName(String name)' method that will find it for you.
--
0
 
xenia27Author Commented:
Is there any example I can check out???  Kinda confused how can I do that....
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
zzynxSoftware engineerCommented:
>> In C++, I can use tags to go back or go to somewhere I want to.
I don't know if it exists in Java.
But if you feel the need to "jump around" in your code that way, you have to ask yourself if you're coding correct.
It's the old "go to" discussion...

So, my advice is: keep yourself far from using tags in whatever language you're coding.
It's bad for readability and maintenance and most of the time it indicates the flow of your program could be improved.
0
 
zzynxSoftware engineerCommented:
This article my interest you: http://java.sun.com/developer/TechTips/2000/tt0613.html (2nd part)
0
 
zzynxSoftware engineerCommented:
Somewhere else I read:
"In Java, break, continue, and label: fill the role of goto - as the article mentioned in my previous comment illustrates -, but without many of goto's negatives."

0
 
shji1Commented:
Thuogh I never tried it myself, I can only advice you to follow these steps.
1. When you create a new instance of a component that you want to get access to in the future, name this component using the setName method.
2. When you want to gain the focus or to set the visibility on the component, you will need to search for it from the parent component. (recursively or by loops) using the getName method.

But now... The more I think of it, I believe that you can use a simple Map (HashMap) that can store the names (tags) as 'keys' and the Components themselfs as 'values'. In this case, every 'interesting' component that you create should have a key in the hashmap so you can gain access to it at any time in a very low complexity. Use the HashMap's put() method to place a mapping, and use the get() method to get the reference to the component.
I think this is a better way then the 'search by name' option.
Hope it helps.
shji1
0
 
zzynxSoftware engineerCommented:
Xenia, it seems that shji1 and I are talking about quite different things.
Who is the nearest to what you meant to ask?
0
 
xenia27Author Commented:
Still working the flow of my codes...map seems too complicate....
so I guess I need to figure out a way to design my codes now...@@
0
 
shji1Commented:
Maps are pretty handy if you need a quick mepping to an object, that is why I think it's best here.
They are not as complicated as you think, and actually, the only 2 methods you need to use here are the get and the put.
If you already wrote your application and you want to integrate the maps now, you can use the singleton design pattern in order to register you components into the map from anywhere in your code.
Hope it helps... (-;
0
 
zzynxSoftware engineerCommented:
>> Who is the nearest to what you meant to ask?
Xenia,
are we talking about code flow (cf. my comments)
or
about component referencing? (cf. shji1's comments)
0
 
xenia27Author Commented:
mMmm...I think I will redesign the flow of my codes...but now I understand how to do tags...^^
I meant to know how to do the component referencing...but I guess I should try an easier way before I get myself into a big trouble...@@

Thanks for everything!!!
0
 
zzynxSoftware engineerCommented:
All right. Thanks for accepting.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now