Screen transition with swing, java

Hello, this is a certain company engineer.

I made a task management web application with swing for studying. The screen transition at that time was unexpectedly troublesome, so I made a memorandum. Click here for related past articles ↓ https://qiita.com/Ohtak/items/1be20a4b06b5833c3d61

1. System structure

-MainFrame as parent, MainPanel as child, TaskRegisterPanel, TaskEditPanel -Panel creation, screen transition, reload management, etc. with only MainFrame -Just call each method of MainFrame from the child

2. Screen

スクリーンショット 2018-01-02 11.04.22.png スクリーンショット 2018-01-02 11.04.43.png スクリーンショット 2018-01-02 11.05.11.png

From the top are MainPanel, TaskRegisterPanel, and TaskEditPanel.

3. Code

It's finally the code. First from the parent mainFrame

MainFrame.java



public class MainFrame extends JFrame{
	public String[] PanelNames = {"main","register","edit"};//Specify the panel with this name
	
	//Create various panels
	MainPanel mainPanel = new MainPanel(this,PanelNames[0]);
    TaskRegisterPanel taskRegisterPanel = new TaskRegisterPanel(this,PanelNames[1]);
    TaskEditPanel taskEditPanel = new TaskEditPanel(this,PanelNames[2]);
    
    public MainFrame(){
        this.add(mainPanel);
        mainPanel.setVisible(true);
        
        this.add(taskRegisterPanel);
        taskRegisterPanel.setVisible(false);
        
        this.add(taskEditPanel);
        taskEditPanel.setVisible(false);
        
        this.setBounds(100, 100, 1200, 800);
    }
    
    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    }
    
    //For page redrawing
    //If the mainPanel does not do this, the changes will not be reflected in the display
    //For the time being, the other two panels ...
    public void reloadPage(String reloadPanelName){
    	if(reloadPanelName == PanelNames[0]){
    		this.remove(this.mainPanel);
    		MainPanel mainPanel = new MainPanel(this,PanelNames[0]);
    		this.add(mainPanel);
    	}
    	else if(reloadPanelName == PanelNames[1]){
    		this.remove(this.taskRegisterPanel);
    		TaskRegisterPanel taskRegisterPanel = new TaskRegisterPanel(this,PanelNames[1]);
    		this.add(taskRegisterPanel);
    	}
    	else if(reloadPanelName == PanelNames[2]){
    		this.remove(this.taskEditPanel);
    		TaskEditPanel taskEditPanel = new TaskEditPanel(this,PanelNames[2]);
    		this.add(taskEditPanel);
    	}
    }
    
    //Panel transition method
    //A method of creating all panels in advance and switching between display and non-display
    
    //Show main panel
    public void showMainPanel(JPanel nowPanel){
    	nowPanel.setVisible(false);
        mainPanel.setVisible(true);
    }
    
    //Display task registration panel
    public void showRegisterPanel(JPanel nowPanel){
    	nowPanel.setVisible(false);
        taskRegisterPanel.setVisible(true);
    }
    
    //Show task edit panel
    public void showEditPanel(JPanel nowPanel, TaskDto task){
    	nowPanel.setVisible(false);
        //Set the initial string
        taskEditPanel.setEditString(task);
        taskEditPanel.setVisible(true);
    }
}

Almost as commented. Specifying which panel to display

public String[] PanelNames = {"main","register","edit"};

I am doing it. I tried not to put public variables as much as possible, but only the panel specification from outside the class did not work. That's all left ...

Next, the access part to the MainFrame of other panels

MainPanel.java


public class MainPanel extends JPanel{
	
    MainFrame mainFrame;
    
    //panel creation
    MainPanel(MainFrame mf,String  name){
    	mainFrame = mf;
    	this.setName(name);

    	[Omission]

    	//Button layout
    	JPanel buttonPanel = new JPanel();
    	JButton createButton = new JButton("Create New");
    	createButton.setPreferredSize(new Dimension(100,50));
    	createButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			panelChangeToRegister(mainFrame.PanelNames[1]);
    		}
    	});
    	buttonPanel.add(createButton);

    	JButton editButton = new JButton("Edit task");
    	editButton.setPreferredSize(new Dimension(100,50));
    	editButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			//Set the selected element to dto when the button is pressed
    			TaskDto task = setTaskDtoFromSelectedString();
    			if(!isNotSelectedError){
    				panelChangeToEdit(mainFrame.PanelNames[2], task);
    			}
    		}
    	});
    	buttonPanel.add(editButton);
    }

    public void panelChangeToRegister(String toPanelName){
    	mainFrame.showRegisterPanel((JPanel)this);
    }
    
    public void panelChangeToEdit(String toPanelName, TaskDto task){
    	mainFrame.showEditPanel((JPanel)this, task);
    }
}

