Story of making a task management application with swing, java

Hello, this is a certain company engineer. I'm studying because I think it's bad that I can only understand object-oriented programming vaguely even though I'm an engineer. As part of that, I decided to understand object-oriented writing while creating a task management app. I will leave the process as a memorandum.

For the time being, the specifications are like this. -Personal desktop application (I don't want to log in or anything else) ・ The basic UI respects trello https://trello.com/ -Use SQLite to record registered tasks

So I made the top page. スクリーンショット 2017-09-13 9.59.04.png

It works with a code like this. (Before refactoring.)

public class Top extends JFrame{
        /**
        *Launch application
        */
        public static void main(String[] args) {
        	Top frame = new Top("Task Manager");
            frame.setVisible(true);
        }
        
        Top(String title){
        	setTitle(title);
            setBounds(100, 100, 1000, 800);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            setLayout(new FlowLayout());

            //TODO task display area
            //Task display area layout
            JPanel TodoPanel = new JPanel();
            TodoPanel.setPreferredSize(new Dimension(300, 600));
            TodoPanel.setBackground(Color.WHITE);
            TodoPanel.setLayout(new BoxLayout(TodoPanel, BoxLayout.X_AXIS));
            
            //Panel for placing titles
            JPanel TodoTitlePanel = new JPanel();
            TodoTitlePanel.setPreferredSize(new Dimension(300, 60));
            TodoTitlePanel.setBackground(Color.WHITE);
            TodoTitlePanel.setLayout(new BoxLayout(TodoTitlePanel, BoxLayout.Y_AXIS));
            TodoTitlePanel.setAlignmentY(0.0f);
            TodoPanel.add(TodoTitlePanel);
            
            //Title placement
            JLabel label = new JLabel("TODO");
            label.setFont(new Font("MS gothic", Font.BOLD, 32));
            label.setAlignmentX(0.5f);
            TodoTitlePanel.add(label);
            
            //Frame border
            LineBorder border = new LineBorder(Color.BLACK, 2, true);
            TodoPanel.setBorder(border);
            
            //DOING task display area
            //Task display area layout
            JPanel DoingPanel = new JPanel();
            DoingPanel.setPreferredSize(new Dimension(300, 600));
            DoingPanel.setBackground(Color.WHITE);
            DoingPanel.setLayout(new BoxLayout(DoingPanel, BoxLayout.X_AXIS));
            
            //Panel for placing titles
            JPanel DoingTitlePanel = new JPanel();
            DoingTitlePanel.setPreferredSize(new Dimension(300, 60));
            DoingTitlePanel.setBackground(Color.WHITE);
            DoingTitlePanel.setLayout(new BoxLayout(DoingTitlePanel, BoxLayout.Y_AXIS));
            DoingTitlePanel.setAlignmentY(0.0f);
            DoingPanel.add(DoingTitlePanel);
            
            //Title placement
            JLabel doingLabel = new JLabel("DOING");
            doingLabel.setFont(new Font("MS gothic", Font.BOLD, 32));
            doingLabel.setAlignmentX(0.5f);
            DoingTitlePanel.add(doingLabel);
            
            //Frame border
            LineBorder border2 = new LineBorder(Color.BLACK, 2, true);
            DoingPanel.setBorder(border2);
            
            //DONE task display area
            //Task display area layout
            JPanel DonePanel = new JPanel();
            DonePanel.setPreferredSize(new Dimension(300, 600));
            DonePanel.setBackground(Color.WHITE);
            DonePanel.setLayout(new BoxLayout(DonePanel, BoxLayout.X_AXIS));
            
            //Panel for placing titles
            JPanel DoneTitlePanel = new JPanel();
            DoneTitlePanel.setPreferredSize(new Dimension(300, 60));
            DoneTitlePanel.setBackground(Color.WHITE);
            DoneTitlePanel.setLayout(new BoxLayout(DoneTitlePanel, BoxLayout.Y_AXIS));
            DoneTitlePanel.setAlignmentY(0.0f);
            DonePanel.add(DoneTitlePanel);
            
            //Frame border
            LineBorder border3 = new LineBorder(Color.BLACK, 2, true);
            DonePanel.setBorder(border3);
            
            //Title placement
            JLabel doneLabel = new JLabel("DONE");
            doneLabel.setFont(new Font("MS gothic", Font.BOLD, 32));
            doneLabel.setAlignmentX(0.5f);
            DoneTitlePanel.add(doneLabel);
            
            //Button layout
            JPanel buttonPanel = new JPanel();
            
            JButton createButton = new JButton("Create New");
            createButton.setPreferredSize(new Dimension(100,50));
            buttonPanel.add(createButton);
            
            JButton editButton = new JButton("Edit task");
            editButton.setPreferredSize(new Dimension(100,50));
            buttonPanel.add(editButton);

            Container contentPane = getContentPane();
            contentPane.add(TodoPanel);
            contentPane.add(DoingPanel);
            contentPane.add(DonePanel);
            contentPane.add(buttonPanel);
          }
} 

It's long. There are three places where the same processing is done. When making labels and panels with swing, it is troublesome because you have to new each time. So, the code below summarizes the duplicated processing.

public class Top extends JFrame{
        /**
        *Launch application
        */
        public static void main(String[] args) {
        	Top frame = new Top("Task Manager");
            frame.setVisible(true);
        }
        
        Top(String title){
        	setTitle(title);
            setBounds(100, 100, 1000, 800);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            setLayout(new FlowLayout());

            //TODO task display
            makeTaskPanel("TODO");
            
            //DOING task display
            makeTaskPanel("DOING");
            
            //DONE task display area
            makeTaskPanel("DONE");
            
            //Button layout
            JPanel buttonPanel = new JPanel();
            TaskRegister taskRegisterPanel = new TaskRegister();
            this.add(taskRegisterPanel);
            taskRegisterPanel.setVisible(false);
            JButton createButton = new JButton("Create New");
            
            buttonPanel.add(createButton);
            
            JButton editButton = new JButton("Edit task");
            editButton.setPreferredSize(new Dimension(100,50));
            buttonPanel.add(editButton);

            Container contentPane = getContentPane();
            contentPane.add(buttonPanel);
          }
        
        public void makeTaskPanel(String panelTitle){
        	//Create a task panel with the title text specified in title
            JPanel mainPanel = new JPanel();
            JPanel titlePanel = new JPanel();
            JLabel titleLabel = new JLabel(panelTitle);
            LineBorder border = new LineBorder(Color.BLACK, 2, true);
            
        	//Task display area layout
            mainPanel.setPreferredSize(new Dimension(300, 600));
            mainPanel.setBackground(Color.WHITE);
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
            
            //Panel for placing titles
            titlePanel.setPreferredSize(new Dimension(300, 60));
            titlePanel.setBackground(Color.WHITE);
            titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
            titlePanel.setAlignmentY(0.0f);
            mainPanel.add(titlePanel);
            
            //Title placement
            titleLabel.setFont(new Font("MS gothic", Font.BOLD, 32));
            titleLabel.setAlignmentX(0.5f);
            titlePanel.add(titleLabel);
            
            //Frame border
            mainPanel.setBorder(border);
            
            Container contentPane = getContentPane();
            contentPane.add(mainPanel);
        }
} 

What was duplicated

  1. New necessary panel, label, etc.
  2. Arrange by playing around with the size Since it was a part to do, I cut out that part into a method. In object-oriented programming, cohesion is strong and coupling is low </ strong>, the better the design.

Does that mean that duplicate functions should be grouped together as much as possible so that the caller can easily call them? So I will continue to do my best.

Recommended Posts

Story of making a task management application with swing, java
The story of making a game launcher with automatic loading function [Java]
The story of making a reverse proxy with ProxyServlet
The story of making dto, dao-like with java, sqlite
A story about hitting the League Of Legends API with JAVA
The story of making ordinary Othello in Java
Java / Twitter clone / task management system (1) Create a database
A story about developing ROS called rosjava with java
A story about making catkin_make of rosjava compatible offline
I tried to modernize a Java EE application with OpenShift.
A story packed with the basics of Spring Boot (solved)
Check the operation of two roles with a chat application
[Java] Simplify the implementation of data history management with Reladomo
Implementation of a math parser with recursive descent parsing (Java)
A story addicted to toString () of Interface proxied with JdkDynamicAopProxy
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
Try developing a containerized Java web application with Eclipse + Codewind
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
A story stuck with NotSerializableException
Screen transition with swing, java
Task management application creation procedure
The story of the first Rails app refactored with a self-made helper
A story that I struggled to challenge a competition professional with Java
Comparison of WEB application development with Rails and Java Servlet + JSP
Let's create a TODO application in Java 4 Implementation of posting function
Let's make a book management web application with Spring Boot part1
I tried running a letter of credit transaction application with Corda 1
[Note] A story about changing Java build tools with VS Code
Let's make a book management web application with Spring Boot part3
Let's create a TODO application in Java 6 Implementation of search function
A story of connecting to a CentOS 8 server with an old Ansible
Let's create a TODO application in Java 8 Implementation of editing function
Let's make a book management web application with Spring Boot part2
Memorandum No.2 "Making a search history with ArrayList and HashSet" [Java]
A story about having a hard time aligning a testing framework with Java 6
A story that struggled with the introduction of Web Apple Pay
Let's create a TODO application in Java 1 Brief explanation of MVC
The story of making a communication type Othello game using Scala.
Let's create a TODO application in Java 5 Switch the display of TODO
Think of a Java update strategy
Deploy a Docker application with Greengrass
Build a Java project with Gradle
[Java version] The story of serialization
I made a GUI with Swing
A really scary (Java anti-pattern) story
Story of passing Java Gold SE8
Sort a List of Java objects
Build a web application with Javalin
Access Teradata from a Java application
Impressions of making BlackJack-cli with Ruby
A brief description of JAVA dependencies
Run an application made with Java8 with Java6
The story of making it possible to build a project that was built by Maven with Ant
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
A record of setting up a Java development environment with Visual Studio Code
The story of forgetting to close a file in Java and failing
Let's express the result of analyzing Java bytecode with a class diagram
Creating a java web application development environment with docker for mac part1
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
Volume of trying to create a Java Web application on Windows Server 2016