0
    public class DisplayPanel extends JPanel {

        private final static ButtonGroup buttonGroup = new ButtonGroup();


        static JRadioButton [] radioButtons = new JRadioButton [4];
        static JLabel [] labels = new JLabel [4];

        ImageIcon [] icons = {

                new ImageIcon("HEX Disabled.jpg"), 
                new ImageIcon("DEC Disabled.jpg"),
                new ImageIcon("OCT Disabled.jpg"),
                new ImageIcon("BIN Disabled.jpg")
        };

        ImageIcon [] selectedIcons = {

                new ImageIcon("HEX Selected.jpg"),
                new ImageIcon("DEC Selected.jpg"),
                new ImageIcon("OCT Selected.jpg"),
                new ImageIcon("BIN Selected.jpg")
        };

  more stuff here.... 

And then I try to set the icon here inside a for loop...

radioButtons[i].setSelectedIcon(icons[i]);
radioButtons[i].setSelectedIcon(selectedIcons[i]);

My question is, if I have a source folder called "icons", how exactly can I access that folder and use the images within to set the icons for my buttons?

I've tried....

new ImageIcon("img/HEX Selected.jpg"),

I've also tried putting in the the bin folder and using

new ImageIcon("bin/HEX Selected.jpg"),

as well as

new ImageIcon("bin\\HEX Selected.jpg"),

What do I need to correct here to make it work?

Thanks,

Thomas

Thomas Walker
  • 145
  • 1
  • 7
  • See [How to Use Icons](https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) for and an example of loading an image as a resource. Basically your classpath will be searched to find the file. – camickr Jul 22 '18 at 23:49
  • Nothing I have tried there has worked. Any suggestions? – Thomas Walker Jul 23 '18 at 00:10
  • Post your [mcve] demonstrating the problem. Put your image files in the same directory as your class file. – camickr Jul 23 '18 at 01:02
  • 1
    *"if I have a source folder called "icons""* - Well, first, none of your search paths include `icons`, so not really sure about that. The "icons" need to be within the class path context of the App, generally this is achieved by having them included in the Jar. How this is actually achieved is dependent on your IDE and build system – MadProgrammer Jul 23 '18 at 01:30
  • You will want to read about current directories and relative filenames. There is no way to guarantee the current directory when you run your program, so you should always place your images in the same location as your compiled class files, and load the images with [Class.getResource](https://docs.oracle.com/javase/10/docs/api/java/lang/Class.html#getResource%28java.lang.String%29). – VGR Jul 23 '18 at 02:22
  • @MadProgrammer yeah I copied and pasted code wrong. I was testing to see if different folders worked. I ended up solving my problem on my own. Turns out I had a duplicated line of code that set the icon and then set it to null. Took a few hours until I noticed it. – Thomas Walker Jul 23 '18 at 23:44

0 Answers0