Switching Between Frames

Question:
I’m building an application that has several different frames. Eachhas a button that when pressed should bring up a new frame and closethe calling frame. How can I display the new frame and closethe calling frame?

Answer:
Creating a new frame is as simple as following the same steps youfollowed when creating the original frame. Closing the callingframe requires a little bit of care. You should make sure you createthe new frame before closing and disposing of the old frame. Oneway of doing this is to hide the old frame, create and display thenew frame, and then dispose of the old frame.

The following code demonstrates how to do this. It creates a frame with anexit button and a button for creating new frames. It registers anActionListener with the exit button to exit the entire application.The ActionListener it registers with the new frame button takes careof hiding the old frame, creating the new frame, and disposing ofthe old frame. Keep in mind that an alternative to creating a newframe is to reuse the old frame, simply replacing its contents witha new component panel.

import java.awt.*;import java.awt.event.*;public class Frames {  public static class NewFrame extends Frame {    int _id;    Button _exitButton, _newFrameButton;    public NewFrame(int id) {      _id = id;      _exitButton     = new Button("Exit");      _newFrameButton = new Button("New Frame");      addWindowListener(new WindowAdapter() {          public void windowClosing(WindowEvent e) {            Window window = e.getWindow();            window.setVisible(false);            window.dispose();            System.exit(0);          }        });      _exitButton.addActionListener(new ActionListener() {          public void actionPerformed(ActionEvent e) {            setVisible(false);            dispose();            System.exit(0);          }        });      _newFrameButton.addActionListener(new ActionListener() {          public void actionPerformed(ActionEvent e) {            Frame frame;            setVisible(false);            frame = new NewFrame(_id + 1);            frame.pack();            frame.setVisible(true);            dispose();          }        });      setLayout(new FlowLayout());      add(_exitButton);      add(_newFrameButton);      setTitle("Frame #" + id);    }  }  public static final void main(String[] args) {    NewFrame frame;    frame = new NewFrame(0);    frame.pack();    frame.setVisible(true);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.