How to add ImageIcon to JToolBar
I'm trying to add an icon to a toolbar, but what's the best place to put it? My desktop, or should I create a new file in the project file or add all the pictures, because it's not showing, so here 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]);
}
I would use one Action
. this is the AbstractAction
constructor
public AbstractAction(String name, Icon icon)
- Create an action with the specified name and small icon.Parameters:
name - the action name (Action.NAME); the icon with a value of null
will be ignored - the small icon of the action (Action.SMALL_ICON); a null value will be ignored
The benefit of using an Action
is that it can be reused for a component with a similar purpose. So, say, you want to have an icon button in the toolbar that opens a file, and also have an icon button JMenuItem
in JMenu
that that also opens a file. They can share the same action, and thus the same icon, action command, and action to perform.
Action action = new AbstractAction("someActionCommand", someIcon) {
@Override
public void actionPerformed(ActionEvent e) {
// do something.
}
};
toolbar.add(action);
The code above will automatically show you the icon, but not the String. It JMenuItem
will place both the string and the icon in it.
Then just add Action
to the toolbar.
See more in " How to Use Actions"
To answer your real question, as @MadProgrammer pointed out, you should load your images as embedded resources , using
ImageIcon icon = new ImageIcon(MyClass.class.getResource("/resources/images/image.png"));
in a /resources/images
directory src
, and getResource()
returns a URL. After building, the IDE should copy the files to your classpath.
ProjectRoot
src
resources
images
image.png
You will find that when using files from the file system, it will not be available after deployment
Here is an example where the same action is shared JMenuItem
with JToolBar
the button. Note that after JToolBar
all I had to do was add it Action
, I didn't need to create a button for it. This JToolBar
automatically makes it a button with no action command
I use "open.gif" in the following file structure and use
ImageIcon icon = new ImageIcon(
ActionTest.class.getResource("/resources/image/open.gif"));
this is the result
Here is the code. enjoy your meal!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class ActionTest {
public ActionTest() {
ImageIcon openIcon = new ImageIcon(
ActionTest.class.getResource("/resources/image/open.gif"));
ImageIcon saveIcon = new ImageIcon(
ActionTest.class.getResource("/resources/image/save.gif"));
ImageIcon newIcon = new ImageIcon(
ActionTest.class.getResource("/resources/image/new.gif"));
Action openAction = new AbstractAction("Open", openIcon) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Open File");
}
};
Action saveAction = new AbstractAction("Save", saveIcon) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Save File");
}
};
Action newAction = new AbstractAction("New", newIcon) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("New File");
}
};
JMenuItem openMenuItem = new JMenuItem(openAction);
JMenuItem saveMenuItem = new JMenuItem(saveAction);
JMenuItem newMenuItem = new JMenuItem(newAction);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(newMenuItem);
menuBar.add(fileMenu);
JToolBar toolBar = new JToolBar();
toolBar.add(Box.createHorizontalGlue());
toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
toolBar.add(newAction);
toolBar.add(openAction);
toolBar.add(saveAction);
JFrame frame = new JFrame("Toolbar and Menu Test");
frame.setJMenuBar(menuBar);
frame.add(toolBar, BorderLayout.PAGE_START);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ActionTest();
}
});
}
}