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)
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.
Last edited by sjalle; 05-08-2005 at 01:05 PM.
Reason: Included the zip anyway
Bookmarks