Results 1 to 17 of 17
  1. #1
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Display Icon from path

    Hello guys and girls.

    I got question about icon displayed from path. So i got project building in the netbeans and all is ok. Now i wana to make Jlabel who will display icon from path what i saved in the table.

    How i can achive this? Any tutorials or topics you can point me up?

    I got this Code that i thinking to implement in project. My question is do I String Path set for Master table elements or to make it difrante?

    And yea i am still newbie at Java

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Main extends JFrame {
        JLabel lblPic = new JLabel();
        JButton butSelectPic = new JButton("Select picture...");
        JTextField txtPic = new JTextField("");
        JPanel pnTemp = new JPanel(new BorderLayout());
        String filLocation;
     
        private void initComponents() {
            this.setLayout(new BorderLayout());
     
            this.add(lblPic, BorderLayout.CENTER);
            pnTemp.add(butSelectPic, BorderLayout.CENTER);
            pnTemp.add(txtPic, BorderLayout.SOUTH);
            this.add(pnTemp, BorderLayout.SOUTH);
        }
     
        public Main() throws IOException {
            initComponents();
     
            butSelectPic.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e) {
                    JFileChooser fchoose = new JFileChooser(filLocation);
                    int ff = fchoose.showOpenDialog(null);
                    if (ff == JFileChooser.APPROVE_OPTION) {
                        filLocation = fchoose.getSelectedFile().getParent();
                        try {
                            BufferedImage myPicture2 = ImageIO.read(fchoose.getSelectedFile());
                            int type = myPicture2.getType() == 0? BufferedImage.TYPE_INT_ARGB : myPicture2.getType();
                            BufferedImage resizedImage = new BufferedImage(lblPic.getWidth(), lblPic.getHeight(), type);
                            Graphics2D g = resizedImage.createGraphics();
                            g.drawImage(myPicture2, 0, 0, lblPic.getWidth(), lblPic.getHeight(), null);
                            g.dispose();
     
                            lblPic.setIcon(new ImageIcon(resizedImage));
                        } catch (IOException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });
     
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(640,480);
            this.setVisible(true);
        }
     
        /**
         * @param args the command line arguments
         */
        @SuppressWarnings("ResultOfObjectAllocationIgnored")
        public static void main(String[] args) throws IOException {
            new Main();
        }
    I got implent this code, and now i got show piucutre at App, how to save to Table and bind to it?
    Last edited by Edmund; 01-12-2012 at 02:38 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Display Icon from path

    do I String Path set for Master table elements or to make it difrante?
    Could you explain what this means? Especially the last word: difrante.

  3. #3
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    Quote Originally Posted by Norm View Post
    Could you explain what this means? Especially the last word: difrante.
    First sorry my English is quite bad. That word should be "A different"

    I mean how to display image into Jlabel from master table path ( cell what have path in it ). I have used a ActionListener to display a Image into application but that is not what I am looking for.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Display Icon from path

    Sorry I don't know what you mean by "master table path".
    There are several basic ways of getting the data from a image file to create an Image object:
    Use a File object
    or use an InputStream
    or Use a URL to a resource.
    There are different versions of the ImageIO read method that you can use to read the image file depending on where it is and how you are accessing it.

    I don't know that an ActionListener would be involved in reading an image.

  5. #5
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    I have mean as "Master Table Path" that is a colum in the table what have a URL to image.

    Yea i see now that code what i posted is not eaven close what i need. I am currently searching to see how to get URL to JTextField whit Jfilechooser. And icon should be reading URL from JtextField and display at APP when i select a row at the table.

    My post looking like ape was try to explain. Sorry on that aswell.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Display Icon from path

    Do you have new code that shows the problem?

  7. #7
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    No, I am still looking for that what I write on last post. I got only Jfilechooser but still not get after that. I will try to make new code to work. Then I can post code. But for now is reading a lot of page's.

    This is what i have made. In gui added a button, below a jtextfield, on button added a actionlistener.

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            String filLocation = null;
            JFileChooser fchoose = new JFileChooser(filLocation);
                    int ff = fchoose.showOpenDialog(null);
                    if (ff == JFileChooser.APPROVE_OPTION) {
                        filLocation = fchoose.getSelectedFile().getParent();
                        File selectedPfile = fchoose.getSelectedFile();
                        myJTextField.setText(selectedPfile.getAbsolutePath());
                        }
                        setSaveNeeded(true);
        }
    Now I will try to edit a jlabel to read a JtextField in code above named myJTextField and to display as icon at application.
    Last edited by Edmund; 01-12-2012 at 08:57 PM. Reason: Addin a code

  8. #8
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    Okey i am officialy stuck.

    I got jtextfield whit path, but i dont know how to send to Jlabel to be a Icon. How to String text = textField.getText(); jlabel to read as path and display it?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Display Icon from path

    Do you have a String that contains the path to the file?
    Given that String, you can use the ImageIcon class's constructor to load/read the image.
    Then use that ImageIcon object with a method in the JLabel class to set its icon.

  10. #10
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    I am working in netbeans, So I have to edit code like this one jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp/resources/park-sculpture.jpg"))); // NOI18N

    So i need to write all that whitin a code or make some other way. And i dont know wher and how to start it? Fist to get text form textfield and turn in it into a String and then send as path?

    Realy dont know how to do that.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Display Icon from path

    get text form textfield and turn in it into a String and then send as path?
    You need to read the API doc for each of the classes you are working with to find the method to use.
    The API doc for the classes is here:
    Java Platform SE 6
    Find the class in the lower left, click on it and the doc shows in the main frame.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Display Icon from path

    Netbeans should have nothing to do with it, unless you are using the GUI builder, in which case I would say STOP, because you clearly don't know enough to be doing that.
    Norm has told you how to get an ImageIcon, and what constructor to use...

  13. #13
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    How I see it I need to write within icon to read a path and display it, and if is null to display a default Image.

    I will dig in it this link what you give me. Blargh so, reading for several hours.

    Netbeans should have nothing to do with it, unless you are using the GUI builder, in which case I would say STOP
    To late, I will doo the reading then try to implement in it.
    Last edited by Edmund; 01-13-2012 at 02:44 PM.

  14. #14
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    Again me, i got working on my problem i make several way to approach but i dont know when I am making mistake.

    Netbeans generated code is this ( i have edit to getResource )

    Java Code:
    Jico.setIcon(new javax.swing.ImageIcon(getClass().getResource(path)));
    Added custom code above generated block,

    Java Code:
    private URL getResource(String Path) throws IOException {
            {
            String Path = fdField.getText();
            BufferedImage image = ImageIO.read(new File(Path));
            int type = image.getType() == 0? BufferedImage.TYPE_INT_ARGB : image.getType();
            BufferedImage resizedImage = new BufferedImage(Jico.getWidth(), Jico.getHeight(), type);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(image, 0, 0, Jico.getWidth(), Jico.getHeight(), null);
            g.dispose();
            Jico.setIcon(new ImageIcon(resizedImage)); }
        }
    problem is in second row "String Path = fdField.getText();" give me a error path is already defined in getResource(java.lang.string)

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Display Icon from path

    You have the variable path define in two places:
    Java Code:
    private URL getResource(String Path) throws IOException {
            {
            String Path = fdField.getText();
    Should one of them be renamed?

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Display Icon from path

    "String Path = fdField.getText();" give me a error path is already defined in getResource
    It is too. That's the name you gave to the parameter to the method.
    private URL getResource(String Path)
    Also, learn to respect Java coding conventions: variable names start with a lowercase letter.
    Code Conventions for the Java(TM) Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    Edmund is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Display Icon from path

    Something i mess up, i was try to doo this

    Quote Originally Posted by Norm View Post
    Do you have a String that contains the path to the file?
    Given that String, you can use the ImageIcon class's constructor to load/read the image.
    Then use that ImageIcon object with a method in the JLabel class to set its icon.
    Thinking that i am mix several things, must doo more of defining code for that.

Similar Threads

  1. Replies: 3
    Last Post: 12-05-2011, 01:28 PM
  2. setting class-path & Library Path in ubantu
    By programmer_007 in forum Eclipse
    Replies: 18
    Last Post: 02-22-2010, 12:31 PM
  3. Not able to display icon with JCheckBox
    By anindde in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2009, 03:19 AM
  4. can java program display icon on unix?
    By java2008 in forum Advanced Java
    Replies: 1
    Last Post: 10-06-2008, 07:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •