I am trying to add a icon to a toolbar but what is the best place to put it in? My desktop or should I make a new file in the project file or add all the pictures in because it is not showing and this is my code:
JToolBar toolBar = new JToolBar();
String[] iconFiles = {"pen-icon","",""};
String[] buttonLabels = {"New","Open","Save"};
icon = new ImageIcon[iconFiles.length];
Obutton = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; ++i) {
icon[i] = new ImageIcon(iconFiles[i]);
Obutton[i] = new JButton(icon[i]);
Obutton[i].setToolTipText(buttonLabels[i]);
if (i == 3)
toolBar.addSeparator();
toolBar.add(Obutton[i]);
}
Resources of this type are typically best contained within the application context (such as a jar file). This reduces the chances of someone tampering with it as it's much more time consuming to unpack, modify and repack a jar file then simply replace a file. It also reduces what you need to distribute as it becomes self-contained.
These are known as embedded resources.
Where you would put them within this context is up to up, many people use a "resources" folder to store these types of files, but sometimes, you may want something that is relative to the context of the class. It's up to you.
This raises issues with loading these resources, as you can no longer reference them using something like
File
.In general you can use
Class#getResource(String)
, which returns aURL
orClass#getResourceAsStream(String)
which returns aInputStream
. This provides you with all you need to load these embedded resources.ImageIcon(String)
expects the value to be a file reference, which means it won't work for embedded resources, butImageIcon
provides a constructor that takes aURL
as a reference, this means you would need to useTo load your images.
Based on your example, the images would need to be relative to the class (ie within a directory structure the same as your package structure). How you achieve this will depend on your development environment.
Remember, you can also specify relative paths to
getResource
and even an absolute path in some contexts. An absolute path basic prefixes the elements of class path to the specified path when it searches for the resources.I would use an
Action
. Here is theAbstractAction
constructorpublic AbstractAction(String name, Icon icon)
- Creates an Action with the specified name and small icon.Parameters:
name - the name (Action.NAME) for the action; a value of null is ignored
icon - the small icon (Action.SMALL_ICON) for the action; a value of null is ignored
The benefit of using an
Action
is that is can be reused for components with similar purposes. So say you want to have an icon button in the toolbar to open a file, and also have aJMenuItem
in aJMenu
that also opens a file. They could share the same action, thus sharing the same icon, action command, and action to perform.The above will automatically put the icon for you, but not the String. In a
JMenuItem
it would put both the String and the icon.Then just add the
Action
to the tool bar.See more at How to use Actions
To answer you real question, as @MadProgrammer noted, you should be loading your images as an embedded resource, using
where the
/resources/images
directory is in thesrc
, andgetResource()
returns a URL. Upon build, your IDE should copy the files into the class path for you.You'll come to find that when using a file from the file system, will not work upon time of deployment
Here's an example, where the
JMenuItem
and theJToolBar
button share the same action. Notice that in theJToolBar
all I have to do is add theAction
, I don't need to create a button for it. TheJToolBar
automatically makes it a button, without the action commandI use this "open.gif" from the below file structure and use
Here's the result
Here's the code. Enjoy!