How to add ImageIcon to JToolBar

I'm trying to add an icon to a toolbar, but what is the best place to place it? My desktop or I have to create a new file in the project file or add all the images because it does not appear, 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]);
    }
+2
source share
2 answers

I would use Action. Here is the constructorAbstractAction

  • public AbstractAction(String name, Icon icon) - Creates an action with the specified name and a small icon.

    Parameters:
    name - name (Action.NAME) for the action; null value is ignored
    icon - small icon (Action.SMALL_ICON) for the action; null is ignored

Action , . , , , JMenuItem JMenu, . , , , .

Action action = new AbstractAction("someActionCommand", someIcon) {
    @Override
    public void actionPerformed(ActionEvent e) {
        // do something.
    }
};

toolbar.add(action);

, . JMenuItem , .

Action .



, @MadProgrammer, ,

 ImageIcon icon = new ImageIcon(MyClass.class.getResource("/resources/images/image.png"));

/resources/images src, getResource() URL-. IDE .

 ProjectRoot
           src
               resources
                       images
                             image.png

,


, JMenuItem JToolBar . , JToolBar , , Action, . JToolBar

"open.gif"

ImageIcon icon = new ImageIcon(
            ActionTest.class.getResource("/resources/image/open.gif"));

enter image description here

enter image description here

. !

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();
            }
        });
    }
}
+6

(, jar). , - , , , jar, . , , .

.

, , "resources" , , . .

, , - File.

Class#getResource(String), URL Class#getResourceAsStream(String), InputStream. .

ImageIcon(String) , , , , ImageIcon , URL , ,

icon[i] = new ImageIcon(getClass().getResource(iconFiles[i]));

.

(.. , ). , .

, getResource . .

+5

Source: https://habr.com/ru/post/1524946/


All Articles