Site icon TestingDocs.com

Write a Sample Java Swing Application with GUI components?

Problem Statement:

Write a Sample Java Swing Application with GUI components. Run the application and execute some testcases on it.

To answer the question, we will code a simple java swing application that keeps track a list of movies that you want to watch for a holiday season.

Application GUI

Below is the GUI window of the application that we will build to add the movies to watch list. We gonna capture movie details like Name, Lead Actor, Year of Release in the List.

 

Application Java Classes

The swing application will have three classes. I will later show you how the application works by adding some movies that I would like to watch in the coming holiday season.

MovieWatchList

This contains the main method where MovieTrackerFrame object will be created.

import javax.swing.JFrame;

public class MovieWatchList {

private static final int WIDTH = 700;
 private static final int HEIGHT = 600;

public static void main(String[] args) {
 MovieTrackerFrame frame = new MovieTrackerFrame("YetToWatchList",WIDTH,HEIGHT);
 frame.setVisible(true);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 } 
}

 

MovieTrackerFrame

Main part of the program. This frame extends Swing JFrame class. This class has the GUI components of the application and button listeners for event handling. For example to add movie when the button is clicked. AddMovieListener: read the inputs and create a movie object, adds the movie object to the movieList and appends it.

public class MovieTrackerFrame extends JFrame
{

private static final long serialVersionUID = 1L;
 private JLabel movieNameLabel;
 private JLabel movieLeadActorLabel;
 private JLabel movieYearLabel;
 private JTextField movieNameField;
 private JTextField movieLeadActorField;
 private JTextField movieYearField;
 private JLabel siteInfoLabel;
 private JButton addMovieButton;
 private JButton resetInputButton;
 private JButton resetOutputButton;

private JTextArea output;
 private ArrayList<Movie> movieList;

public MovieTrackerFrame(String title,int width,int height) 
 {
 super(title);
 final int ROW = 20;
 final int COLUMN = 70;
 movieList = new ArrayList<Movie>();
 output = new JTextArea(ROW, COLUMN);
 output.setEditable(false);
 output.setText("\t" + "MovieName" + "\t" + "LeadActor" + "\t" + "Release Year" + "\n");
 createComponents();
 setSize(width, height);
 }

private void createComponents()
 {
 /* GUI for the frame */
 final int FIELD_WIDTH = 60;
 movieNameLabel = new JLabel("MovieName: ");
 movieNameField = new JTextField(FIELD_WIDTH);
 movieLeadActorLabel = new JLabel("LeadActor: ");
 movieLeadActorField = new JTextField(FIELD_WIDTH);
 movieYearLabel = new JLabel("Release Year: ");
 movieYearField = new JTextField(FIELD_WIDTH);

addMovieButton = new JButton("AddMovie");
 resetInputButton = new JButton("ResetInput");
 resetOutputButton = new JButton("ResetOutput");
 
 siteInfoLabel = new JLabel("www.TestingDocs.com");

/* Define action listeners */
 ActionListener listener1 = new AddMovieListener();
 addMovieButton.addActionListener(listener1);
 ActionListener listener3 = new ResetInputListener();
 resetInputButton.addActionListener(listener3);
 ActionListener listener4 = new ResetOutputListener();
 resetOutputButton.addActionListener(listener4);

/* Adding components to panel */
 JPanel jpanel = new JPanel();
 jpanel.add(movieNameLabel);
 jpanel.add(movieNameField);
 jpanel.add(movieLeadActorLabel);
 jpanel.add(movieLeadActorField);
 jpanel.add(movieYearLabel);
 jpanel.add(movieYearField);

jpanel.add(addMovieButton);
 jpanel.add(resetInputButton);
 jpanel.add(resetOutputButton);

JScrollPane scroll = new JScrollPane(output);
 jpanel.add(scroll);
 
 jpanel.add(siteInfoLabel);
 add(jpanel);
 }

class AddMovieListener implements ActionListener
 {
 public void actionPerformed(ActionEvent event)
 {
 String movieName = movieNameField.getText();
 String movieLead = movieLeadActorField.getText();
 String releaseYear = movieYearField.getText();
 Movie movie = new Movie(movieName, movieLead, releaseYear);
 movieList.add(movie);
 output.append("\t" + movieName + "\t" + movieLead + "\t" + releaseYear + "\n");
 }
 }


 
 class ResetInputListener implements ActionListener
 {
 public void actionPerformed(ActionEvent event)
 {
 movieNameField.setText(null);
 movieLeadActorField.setText(null);
 movieYearField.setText(null);
 }
 }


 class ResetOutputListener implements ActionListener
 {
 public void actionPerformed(ActionEvent event)
 {
 output.setText(null);
 output.setText("\t" + "MovieName" + "\t\t" + "LeadActor" + "\t\t" + "Release Year" + "\n");
 }
 }

}

 

Movie

This class is a java bean to hold data for a movie.

public class Movie
{
 private String leadActor;
 private String year;
 private String name;

public Movie()
 {
 name="";
 leadActor="";
 year="";
 }

public Movie(String name, String lead, String year)
 {
 this.name = name;
 this.leadActor = lead;
 this.year= year;
 }

public String getName() {
 return name;
 }

public void setName(String name) {
 this.name = name;
 }

public String getLeadActor() {
 return leadActor;
 }

public void setLeadActor(String leadActor) {
 this.leadActor = leadActor;
 }

public String getYear() {
 return year;
 }

public void setYear(String year) {
 this.year = year;
 }

}

 

Exit mobile version