0

How can I know the path of the picture that will be store in this button. Also, what type of picture will you recommend me to upload in this button ?

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();              
            int result = fc.showOpenDialog(null);

            if (result == JFileChooser.APPROVE_OPTION) {              
                try {
                    File file = fc.getSelectedFile();
                    btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        }
    });
dic19
  • 17,821
  • 6
  • 40
  • 69
dens14345
  • 192
  • 1
  • 4
  • 17

1 Answers1

2

how can i know the path of the picture that will be store in this button

This can be easily done by calling File.getPath() method:

File file = fc.getSelectedFile();
System.out.println(file.getPath());

Additionaly you can store this path in the button through JComponent.putClientProperty(Object key, Object value):

File file = fc.getSelectedFile();
btnNewButton.putClientProperty("imagepath", file.getPath());

what type of picture will you recommend me to upload in this button ?

It can be JPG, PNG, BMP, WBMP and GIF, as per javax.imageio package description. Be aware Java doesn't support ICO format natively: Adding image to JButton

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • thank you so much for your simplified answer sir . thanks for helping beginners like me :D – dens14345 Jan 24 '14 at 14:49
  • last question , I saved the imagepath in the database, then how can i set it to the same button when i navigate to the table row containing the path . thanks in advance =) – dens14345 Jan 24 '14 at 15:17
  • @dens14345 that's hard to say without seeing your code. Maybe you should post this in a different question, including relevant code and description of your goal and problem. – dic19 Jan 24 '14 at 15:23