Hi!

I need to know how to make global object from a local place like a function.
And of course, they need to have unique names each time the function is run
Could I somehow pass the a String parameter of the function to the name of the new object?

Thnx :)

Dani AI

Generated

As suggested, you cannot create Java variable names dynamically at runtime. The usual, robust solution is to keep your objects in a global container you can look up by name (for example a Map<String, JButton>) or in a dynamic List if you only need an ordered collection. was right to point you toward Maps; below is a concrete, thread-safe registry pattern you can use, plus a simple List alternative and practical tips.

import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.util.concurrent.atomic.AtomicReference;

public final class ButtonRegistry {
    private static final Map<String,JButton> MAP = new ConcurrentHashMap<>();

    public static JButton create(final String name) {
        final AtomicReference<JButton> ref = new AtomicReference<>();
        try {
            SwingUtilities.invokeAndWait(() -> ref.set(new JButton(name))); // create on EDT
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        MAP.put(name, ref.get());
        return ref.get();
    }

    public static JButton get(String name) { return MAP.get(name); }
    public static JButton remove(String name) { return MAP.remove(name); }
}

If you only need a growable array, use a List rather than copying arrays manually:

List<JButton> buttons = new ArrayList<>();
buttons.add(new JButton("label")); // update on EDT

Practical notes: ensure all Swing creation/modification runs on the Event Dispatch Thread (SwingUtilities.invokeLater/invokeAndWait). Use ConcurrentHashMap or synchronize access if multiple threads touch the registry. Prevent duplicate keys by checking MAP.containsKey(...) and appending a suffix or using a UUID. Avoid holding long-lived static references to many Swing components (memory leak risk) — store models or Actions when possible and remove entries when components are disposed. Finally, was right about the array syntax pitfall you hit; ArrayList is usually simpler for dynamic collections.

Recommended Answers

All 6 Replies

For something like this you would need a global map object that maps names with data, so your function could create the new object and add it with its name to the map which is the real global container of these objects. Then, create a function that allows you to do lookups by passing the name, getting the object back in return. You might want to implement this as a class, with accessor, destructor, and other such functions, so the global object would be an instance of this class.

What's a map?
I've never heard that term before.

Now, I can up with a simpler way of doing what I need, and that is create the objects, then copy them into an array. Then, when I need the new member, I'll copy the old array into the new one- but with one new member.

However, I cannot create a JButton array (my syntax is JButton[] buttons=new JButton()[2]; ) because apparently the syntax used for any other data type is not right for the JButton (the error message is: The type of the expression must be an array type but it resolved to JButton. My thought was that the JButton cannot create arrays, but I see no reason not to...)

Do you have any other way to do what I need (the map idea was very confusing)

The problem with the code you posted is the () after JButton. Remove them and it should compile.

I fixed it before you commented, I solved the problem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.