Question

My main dialog has gotten so cluttered with buttons. I thought it would be pretty cool to just add a tool bar with some of the buttons to the dialog.

I am currently using a image in a application toolbar to start my application. It is in Plugin.xml code

 icon="icons/ediOrb.png"

So in my Dialog I am trying to use the same icon for my refreshButton

JToolBar toolbar = new JToolBar();
refreshButton = new JButton(new ImageIcon("icons/ediOrb.png"));
refreshButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        refreshTable();
     }
  });
  toolbar.add(refreshButton);
  centerPanel = new JPanel();
  centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
  centerPanel.setPreferredSize(new Dimension(850, 450));
  centerPanel.add(Box.createRigidArea(new Dimension(5, 0)));
  centerPanel.add(currentPanel);
  centerPanel.add(selectionsPanel);
  centerPanel.add(Box.createHorizontalGlue());
  centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
  centerPanel.add(buttonPanel);
  getContentPane().add(toolbar,BorderLayout.PAGE_START );
  getContentPane().add(centerPanel);

There is no image showing for the button. It only shows a tiny spot. It is not throwing any errors, just does not show the image.

Any Ideas?

Was it helpful?

Solution

I had the same issue. In Netbeans I had to go to:

Run | Set Project Configuration | customize -- 

Then add the Working Directory path that contained my project and the icons. I'm not sure how to do this in a different IDE but my Icons showed up after this fix.

OTHER TIPS

Try this:

refreshButton = new JButton(new ImageIcon(getClass().getResource("/icons/ediOrb.png")).getImage()));

If your icons folder is under src folder.

JButton refreshButton = new JButton();
  try {
    Image img = ImageIO.read(getClass().getResource("icons/ediOrb.png"));
    refreshButton.setIcon(new ImageIcon(img));
  } catch (IOException ex) {
  }

Try

refreshButton .setIcon(new ImageIcon(getClass().getResource("icons/ediOrb.png"))); 

However, make sure the image is not too large(happened to me once) + the path is correct. Test it with giving the COMPLETE path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top