Easy to implement interface jump in Java

Source: Internet
Author: User

Easy to implement interface jump in Java

Brother Lian Easy to implement interface jump in java, easy to implement interface jump in Java

Suppose such a situation , our system interface uses the Javax.swing package constructs, The interface Foundation is baseview, He is a container, certainly he should provide the function which obtains the control element, for example obtains the button, the Drop-down box, the table and so on, of course is only a container, And the elements of our interface are all deployed on jpanel.

described as :

an interface is a baseview, he contains only one jpanel, this contains jpanel contains all of our swing controls, such as jbutton,jlable and so On.

the problem arises : We usually complete an interface for business needs to automatically jump to the next interface, complete the next interface and can jump back (digression: because our operations are based on the gui, it is often possible to save the session information, and the Web can not do), And this tends to be a factor in the inefficiency of the system implementation, and I've seen my current system where someone uses 600 lines of code to determine which of the previous interface should be to jump over, because many interfaces can jump to the current interface.

of course, There is a way to include a variable in the next interface class that points to the previous interface, which, we say, is inconvenient and increases dependency, which is bad for software.

next , I'm going to give my solution, hoping to help a friend with this interface structure.

(all of the following are described in simplified models.)

1. simply, Let's assume that Baseview inherits jwindow, which can of course be other containers (depending on your implementation), presumably like This:

Public abstract class Baseview extends jwindow{


(implement Some methods of getting interface controls, and interface information).


2. Each interface class is defined like This:

public class MyView extends baseview{

JPanel mypanel;

public void Playoutpanel () {

JButton MyButton = new JButton ("ok");

Mypanel.add (myButton);


(add the controls and layouts you need to the Mypanel)



3. Assume that there are other interface Oneview,twoview,threeview after processing the operation will need to jump to myview, in the MyView OK button pressed back to the original interface.

the original bloated code needs to add a variable Baseview anyview in the myview, which is used to store the transferred interface anyview, which is assigned in the jump code of the three references MyView to set. The jump code looks like this:

public void Jump () {

MyView MyView = new MyView ();

Myview.anyview = this;

This.remove (this.xxpanel);

This.add (myview.getpanel ());

This.repaint ();


It looks good , although it is necessary to refer to the MyView class and call his variables and methods. but jumping back is not so easy, or how to use 600 lines!

probably like This:(it's already been simplified by Me)

public void GoBack () {

If (anyview instanceof OneView) {

Anyview.remove (this.myview);

OneView ov = (OneView) anyview;

Anyview.add (ov.getpanel ());

Anyview.repaint ();


If (anyview instanceof Twoview) {





This compilation dependency is not a good thing without the use of a lot of other business use case interfaces , not to mention a lot of inefficient instanceof judgments and transformational operations.

to optimize this situation and solve this problem completely, I think we should design a Third-party class to eliminate this dependency, and let the interface jump not so Hard. This Third-party class is designed like This:

in this class , you must have a variable to hold the path of an interface jump, such as A-b-c. once the path is saved, you have the right to control the display of any interface. In this chain, the first position of the interface should be the first stop of this jump, the last position is the current Station. There is a causal relationship: you can jump back only if you jump. This allows us to save the path with an array. In reality, The jump should not be more than 10 times, so we set the path length to 10 (of course you can change as needed). This class looks like this:

Class viewpath{

jpanel[] Pnlpath = null; Jump to the interface path, the interface jumps to the maximum of 10 levels bar!!!

int index = 0; The current subscript in the path

Baseview Bsview = null; The same view where the current path is located

How to find a target in a path

public int Find (JPanel pnl) {//there is a panel under this path, some return subscript, no return-1

If (bsview==null) return-1; There is no initialization, there is no panel under this path

For (int I=0;i if (pnl==pnlpath[i]) {

index = i;

Return i; Returns the position if found, and sets the current position to the target location



return-1; Not found, return-1


constructor function

ViewPath (JPanel Mypanel,baseview MyView) {

Pnlpath = new JPanel; Set path maximum length is 10

Bsview = myView; Set the view that the path belongs to

pnlpath[0] = mypanel; Set up a starting station

index = 0; Set up a starting station index



Easy to implement interface jump in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.