sapdev logo background
sapdev logo sapdev logo
Comments

Code of simple Swing Java Applications




Below you will find the Java code for creating a simple Swing application.

Swing application to display picturs via a dropdown list

//package swing;
import java.awt.event.*;
import javax.swing.*;

public class Album implements ActionListener {
final static int NUM_IMAGES = 8;
final static int START_INDEX = 3;

ImageIcon[] images = new ImageIcon[NUM_IMAGES];
JPanel mainPanel, selectPanel, displayPanel;

JComboBox myChoices = null;
JLabel myIconLabel = null;

public Album() {
	//Create the Album selection and display panels.
	selectPanel = new JPanel();
	displayPanel = new JPanel();

	//Add various items to the sub panels.
	addItems();

	//Create the main panel to contain the two sub panels.
	mainPanel = new JPanel();
	mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
	mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

	//Add the select and display panels to the main panel.
	mainPanel.add(selectPanel);
	mainPanel.add(displayPanel);
}

//	  Get the images and set up the items.
private void addItems() {
	//Get the images and put them into an array of ImageIcons.
	for (int i = 0; i < NUM_IMAGES; i++) {
	  images[i] = createImageIcon("/swing/images/image" + i + ".jpg");
	}

	// Create a label for displaying the  images and
	// put a border around it.

	myIconLabel = new JLabel();
	myIconLabel.setHorizontalAlignment(JLabel.CENTER);
	myIconLabel.setVerticalAlignment(JLabel.CENTER);
	myIconLabel.setVerticalTextPosition(JLabel.CENTER);
	myIconLabel.setHorizontalTextPosition(JLabel.CENTER);
	myIconLabel.setBorder(
		BorderFactory.createCompoundBorder(
			BorderFactory.createLoweredBevelBorder(),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

	myIconLabel.setBorder(
		BorderFactory.createCompoundBorder(
			BorderFactory.createEmptyBorder(0, 0, 10, 0),
			myIconLabel.getBorder()));

	//Create a combo box with choices.
	String[] album =
		{	"Daughter",
			"Wife",
			"House",
			"Cat",
			"Paris",
			"Car" };
	myChoices = new JComboBox(album);
	myChoices.setSelectedIndex(START_INDEX); // we set as 3

	//Display the first image.
	myIconLabel.setIcon(images[START_INDEX]); //start at 3rd image
	myIconLabel.setText("");

	//	Add a border around the select panel.
	selectPanel.setBorder(
		BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder("Select Phase"),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

	//Add a border around the display panel.
	displayPanel.setBorder(
		BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder("Display Phase"),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

	//Add combo box to select panel and image label.
	displayPanel.add(myIconLabel);
	selectPanel.add(myChoices);

	//Listen to events from the combo box.
	myChoices.addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
	if ("comboBoxChanged".equals(event.getActionCommand())) {
		//Update the icon to display the new data.
		myIconLabel.setIcon(images[myChoices.getSelectedIndex()]);
	}
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
	java.net.URL imageURL = Album.class.getResource(path);

	if (imageURL == null) {
		System.err.println("Resource not found: " + path);
		return null;
	} else {
		return new ImageIcon(imageURL);
	}
}
	
// Create the GUI and show it.  For thread safety,
// this method should be invoked from the event-dispatching thread.
private static void createAndShowGUI() {
	//Make sure we have nice window decorations.
	JFrame.setDefaultLookAndFeelDecorated(true);
	//Create a new instance of Album.
	Album album = new Album();

	//Create and set up the window.
	JFrame frame = new JFrame("Family Album");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setContentPane(album.mainPanel);

	//Display the window.
	frame.pack();
	frame.setVisible(true);
}
public static void main(String[] args) {
	//Schedule a job for the event-dispatching thread:
	//creating and showing this application's GUI.
	javax.swing.SwingUtilities.invokeLater(new Runnable() {
		public void run() {
			createAndShowGUI();
		}
	});
}
}




comments powered by Disqus