简体   繁体   中英

how to properly save a picture path in a database and assign it into a button?

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);
                }
            }
        }
    });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM