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
-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
From the top are MainPanel, TaskRegisterPanel, and TaskEditPanel.
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 ...
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
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