Event Handler question

Hi all. I have a question on assigning Event Handlers.

This is a homework project so much of the code was already given I am simply filling it in. The area I can't figure out is matching the button's ActionListener with the method I was given.

The event is code I added, and I can not get it connected to the ButtonListener class they gave me.

Any help is appreciated.

privatevoid buildButtonPanel()

{

JPanel buttonPanel =new JPanel();

JButton button =new JButton("Button Panel Button");

//button.addActionListener(new ActionListener()

//{

//public void actionPerformed(ActionEvent e)

//{

}

privateclass ButtonListenerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent e)

{

int fileChooserStatus = fileChooser.showOpenDialog(ImageViewer.this);//Added int

if(fileChooserStatus == JFileChooser.APPROVE_OPTION)

{

File selectedFile = fileChooser.getSelectedFile();//Added

//Get the path of the selected file

//Read the image from the file

//Store the image in the label

//If the label displays text, remove it

pack();//Added

}

}

}

[2165 byte] By [Veldraina] at [2007-11-27 4:51:03]
# 1

You pass an instance of the ButtonListener class as a parameter to the jButton.addActionListener(ActionListener) method. This is demonstrated below.

button.addActionListener(new ButtonListener());

You can also do it on-the-fly like this:

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// code here

}

});

In the above example you create an anonymous ActionListener object. You can instantiate interfaces just like you do classes so long as you provide implementations for all required methods.

ktm5124a at 2007-7-12 10:04:38 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks alot.

I have the program mostly finished, but their are two last items. I can not the image into the label. Creating a label with the image at once would be easy, but I can't find the syntex to delete the text in a current label and switch it with the image.

Second, none of the panels or buttons are showing up. I tried addeding them into the ContentPane but I thought that they by default were included anyway.

Here is the whole code. Any commented areas the code I added, the rest is the code I was given to work with.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.File;

public class ImageViewer extends JFrame

{

private JPanel imagePanel;

private JPanel buttonPanel;

private JLabel imageLabel;

private JButton button;

private JFileChooser fileChooser;

private Container contentPane;

private ImageIcon image;

public ImageViewer()

{

buildImagePanel();

buildButtonPanel();

JFileChooser fileChooser = new JFileChooser(".");//Added

pack();

setVisible(true);

}

private void buildImagePanel()

{

JPanel imagePanel = new JPanel(new BorderLayout());//Added

JLabel imageLabel = new JLabel("Image Label 01");//Added

imagePanel.add(imageLabel);//Added

}

private void buildButtonPanel()

{

JPanel buttonPanel = new JPanel(new BorderLayout());//Added

JButton button = new JButton("Button Panel Button");//Added

button.addActionListener(new ButtonListener());//Added

buttonPanel.add(button);//Added

}

private class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int fileChooserStatus = fileChooser.showOpenDialog(ImageViewer.this);//Added int

String path;//Added

if(fileChooserStatus == JFileChooser.APPROVE_OPTION)

{

File selectedFile = fileChooser.getSelectedFile(); //Added

path = selectedFile.getPath();//Added

image = (new ImageIcon(path));//Added

imageLabel = (image);//Added

//If the label displays text, remove it

pack();//Added

}

}

}

public static void main(String[] args)

{

ImageViewer frame = new ImageViewer();

frame.setTitle("Image Viewer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

frame.setVisible(true);

}

}

Veldraina at 2007-7-12 10:04:38 > top of Java-index,Java Essentials,Java Programming...
# 3

In the future, Swing related questions should be posted in the Swing forum.

> I can't find the syntex to delete the text in a current label and switch it with the image.

label.setText("");

label.setIcon(....);

> Second, none of the panels or buttons are showing up.

Well, you do need to add then to the content pane. They don't just magically appear because you create them. How is the program supposed to know how you want the panels positioned in the content pane unless you specify this?

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Manager[/url] for working examples.

camickra at 2007-7-12 10:04:38 > top of Java-index,Java Essentials,Java Programming...