Is it possible to override the implementation inside the generated actionPerformed in java?

74 Views Asked by At

I'm using a NetBeans as my IDE for Java. I have class called NewSchoolYearDialog extended by JDialog:

Add School Year

The purpose of that Save button in the image above is to insert a school year into a database.

Now I want to reuse that design to create another JDialog to edit the school year by just creating an object of a class NewSchoolYearDialog, set the title to "Edit School Year", and change the Save button implementation to update the school year.

Should I create another class extended by JDialog and just copy the design, or there is another way to do it?

2

There are 2 best solutions below

2
amir rad On

As a rule of clean code, it is not clean to copy your code, and based on your issue, the difference between insert and update is just having id or not. you can check id exists or not and then call update or insert.

0
MadProgrammer On

Decouple your concerns.

Consider the fact that the editor should do one job, allow the user to input the data. It's not it's responsibility to save it.

Instead, create a "editor" which simply does the core functionality of managing the input (ie making sure the "from year" is always earlier to the "to year"). Then provide some getters to get the values from the editor.

You should then make use of dependency injection to pass the values into the editor.

A simple solution would then be to present the editor in a JOptionPane. Then based on the response, either save the results ... or don't.

import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                YearEditorPane editorPane = new YearEditorPane(2023, 2024);
                int option = JOptionPane.showOptionDialog( 
                        null,
                        editorPane,
                        "Add Shcool Year",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        new String[] {"Save", "Cancel"},
                        "Save"
                );
                if (option == 0) {
                    int fromYear = editorPane.getFromYear();
                    int toYear = editorPane.getToYear();
                    System.out.println("Save the data - " + fromYear + " - " + toYear);
                }
            }
        });
    }

    public class YearEditorPane extends JPanel {

        private JSpinner fromYearSpinner;
        private JSpinner toYearSpinner;

        public YearEditorPane(int fromYear, int toYear) {
            add(new JLabel("School Year:"));

            fromYearSpinner = new JSpinner(
                    new SpinnerNumberModel(
                            fromYear, 
                            fromYear, 
                            fromYear + 100, 
                            1
                    ));
            toYearSpinner = new JSpinner(
                    new SpinnerNumberModel(
                            toYear, 
                            toYear, 
                            toYear + 100, 
                            1
                    ));

            add(fromYearSpinner);
            add(toYearSpinner);
        }

        public int getFromYear() {
            return (int)fromYearSpinner.getValue();
        }

        public int getToYear() {
            return (int)toYearSpinner.getValue();
        }        
    }
}