image


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: image

  1. #1
    Join Date
    May 2005
    Posts
    4

    image

    I have this code form my application (a frame,a button, a label):


    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.filechooser.*;
    import java.util.*;
    import java.io.*;

    public class Frame1 extends JFrame {
    JPanel contentPane;
    JButton jButton1 = new JButton();
    JLabel jLabel1 = new JLabel();

    File m_CurrentFile;
    Hashtable m_AppElements;
    int m_CurrentPos;




    /**Construct the frame*/
    public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
    jbInit();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    }
    /**Component initialization*/
    private void jbInit() throws Exception {
    jButton1.setText("jButton1");
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    jButton1_mouseClicked(e);
    }
    });
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    jLabel1.setDoubleBuffered(true);
    jLabel1.setDisplayedMnemonic('0');
    contentPane.add(jButton1, BorderLayout.EAST);
    contentPane.add(jLabel1, BorderLayout.CENTER);
    }
    /**Overridden so we can exit when window is closed*/
    protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    System.exit(0);
    }
    }

    void jButton1_mouseClicked(MouseEvent e) {

    }
    }


    What i want:
    when i press the button to open a filechooser, to choose a image and to dislay the image using the label. How can i do that? (I olso want to use BufferedImage to make some transformation to the image)

    THNX!

  2. #2
    Join Date
    Dec 2004
    Location
    france
    Posts
    35

    i see

    well you just need instanciate the label you got as the new JLabel(new ImageIcon("image url ');
    that should be on the actionperformed method
    good luke
    Compil and run!

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Im not quite sure of this-

    You want to use the JLabel as a component for dynamically displaying
    selected images, you add this to the GUI, and then, when the user selects an
    image you do a new JLabel(....) then that is a new JLabel,
    with no connection whatsoever with the JLabel already displayed, -
    the pointer delivered to the container during component add is still the
    same and the container has no logic for detecting that the invoker has
    changed the pointer value stored in one of its variables.

    Changing the icon already diplayed is not all that easy either. In my
    opinion the JLabel component is not well suited for this, and you should use
    a JPanel extension instead.

    Here is a code example using a JPanel extension and a BufferedImage loaded
    from file.

    (Stating the obvious: ) for an applet the image load will have to be changed.
    For big images you will have to put the ImagePanel in a JScollPane, and
    do a validate() for that scrollpane on each call to setImage, - to get the
    scrollbars right.
    Attached Files Attached Files
    Last edited by sjalle; 05-08-2005 at 01:05 PM. Reason: Included the zip anyway
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center