I pasted only the Main Panel, but the other two are also like this. By the way, when transitioning from the other two panels to the mainPanel, mainFrame.reloadPage is called to reset the display. Since I wanted to display the initial character string when transitioning to the edit screen, only panelChangeToEdit has an argument.

I also use Dto, but this is another article ...

4. Impressions

The parent-child structure of Frame and Panel was more troublesome than I imagined ... If you are not aware of the timing of new and add, you will be confused as to which variable is displayed. The following is an example of failure.

MainFrame_Failure example.java


    public void reloadPage(String panelName, JPanel jp){
    	
    	MainFrame mainFrame = new MainFrame();//I have new frame
    	mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	mainFrame.setVisible(true);

    	if(reloadPanelName == PanelNames[0]){
    		mainFrame.remove(jp);//I'm trying to remove from a new one instead of the one I made at the beginning.
    		MainPanel mainPanel = new MainPanel(this,PanelNames[0]);
    		mainFrame.add(mainPanel);//And I'm adding.
    	}
    	else if(reloadPanelName == PanelNames[1]){
    		mainFrame.remove(jp);
    		TaskRegisterPanel taskRegisterPanel = new TaskRegisterPanel(this,PanelNames[1]);
    		mainFrame.add(taskRegisterPanel);
    	}
    	else if(reloadPanelName == PanelNames[2]){
    		mainFrame.remove(jp);
    		TaskEditPanel taskEditPanel = new TaskEditPanel(this,PanelNames[2]);
    		mainFrame.add(taskEditPanel);
    	}
    }

What I wanted to do was

  1. Delete the panel added to the frame once
  2. New, add new However, in this example, a new frame was created in step 1 and deleted from there. Of course, it has not been deleted from the original frame. As a result, two identical panels are displayed overlapping. I don't understand the meaning of variable names from time to time ... The one pasted in [3. Code] is fixed,

I want to be careful because the timing of new can be applied not only to swing but also to other java applications.

Recommended Posts

Screen transition with swing, java
[Java Swing] Screen transition by CardLayout
Screen transition by Post method [Java]
Screen transition method
Screen transition memorandum
Screen transition by [Android Studio] [Java] button
Install java with Homebrew
Change seats with java
Install Java with Ansible
Comfortable download with JAVA
Switch java with direnv
Download Java with Ansible
Let's scrape with Java! !!
Build Java with Wercker
Java Swing, SWT, JavaFX
Endian conversion with JAVA
[Android / Java] Screen transition and return processing in fragments
[Android 9.0 Pie Java] LINE-style chat list screen → personal chat screen transition animation implemented (with sample application)
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java
Java multi-project creation with Gradle
Getting Started with Java Collection
Java Config with Spring MVC
Basic Authentication with Java 11 HttpClient
Let's experiment with Java inlining
Run batch with docker-compose with Java batch
[Template] MySQL connection with Java
[Swift] Simple screen transition summary
Rewrite Java try-catch with Optional
[Java] JSON communication with jackson
Java to play with Function
Try DB connection with Java
Enable Java EE with NetBeans 9
[Java] JavaConfig with Static InnerClass
Try gRPC with Java, Maven
Let's operate Excel with Java! !!
Android development ~ Screen transition (intent) ~
Version control Java with SDKMAN
RSA encryption / decryption with java 8
Paging PDF with Java + PDFBox.jar
Sort strings functionally with java
Object-oriented (java) with Strike Gundam
[Note] Scene transition with JavaFX
[Java] Content acquisition with HttpCliient
Java version control with jenv
Troubleshooting with Java Flight Recorder
Streamline Java testing with Spock
Connect to MySQL 8 with Java
Error when playing with java
Using Mapper with Java (Spring)
Java study memo 2 with Progate
Getting Started with Java Basics
Seasonal display with Java switch
Screen transition Information transfer memorandum
Use SpatiaLite with Java / JDBC
Study Java with Progate Note 1
Compare Java 8 Optional with Swift
HTML parsing with JAVA (scraping)
Java unit tests with Mockito
[Swift] Implement screen transition and pass by value with Segue and present
I want to return to the previous screen with kotlin and java!