0

I am trying a simple code to add ImageIcons to Jbutton. I tried with JmenuBar too, but not able to import them on GUI application. This is the starter code I am using and the image address is correct.


import javax.swing.*;
import java.awt.*;
class demo
{
//constructor being used
demo()
{
//creating our Frame object
JFrame frame = new JFrame("JToolBar Demo");
//creating out tool bar object
JToolBar toolbar = new JToolBar("Applications");
//adding button icons instead of text
JButton btnCalendar = new JButton(new ImageIcon("exit.png"));
JButton btnClock = new JButton(new ImageIcon("exit2.png"));
JButton btnContacts = new JButton(new ImageIcon("src/images/Contacts1.png"));
JButton btnMail = new JButton(new ImageIcon("src/images/Mail1.png"));
JButton btnMessages = new JButton(new ImageIcon("src/images/Message1.png"));
//creating combo box object
//JComboBox fonts=new JComboBox();
//adding combo box items
//fonts.addItem("Arial");
//fonts.addItem("Times New Roman");
//fonts.addItem("Comic Sans Ms");
//adding combo box and buttons to our tool bar
toolbar.add(btnCalendar);
toolbar.add(btnClock);
toolbar.add(btnContacts);
toolbar.add(btnMail);
toolbar.add(btnMessages);
//toolbar.add(fonts);
//setting properties for the Frame
frame.setLayout(new BorderLayout());
frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setVisible(true);
}
public static void main(String args[]) throws Exception
{
//calling our class
new demo();
}
}


This is the output GUI I am getting: enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sanya
  • 129
  • 1
  • 6
  • `"...the image address is correct."` -- the onus is on you to *prove* this and not to assume *anything*. Have you printed out the "user.dir" property? Where are your files in relation to the user's directory? And why aren't you using resources and not files? – Hovercraft Full Of Eels Jul 22 '20 at 21:10
  • @HovercraftFullOfEels The images address are correct. 2 of them are in the same directory and others are in the folder src. I tried with implementing just one also and keeping the image in the same folder as my java class file but still I am getting an empty GUI with no image. I did not get what you mean by resources? – Sanya Jul 22 '20 at 21:14
  • You still haven't told me where the images are relative to the user's directory. What happens when you do `System.out.println(System.getProperty("user.dir"));` – Hovercraft Full Of Eels Jul 22 '20 at 21:15
  • 1
    For resources, please check this question: [How to correctly get image from 'Resources' folder in NetBeans](https://stackoverflow.com/questions/6845231/how-to-correctly-get-image-from-resources-folder-in-netbeans) as well as this question: [What's the difference between a Resource, URI, URL, Path and File in Java?](https://stackoverflow.com/questions/27845223/whats-the-difference-between-a-resource-uri-url-path-and-file-in-java) – Hovercraft Full Of Eels Jul 22 '20 at 21:17
  • The path where this source code and image files are. I am getting this. Okay. I will go through those posts. Thanks a lot! @HovercraftFullOfEels – Sanya Jul 22 '20 at 21:18

0 Answers0