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.