2
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Test extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                constructGUI();
            }
        });
    }

    private static void constructGUI() {
        JFrame frame = new JFrame("Testy");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.DARK_GRAY);
        centerPanel.setPreferredSize(new Dimension(100, 400));
        frame.add(centerPanel, BorderLayout.CENTER);

        Test eastPanel = new Test();
        frame.add(eastPanel, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
    }

    public Test() {

        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        Dimension d = new Dimension(50, 50);

        JButton button1 = new JButton("");
        button1.setPreferredSize(d);

        button1.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));

        button1.setMaximumSize(new Dimension(Integer.MAX_VALUE, button1.getMinimumSize().height));
        add(button1);

        JButton button2 = new JButton("");
        button2.setPreferredSize(d);

        button2.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));

        button2.setMaximumSize(new Dimension(Integer.MAX_VALUE, button2.getMinimumSize().height));
        add(button2);

        JButton button3 = new JButton("");
        button3.setPreferredSize(d);

        button3.setIcon(new ImageIcon(this.getClass().getResource("/Pictures/ellipse.png")));

        button3.setMaximumSize(new Dimension(Integer.MAX_VALUE, button3.getMinimumSize().height));
        add(button3);

        add(Box.createVerticalGlue());

    }

}

enter image description here

In my program I am trying to put the ellipse picture on top of all my buttons. As you can see in the image I posted, the ellipse.png is in the "Pictures" source folder.

However, the image does not appear on the JButtons for some reason.

I have read many posts but I can't see a way to solve my problem.

Also, here is a link to the actual ellipse picture:

https://maxcdn.icons8.com/Share/icon/Editing//ellipse_stroked1600.png enter image description here

Jack Kong
  • 109
  • 1
  • 10
  • 1
    I'm not a Eclipse user, but based on the layout of you project and the error message, I would say that `Pictures` is not included within the class path context of the application. You can try exporting the app as a Jar and extracting the contents to double check what's been included – MadProgrammer Oct 24 '17 at 03:25
  • @MadProgrammer I made an edit. The program runs but the ellipse picture does not appear on any of the buttons – Jack Kong Oct 24 '17 at 03:40
  • 1
    I'd also recommend not messing with the dimensions of the buttons either, none of the Swing components will scale the image – MadProgrammer Oct 24 '17 at 04:31
  • @MadProgrammer the application I'm creating requires the size of the buttons to be small. This is the only way I can figure out. – Jack Kong Oct 24 '17 at 04:33
  • 1
    Change the size of the images to meet your requirements – MadProgrammer Oct 24 '17 at 04:41
  • @MadProgrammer How would I do that? – Jack Kong Oct 24 '17 at 04:43
  • 1
    Either use a image editor or scale them at runtime - the first option is almost always the better one – MadProgrammer Oct 24 '17 at 04:46
  • This is duplication of https://stackoverflow.com/questions/10981650/jbutton-from-image – Oleg Cherednik Oct 24 '17 at 06:43

2 Answers2

1

First you have to use image that have smaller resolution size ( your image have 1600x1600 pixels , i would suggest 32X32)

Use image like this enter image description here

and refer below code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Test extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                constructGUI();
            }
        });
    }

    private static void constructGUI() {
        JFrame frame = new JFrame("Testy");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.DARK_GRAY);
        centerPanel.setPreferredSize(new Dimension(100, 400));
        frame.add(centerPanel, BorderLayout.CENTER);

        Test eastPanel = new Test();
        frame.add(eastPanel, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
    }

    public Test() {

        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        Dimension d = new Dimension(50, 50);

        JButton button1 = new JButton("");
        button1.setPreferredSize(d);

        ImageIcon imageIcon = new ImageIcon(System.getProperty("user.dir") + "/Pictures/ellipse.png");

        button1.setIcon(imageIcon);

        button1.setMaximumSize(new Dimension(Integer.MAX_VALUE, button1.getMinimumSize().height));
        add(button1);

        JButton button2 = new JButton("");
        button2.setPreferredSize(d);

        button2.setIcon(imageIcon);

        button2.setMaximumSize(new Dimension(Integer.MAX_VALUE, button2.getMinimumSize().height));
        add(button2);

        JButton button3 = new JButton("");
        button3.setPreferredSize(d);

        button3.setIcon(imageIcon);

        button3.setMaximumSize(new Dimension(Integer.MAX_VALUE, button3.getMinimumSize().height));
        add(button3);

        add(Box.createVerticalGlue());

    }

}

Your out-put should be like this.

enter image description here

Akila
  • 1,258
  • 2
  • 16
  • 25
  • It doesn't seem to work for me. I am using a Mac btw. Should I change "user.dir" to something else? – Jack Kong Oct 24 '17 at 04:24
  • 1
    Please see the picture of Gui I just posted – Jack Kong Oct 24 '17 at 04:29
  • Sadly I don't have a mac environment to test this code , anyway this link might help you >> [Java System.getProperty(“user.dir”) on Mac OS X](http://stackoverflow.com/questions/936714/java-system-getpropertyuser-dir-on-mac-os-x) – Akila Oct 24 '17 at 04:35
  • 1
    Sorry I am new, I don't understand how this. Is there an easier way than using System.getProperty? – Jack Kong Oct 24 '17 at 04:38
  • did you test with full path ? _Ex : Users/LuxuryMode/Desktop/image.png_ and images are showing ? if **yes** you have to figure out way to get abosulte path. if **not**, there is a issue in your image – Akila Oct 24 '17 at 04:48
  • [Path to file on a Mac](http://stackoverflow.com/questions/5972026/path-to-file-on-a-mac-filenotfoundexception) – Akila Oct 24 '17 at 04:51
0

Eclipse is picky about images and crap, so here is the workaround (sorry it's so long.)

Dont make a seperate source folder for your images. Put it in your main source folder under a subfolder called "assets" or something.

From there, create a buffered image and an input stream (java.awt.image.BufferedImage and java.io.InputStream)

Then you want to set the input stream to the image using the getResourceAsStream method.

Then do the following

bufferedImageName = ImageIO.read(inputStreamName);

From there, use the java.awt.Graphics library to paint it to the JButton.

Finished!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jacob B.
  • 423
  • 3
  • 12
  • 1
    *"From there, use the java.awt.Graphics library to paint it to the JButton"* ... or just make use of the buttons `icon` support, because, you know, it's there – MadProgrammer Oct 24 '17 at 04:31
  • Well yeah, that can work, but this way of doing it seems to work better with eclipse and all final products. Longer but safer imo. – Jacob B. Oct 24 '17 at 04:35
  • 1
    If you need to resort to "painting" the buttons manually, then there is something seriously wrong, regardless of what IDE you're using – MadProgrammer Oct 24 '17 at 04:40
  • Hey quick question @MadProgrammer , what do you develop on? – Jacob B. Oct 24 '17 at 04:42
  • Swing on Windows and Mac - I typically use Netbeans, but have used Eclipse and IntelliJ - at the end of the day, the IDE should be irrelevant - as it's the API that does the work – MadProgrammer Oct 24 '17 at 04:47
  • You are right, but i guess i just think with the end in mind. For me, i bundle my java.jars into Mac .apps, so when you use icon support, the Mac app has problems. My bad i just try to write in ways that work EVERY time, not just 99% of the time – Jacob B. Oct 24 '17 at 04:50
  • *"For me, i bundle my java.jars into Mac .apps, so when you use icon support, the Mac app has problems"* - Having done the same thing in past, it shouldn't, as the images should be contained within the Jar as embedded resources – MadProgrammer Oct 24 '17 at 08:08