Hi everyone,

I am having a little(lot of..) trouble figuring out how to combine a GUI I have made and the code that goes with it.

I am vaugley familiar with action listener process, and I understand this is how to give functionality to buttons etc.

However, the part I cannot get my head around is:

I have a button that when pressed, loops through and array[5] and prints out a correlating image.

Do I put this code in the GUI class or in my main class?

Any help no matter how small is welcome.


I guess that this is a question of design - Do you want the GUI class to be only for the GUI and the logic in another class or do you want all of the information to be together? Regarding to "what will work" - both approaches will work.

Separation of user interface and logic/model is a fundamental design pattern that is followed by all serious code.
Here's a simple heuristic for determining where to put any particular function:
Ask yourself "suppose I write two versions, one with a GUI, one with a command-line interface (or maybe an HTML web interface)" now, what will be common to both those versions and what will have to be different? All the common stuff belongs in the logic/model class(es).

I would like to keep the GUI and the logic seperate.

Here is my code so far

The GUI:

import java.awt.*;
import javax.swing.*;

public class GUI extends JFrame {


		setTitle("Single Card Printout");
		setLayout(new BorderLayout(50,100));
		add(createButtonPane(), BorderLayout.SOUTH);
		add(createContentPane(), BorderLayout.CENTER);
	private Container createContentPane() {
		JPanel pane = new JPanel();
		// I want the generated picutre to go here as a JLabel.
		JLabel b = new JLabel("Picture here");

		return pane;
	private Container createButtonPane() {
		JPanel pane = new JPanel();
		pane.setLayout(new GridLayout());
		JButton button1 = new JButton("Press to show picture");
		return pane;
	// This is the event handler for ActionListener, called actionPerformed.
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button1)
		// How can I implement my logic here?	


import java.io.*;
import javax.swing.*;

class CC {

public static void main(String[] args) {
	// Make a new User interface obj.
	GUI g = new GUI();

	String imageName = "2c.gif";

	// When the button is pressed I would like the image name to be retireved and used for an ImageIcon location.

It seems that, in this case, there is very little non-GUI logic.
When you say "retrieve the image" what exactly does that mean? Are there multiple images? Where are they retrieved from? What is the "retrieval" process?

Ok,to simplify I will have an object called Lamp.With this lamp object it has an attribute called "source" which holds the image location for this object's picture.

When I call the function getSource() it will return a string - "lamp.gif".

I then want to set this as an imageIcon and display it on the GUI main component.
However, I am puzzled as to how I can do this.Do I need to pass the imageIcon back to the GUI?

Please recognize that I don't have a full spec. for all this, so I will have to make a lot of assumptions and, if I guess wrong, I'll probably send you the wrong way. So one more quick question:
Is the Lamp object one of a number of similar objects? Are they all members of a class? How does the user say which object's picture they want to see - eg is there a list of objects, or object names, or what>

I have an array of Lamps[4].

Each Lamp object will have the attributes - name, and imageSource.

So when I loop through these 4 "Lamp" objects I print the name, but Ideally what I would like to do is show a picture of each lamp after this, with a flowlayout..

lamp1 picture, lamp2 picture, lamp3 picture, lamp4 picture.

I am able to get a generic picture to display when I hard code it in my gui class.However, when it comes to combining my GUI, Lamp and Main classes I am stuck.

So when you create the GUI you can pass the array of Lamps as a parameter to the constructor and keep a local copy of that reference in the GUI instance. It's a standard way of associating the GUI with its underlying model - in this case the "model" is just the array of objects.
Now you can use the objects in the array to query their imagesources, read in the images, and display them as the GUI requires.
Returning the file name is a reasonable design - it's up to the GUI whether it wants to read that file into an ImageIcon or a BufferedImage or whatever. The only suggestion I would make is to return a URL object rather than a String filename. That way the GUI doesn't have to find out what directory etc the files are in.
Alternatively return an Image object which the GUI can then turn into an ImageIcon if it wants. I don't think there's one "right" answer here.

Thank you for all the help and patience James, I have finally figured out how to make it work.It never occurred to me to think of the GUI class object...objectively.And too pass objects or call methods on it from inside the main.As a beginner this concept escaped me.Once again thank you :)