Reputation: 45
So I am trying to put an image on a JButton
by the means of the JButton
constructor
JButton button = new JButton(ImageIcon image)
I have a few icons are defined as
ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
The icon files are in a folder called
C:\Users\Wonseok\Documents\CompSciClass\Homework Files\icons.
The buttons that these icons are on are defined as
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);
I have these in a JPanel
.
My problem is that the icons aren't showing properly on the JButtons
, and I have no idea why. The buttons show up as small, thin, blank rectangles without an image on it. If you want a picture, please tell me how to post one from my computer. Is this my computer being annoying and glitchy? Or is there something wrong with my coding?
The pertinent code is
JPanel ButtonBar = new JPanel(new FlowLayout());
JPanel FileActions = new JPanel(new GridLayout(1, 2));
ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JPanel TextActions = new JPanel(new GridLayout(1, 3));
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);
public basic_text_editor()
{
super("Text Editor");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.WHITE);
setLayout(new FlowLayout(FlowLayout.LEFT));
FileActions.add(OpenButton);
FileActions.add(SaveButton);
TextActions.add(CutButton);
TextActions.add(CopyButton);
TextActions.add(PasteButton);
ButtonBar.add(FileActions);
ButtonBar.add(TextActions);
add(ButtonBar);
}
Upvotes: 1
Views: 2542
Reputation: 45
Thanks all, but I've found the answer to my problem.
The file extension shouldn't have been .jpeg, which is the name for the type of file, but should've been .jpg.
Thanks for your help, anyways. I'll make a copy of the folder in my project folder and use relative paths. :D
Upvotes: 0
Reputation: 46841
Put the images in the project itself otherwise it will not work when this code is shipped on another machine. Avoid absolute path.
Looking an image 2.png
from resources folder
ImageIcon folderIcon = new ImageIcon("resources/2.png");
OR
ImageIcon icon = new ImageIcon(ImageIO.read(new File("resources/2.png")));
OR
Try this one if the image is in the package where the class is present
ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("2.png")));
OR
I never suggest you to use absolute path but you can try it.
ImageIcon icon = new ImageIcon(ImageIO.read("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg"));
OR
ImageIcon icon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
If you want to re-size the image then use this method.
public BufferedImage resizeImage(BufferedImage originalImage, int width, int height)
throws IOException {
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
how to convert an Image(BufferedImage) into ImageIcon?
ImageIcon icon = new ImageIcon(image);
Here is the project structure
Upvotes: 1