0

My question probably has a rudimentary answer. I've read some tutorials but couldn't find a specific answer, and I'm only just starting to learn OOP concepts.

Expected outcome: Add a focusListener to each component in a JPanel, and on that focusGained event, retrieve a String unique to each component (So each text field might have "this is field 1", "this is field 2", etc.)

Problem:

for (Component comp : bookCard.getComponents())
        {
            comp.addFocusListener(new FocusAdapter()
            {
                @Override
                public void focusGained(FocusEvent e)
                {
                    super.focusGained(e);

                    //How do I implement the following line?
                    System.out.println(this.getUniqueString());
                }
            });
        }

My class hierarchy (correct term?) is as follows: BookCard extends MainCard, MainCard extends JPanel. This is fine.

So what kind of hierarchy will I need to solve this particular problem? Something like: MyTextField extends MyComponent, MyComponent extends JComponent - where MyComponent holds the getUniqueString() method?

Edit: To be more specific, I'm making a dialog similar to Microsoft Word's 'add new source' dialog, which would grab an example String from the component to help the user. "Author" field might say "Example: Kramer, James D".

4 Answers4

2

As far as unique name, you can use JComponent.set/getName to set a unique name for each component.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • This is by far the best solution. For the `FocusEvent e` do `e.getComponent().getName()`. The name must be set on beforehand to your liking. – Joop Eggen Jul 15 '15 at 12:13
2

My question probably has a rudimentary answer. I've read some tutorials but couldn't find a specific answer, and I'm only just starting to learn OOP concepts.

hasn't something with OOP concepts, it could be about programing logics, by default in progemming isn't required to get any unique identifier at runtime

So what kind of hierarchy will I need to solve this particular problem? Something like: MyTextField extends MyComponent, MyComponent extends JComponent - where MyComponent holds the getUniqueString() method?

seems to putClientProperty / getClientProperty is proper method for your requirements, it can be multiplied, with any descriptions, for example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Very useful; reminder to readers: the "clientProperty" attribute is introduced at the `JComponent` level in the class hierarchy. –  Feb 21 '21 at 14:38
0

I'd use an interface that all your components have to implement and which provides the getter for the unique string.

In your code you'd then check if a component implements that interface and if so you cast and call the method.

Example:

Interface:

interface UniqueComponent {
   String getUniqueString();
}

Usage:

JComponent component = ...;
if( component instanceof UniqueComponent ) {
  String uniqueString = ((UniqueComponent)component).getUniqueString();
}

One question would remain though: how would you ensure the String is unique? What do you need it for?

In case it's just meant to unique identify a component instance you could either use System.identityHashCode(component) which should return a different hash code for each instance. It's not that readable but should be unique.

Another option might be the use of a UUID, also not quite readable but similar in uniqueness.

For more readability you can also combine the component's class name with the object hashcode, which basically is what the default toString() implementation does.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • I'm making a dialog similar to Microsoft Word's 'add new source' dialog, which would grab an example String from the component to help the user. "Author" field might say "Example: Kramer, James D". Sorry about the ambiguity, I would post a picture of what I'm trying to achieve if my reputation would allow. – TrickyTrev Jul 15 '15 at 12:07
0

If you want some global unique identifier, you might consider a UID...

A UID instance contains three primitive values:

unique, an int that uniquely identifies the VM that this UID was generated in, with respect to its host and at the time represented by the time value (an example implementation of the unique value would be a process identifier), or zero for a well-known UID

time, a long equal to a time (as returned by System.currentTimeMillis()) at which the VM that this UID was generated in was alive, or zero for a well-known UID

count, a short to distinguish UIDs generated in the same VM with the same time value

Constantin
  • 1,506
  • 10
  • 16