Hi, this is my first post on a Java forum, so please go easy ! (I`m sure you won`t bite !)
OK, I`m learning Java on an Open University Course. I`ve come to a point where I want to "deviate" from the course a little, to test and expand upon what I`ve learned. As usual, I`m running (well, jogging) a little before I can walk properly, but that`s the way I am !
I`m trying to write a Lottery Syndicate Management program for a friend who runs the syndicate that I am a member of. It`s a GUI standalone application that stores and maintains sysndicate member`s details and monitors payments from members, payouts to members and checks our lottery entries against each week`s lottery draw. So far, so good(ish). I have now stumbled upon a programming issue that I`m stuck on.
I have a JPanel with several buttons to invoke different functions for Syndicate Members - Add, Delete, Edit, Payment, Payout, OK and Cancel. Next to these buttons, in the same JPanel, I`m displaying the currently selected member (if there is one). If I click on "Add", the "add" method is called to carry out the procedure to enter a new syndicate member`s details, then add this member to the current syndicate. Depending on current "mode", I enable or disable appropriate buttons. In "add" mode, I disable Edit, Payment etc buttons, but enable OK and Cancel. When the member has been added, the add method finishes by creating a new member and enabling the "Add, Delete Edit etc buttons, ready for the next user request.
Here`s the question !
The "Cancel" and "Ok" buttons need to execute different code/methods, depending upon the current procedure being carried out. If a member is being added, the OK button should check the entered details and create a new member. If a payment is being input for a current member, the OK button will simply invoke code to enter the payment into the member`s details. I`m using ActionListener to detect mouse clicks on enabled JButtons, then call the appropriate method, as below.
If the program is executing "add()", and the user clicks the Cancel button, I need invoke specific code to cancel the "add" operation. However, if the user is using "Payment", the same cancel button will need to invoke different code.public MemberPanel() { //initComponents(); super(); jButton1.addActionListener(this); jButton2.addActionListener(this); jButton3.addActionListener(this); jButton4.addActionListener(this); jButton5.addActionListener(this); jButton6.addActionListener(this); jButton7.addActionListener(this); jButton8.addActionListener(this); jButton9.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == jButton1) { this.add(); } if (source == jButton2) { this.payment() } if (source == jButton6) { this.ok(); } if (source == jButton7) { this.cancel(); } }
Any suggestion on what is the best way to achieve what I`m trying to do ?
I`ve thought about creating seperate, specific JPanels for each mode of operation, but that seems like a lot of work, and is probably inefficient.
Could it be that I need to create several "Cancel" buttons, and utilise a specific button for each mode of operation ? What I`ve learned about Java so far, is that it`s more powerfull, yet complex than I thought it would be, and that there are often "clever" ways of achieving certain programming goals.
Oh, and studying Java is interesting, but it sometimes makes my brain hurt.