0

I've started to work with Java Swing and I'm asking myself if it is possible to get elements on a generic JPanel, by Id or simply by name. I mean something like 'getElementById("example")', used in javascript.

This is a little example, in pseudo-code of what I'd like to get:

....

myButton = my_panel.getElementById("xxxxxxx");

// OR BY VARIABLE NAME

myButton = my_panel.getElementByVariableName("xxxxxxx");

....

Is that possible in java?

starscream
  • 741
  • 2
  • 11
  • 23
  • Since you say you're a beginner, I think we should be asking _why_ you need to do this? Maybe there is a different answer for a different question that should be asked. It's hard to tell what you're trying to accomplish. – Paul Samsotha Mar 17 '14 at 11:20
  • I have two classes: the first one is used to show the frame on the monitor and the second one works on the background to collect some data. After data collection I'd like the second class to insert them into a list which is on a JPanel. To get this goal I have to find the element on the panel and then add the data collected. – starscream Mar 17 '14 at 11:53
  • Why not just create an instance of the data class in the class with the list, and populate it? If the list needs to be updated again with new data created by that class, just update the list. I may not be following the actual problem you're facing. Maybe showing some of your relevant code would help. – Paul Samsotha Mar 17 '14 at 11:59

3 Answers3

3

You can use putClientProperty and getClientProperty as shown in below code:

    JPanel jPanel = new JPanel();

    JLabel label=new JLabel("Hi");
    jPanel.add(label);
    jPanel.putClientProperty("hiLabel", label);

    System.out.println(jPanel.getClientProperty("hiLabel").getClass().getName());
Braj
  • 46,415
  • 5
  • 60
  • 76
1

Swing core has no concept of an Id for components. You can use setName() to set an unique name and then traverse the component tree of the container recursively to get the component with a certain name (Get a Swing component by name).

Community
  • 1
  • 1
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
1

While Swing components can have a name (http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getName()) this is not an id; it is up to you to ensure consistency.

This name property is not widely used. In SwingUtilities there is no method for finding a child by name, just an ancestor: SwingUtilities#getAncestorNamed.

You would have to to this by yourself. In general it is not impossible. You would start with a Container, iterate over its children (Component[] getComponents) and check for the name. Do this recursively for every child that is a Container itself. Break when found.

But this is untypical for Swing. The framework is not used declarative. (Other Java GUI toolkits are or can, like GWT or Android's native UI.) You would just store a reference to the component you create programmatically for later use.

Of course you can't search by variable name in Java. The name doesn't exist at runtime. Java works different than JavaScript. Most of the time it is not a good idea to directly transfer concepts from one system to another. You need to get used to each one to utilise it fully.

Hauke Ingmar Schmidt
  • 11,559
  • 1
  • 42
  • 50