Java Article 16 - Naming Components with setName()

When you are developing a GUI it is sometimes convenient to name the various buttons and textfields (components) so you can include the component's name in things like log files (a record of a system's or user's actions) or error messages.

Java provides an easy way to name and retrieve the name of components with setName() and getName().

The syntax is as follows:

JTextPane textPane = new JTextPane(); //instantiate an object
textPane.setName("textPane"); //give it a name.
System.out.print(textPane.getName()); //get the name

Examples of these statements are included in the program below so you can see their output. The term "name" can be a bit confusing. Above, textPane is sometime referred to as the object name but, more accurately, it is the object identifier. There is no way to get a component object to return its identifier, so the workaround is to use setName() and getName();

You can also get an object's class name with:

System.out.println(textPane.getClass().getName());

You can also access a lot of information about an object (any object including your own) just by using the return from its identifier:

System.out.println(textPane);

If you scroll all the way to the end of the above you will see an @ followed by some characters such as:

@b02e7a

This is a string representation of the memory address. It is useful for troubleshooting or experimenting with memory allocation, including weather something is being overwritten at a particular address.

The following is a quick swing app showing all of the above in action:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ObjectNames extends JFrame implements ActionListener
{
	int numberFromButton;
	String outputText= "";
	String button1Name; //This is the initial button name.
	 // We are just initializing it here for the sake of appearances.
	 
	String previousButton1Name = "button1";
	JPanel panel = new JPanel();
	JButton button1 = new JButton("button1");
	JTextArea labelOne = new JTextArea("Enter a new name for the button" +
			"\n in the textField then click rename");
	JTextField textField1 =  new JTextField(7);
	JButton renameButton = new JButton("Rename");
	JTextPane textPane = new JTextPane();
	
	//Constructor
	public ObjectNames()
	{
		
		 // If you would like to earn more about these swing 
		 // components we covered them in previous Java articles.
		 
		panel.add(button1);
		panel.add(textField1);
		panel.add(labelOne);
		panel.add(renameButton);
		panel.add(textPane);
		
		JScrollPane scrollPane = new JScrollPane(textPane);
		scrollPane.setVerticalScrollBarPolicy
		(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(200, 200));
		panel.add(scrollPane);
		
		add(panel);
		
		button1.setActionCommand("1");
		button1.addActionListener(this);
		
		renameButton.setActionCommand("rename");
		renameButton.addActionListener(this);
		
		
		 // Below are quick naming examples for a component, textPane1
		 // textPane1 is not actually used for anything in this app.
		 
		JTextPane textPane1 = new JTextPane();//Create an object
		textPane1.setName("Text pane1");//name it
		System.out.println(textPane1.getName());//get the name
		System.out.println("The return from the identifier: " 
				+ textPane1);
		//get the component's class name:
		System.out.println(textPane1.getClass().getName());
	}

	public void actionPerformed(ActionEvent e)
	{
		
		String cmd = e.getActionCommand();	
		
		if (cmd == "rename")
		{
			
			button1Name = (textField1.getText());
			
			
			 // This sets names for the object so we can reference 
			 // the object name easily later
			 
			button1.setName(button1Name);
			//This sets the name appearing on the button
			button1.setText(button1Name);

			outputText += previousButton1Name + "'s name changed to: " 
			+ button1Name + "\n";

			
			 // Save the button name as the previous name, so we
			 // can use it later
			 
			previousButton1Name = button1Name;
		}
		
		if (cmd == "1")
		{
			//An example of how to use getName();
			outputText += "Hello, my name is "  + button1.getName() 
			+ "\n";
		}
		
		textPane.setText(outputText);
	}
	
	public static void main(String[] args)
	{
		ObjectNames objectNames = new ObjectNames();
		objectNames.setSize(250, 350);
		objectNames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		objectNames.setVisible(true);
	}
}

In the next article we will pass a reference to a component to another class to show an example of a basic error message and how getName() can be convenient.