0

I'm currently working on a banking application project for university and the GUI (using swing here) consists of many different screens, having 2-8 buttons each, so in the end it comes down to let's say 50+ buttons which all have different functionalities. All the GUI's single components are outsourced to a different class which I call when the GUI is invoked by the main program.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.NumberFormatter;

import java.sql.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.io.*;

public class Gui extends JFrame {

    private JPanel contentPane = new JPanel();
    private static Database userData;
    private Components components;

    public Gui(Database userData) {
        components = new Components(this, userData);

        ActionListeners al = new ActionListeners(components, this, userData);

        for (int i = 0; i < components.accountModels.length; i++) {
            initializeSettings(components.accountModels[i]);
        }

        components.checkingAccSettings = readSettings("Checking Account");
        components.dayMoneyAccSettings = readSettings("Day Money Account");
        components.depositAccSettings = readSettings("Deposit Account");
        components.fixedDepositAccSettings = readSettings("Fixed Deposit Account");
        components.robberyAccSettings = readSettings("Robbery Account");

        components.loadLookAndFeel();
        this.userData = userData;
        setIconImage(Toolkit.getDefaultToolkit()
                .getImage(Gui.class.getResource("/de/magani/banking/sparkasse_logo_transparent.png")));

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        int screenWidth = (int) width;
        int screenHeight = (int) height;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
        setResizable(true);
        setMinimumSize(new Dimension(960, 608));
        getContentPane().setLayout(null);

        try {
            File file = new File("C:/Program Files/Sparbank/adminCred.sparbank");
            BufferedWriter out = new BufferedWriter(new FileWriter(file, true));
            BufferedReader in = new BufferedReader(new FileReader(file));
            String currentLine = null;
            if (file.exists() && ((currentLine = in.readLine()) != null)) {
                components.adminPassword = currentLine;
                in.close();
                out.close();
            } else {
                file.createNewFile();
                components.adminPassword = "123";
                out.write(components.adminPassword);
                in.close();
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // menuScreen
        components.btnDisplayBalance.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    displayBalanceScreen();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });

        components.btnWithdraw.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                withdrawScreen();
            }
        });

        components.btnDeposit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                depositScreen();
            }
        });

        components.btnTransfer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (components.currentAccount.equals("Robbery Account")) {
                    robberyScreen();
                } else {
                    transferScreen();
                }
            }
        });

Everything works fine to this point and the application is actually already completely done but the problem which I'm facing now is that I don't want my GUI's constructor filled up with about 600 lines of code for my ActionListeners, so I tried moving them to a different class, passing over the components and the GUI itself as parameters. But when launching the programm now none of the buttons works. I have been searching some websites but haven't really found an answer that seemed to help me out on this.

Any help on this would be very very much appreciated. If any code samples or anything else was needed to solve the problem feel free to tell me, I just didn't want to spam this post with to much unneccessary code.

Samaranth
  • 385
  • 3
  • 16
  • Not exactly answering your question: You could reduce your `addActionListener` code by using [Java 8 lambda expressions](http://www.codejava.net/java-core/the-java-language/java-8-lambda-listener-example). – Thomas Fritsch Jun 26 '17 at 00:17
  • Welcome to SO. There is no need for so much code to demonstrate the issue. Please post [mcve]. – c0der Jun 26 '17 at 03:20
  • Use `Action`, for [example](https://stackoverflow.com/a/37063037/230513), to encapsulate the functionality. – trashgod Jun 26 '17 at 11:48

1 Answers1

1

Your ActionListeners reference method in the GUI class, so you easiest refactoring would be to have each ActionListener in a nested class within GUI:

   class DepositeAL implements ActionListener{

        public void actionPerformed(ActionEvent arg0) {
            depositScreen();        
        }
    }

And use it : components.btnDeposit.addActionListener(new DepositeAL());

To refactor the action listener to a non-nested class, you'll need to pass a reference to GUI:

    class DepositeAL implements ActionListener{

        private Gui gui;
        DepositeAL(Gui gui){
            this.gui = gui;
        }
        public void actionPerformed(ActionEvent arg0) {
            gui.depositScreen();        
        }
    }

And use it : components.btnDeposit.addActionListener(new DepositeAL(this));

c0der
  • 18,467
  • 6
  • 33
  • 65