Linking GUI to Logic
up vote
1
down vote
favorite
I am relatively new to Java and am struggling to make the GUI work for a
simplified bank application logic. Besides the obvious Customer and Account
classes I have a BankLogic class that contains methods for all the
operations (deposit, creation of new accounts, customers etc), which in most
cases take in a persNumber parameter.
When it comes to the GUI, I have a menu with menu items for all the operations.
What I try to do for most of these items is:
- supply the persNr in a frame with text field.
- if the persNumber corresponds to an existing customer, then it's moving on
to doing the logic operation and displaying the relevant stuff. Display
comes in various types of frames that I create separate classes for.
Example of how this looks like:
public abstract class InputUI extends JFrame
{
private static final int FRAME_WIDTH = 450;
private static final int FRAME_HEIGHT = 100;
private JLabel persNrLabel;
private JTextField persNrInput;
private JButton button;
public InputUI()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
createTextField();
createButton();
createPanel();
pack();
}
public void createTextField() {
persNrLabel = new JLabel("Input personal number: ");
final int FIELD_WIDTH = 10;
persNrInput = new JTextField(FIELD_WIDTH);
}
public String getFieldText() {
String x = persNrInput.getText();
return x;
}
public void createButton() {
class PersNoListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
try
{
if (foundPersNr() == true)
{
performAction();
}
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Unknown error", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
}
button = new JButton("OK");
ActionListener listener = new PersNoListener();
button.addActionListener(listener);
}
public abstract void performAction();
public boolean foundPersNr() {
boolean found = false;
BankLogicInit referenced below is a class that only contains a BankLogic protected static variable (logic) - to get access to the data for the GUI, not sure this is the right approach.
for (String searchPNr : BankLogicInit.logic.getAllCustomers())
{
if (searchPNr.contains(getFieldText()))
{
found = true;
} else
{
JOptionPane.showMessageDialog(null, "Customer does not exist!", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
return found;
}
public void createPanel() {
JPanel panel = new JPanel();
panel.add(persNrLabel);
panel.add(persNrInput);
panel.add(button);
add(panel);
}
}
Then extending this class for operations that require input of persNumber by overriding the performAction() method:
public class CreateSavAccUI extends InputUI
{
public void performAction()
{
String persNoInput=super.getFieldText();
A. this doesn't work, but I need it to work - should return int:
BankLogicInit.logic.createSavingsAccount(persNoInput);
B. providing a value for persNoInput method works properly
BankLogicInit.logic.createSavingsAccount("9000000");
Then finally, displaying the appropriate view:
SavingsAccUI=new SavingsAccUI();
I can't make the connection why B works but not A. What would be a good
solution to make A work with as little structural alterations as possible? If the answer is related to MVC, it's still unknown to me, so all guidance
and suggestions for structure would be highly appreciated. Much thanks in advance!
java swing user-interface logic
add a comment |
up vote
1
down vote
favorite
I am relatively new to Java and am struggling to make the GUI work for a
simplified bank application logic. Besides the obvious Customer and Account
classes I have a BankLogic class that contains methods for all the
operations (deposit, creation of new accounts, customers etc), which in most
cases take in a persNumber parameter.
When it comes to the GUI, I have a menu with menu items for all the operations.
What I try to do for most of these items is:
- supply the persNr in a frame with text field.
- if the persNumber corresponds to an existing customer, then it's moving on
to doing the logic operation and displaying the relevant stuff. Display
comes in various types of frames that I create separate classes for.
Example of how this looks like:
public abstract class InputUI extends JFrame
{
private static final int FRAME_WIDTH = 450;
private static final int FRAME_HEIGHT = 100;
private JLabel persNrLabel;
private JTextField persNrInput;
private JButton button;
public InputUI()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
createTextField();
createButton();
createPanel();
pack();
}
public void createTextField() {
persNrLabel = new JLabel("Input personal number: ");
final int FIELD_WIDTH = 10;
persNrInput = new JTextField(FIELD_WIDTH);
}
public String getFieldText() {
String x = persNrInput.getText();
return x;
}
public void createButton() {
class PersNoListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
try
{
if (foundPersNr() == true)
{
performAction();
}
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Unknown error", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
}
button = new JButton("OK");
ActionListener listener = new PersNoListener();
button.addActionListener(listener);
}
public abstract void performAction();
public boolean foundPersNr() {
boolean found = false;
BankLogicInit referenced below is a class that only contains a BankLogic protected static variable (logic) - to get access to the data for the GUI, not sure this is the right approach.
for (String searchPNr : BankLogicInit.logic.getAllCustomers())
{
if (searchPNr.contains(getFieldText()))
{
found = true;
} else
{
JOptionPane.showMessageDialog(null, "Customer does not exist!", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
return found;
}
public void createPanel() {
JPanel panel = new JPanel();
panel.add(persNrLabel);
panel.add(persNrInput);
panel.add(button);
add(panel);
}
}
Then extending this class for operations that require input of persNumber by overriding the performAction() method:
public class CreateSavAccUI extends InputUI
{
public void performAction()
{
String persNoInput=super.getFieldText();
A. this doesn't work, but I need it to work - should return int:
BankLogicInit.logic.createSavingsAccount(persNoInput);
B. providing a value for persNoInput method works properly
BankLogicInit.logic.createSavingsAccount("9000000");
Then finally, displaying the appropriate view:
SavingsAccUI=new SavingsAccUI();
I can't make the connection why B works but not A. What would be a good
solution to make A work with as little structural alterations as possible? If the answer is related to MVC, it's still unknown to me, so all guidance
and suggestions for structure would be highly appreciated. Much thanks in advance!
java swing user-interface logic
what error if any? and why aren't you showing the code tologic.createSavingsAccount()
method?
– T McKeown
Nov 19 at 21:18
The problem is that I can't get the call logic.createSavingsAccount(persNr) to return anything. The method should return int. Code in BankLogic works properly when separated from GUI.
– J.Dow
Nov 19 at 21:24
We can't help if we can't see the code.....
– T McKeown
Nov 19 at 21:25
It's a general problem, the logic.methods(persNr) don't make the proper return if they take a "persNr" reference as parameter. They work if parameters are values of existing customers personal numers (e.g."900205"). Sorry, I can't explain better than this. My first guess is that I need to instantiate BankLogic some ther way in the GUI. What do you think?
– J.Dow
Nov 19 at 21:35
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am relatively new to Java and am struggling to make the GUI work for a
simplified bank application logic. Besides the obvious Customer and Account
classes I have a BankLogic class that contains methods for all the
operations (deposit, creation of new accounts, customers etc), which in most
cases take in a persNumber parameter.
When it comes to the GUI, I have a menu with menu items for all the operations.
What I try to do for most of these items is:
- supply the persNr in a frame with text field.
- if the persNumber corresponds to an existing customer, then it's moving on
to doing the logic operation and displaying the relevant stuff. Display
comes in various types of frames that I create separate classes for.
Example of how this looks like:
public abstract class InputUI extends JFrame
{
private static final int FRAME_WIDTH = 450;
private static final int FRAME_HEIGHT = 100;
private JLabel persNrLabel;
private JTextField persNrInput;
private JButton button;
public InputUI()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
createTextField();
createButton();
createPanel();
pack();
}
public void createTextField() {
persNrLabel = new JLabel("Input personal number: ");
final int FIELD_WIDTH = 10;
persNrInput = new JTextField(FIELD_WIDTH);
}
public String getFieldText() {
String x = persNrInput.getText();
return x;
}
public void createButton() {
class PersNoListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
try
{
if (foundPersNr() == true)
{
performAction();
}
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Unknown error", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
}
button = new JButton("OK");
ActionListener listener = new PersNoListener();
button.addActionListener(listener);
}
public abstract void performAction();
public boolean foundPersNr() {
boolean found = false;
BankLogicInit referenced below is a class that only contains a BankLogic protected static variable (logic) - to get access to the data for the GUI, not sure this is the right approach.
for (String searchPNr : BankLogicInit.logic.getAllCustomers())
{
if (searchPNr.contains(getFieldText()))
{
found = true;
} else
{
JOptionPane.showMessageDialog(null, "Customer does not exist!", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
return found;
}
public void createPanel() {
JPanel panel = new JPanel();
panel.add(persNrLabel);
panel.add(persNrInput);
panel.add(button);
add(panel);
}
}
Then extending this class for operations that require input of persNumber by overriding the performAction() method:
public class CreateSavAccUI extends InputUI
{
public void performAction()
{
String persNoInput=super.getFieldText();
A. this doesn't work, but I need it to work - should return int:
BankLogicInit.logic.createSavingsAccount(persNoInput);
B. providing a value for persNoInput method works properly
BankLogicInit.logic.createSavingsAccount("9000000");
Then finally, displaying the appropriate view:
SavingsAccUI=new SavingsAccUI();
I can't make the connection why B works but not A. What would be a good
solution to make A work with as little structural alterations as possible? If the answer is related to MVC, it's still unknown to me, so all guidance
and suggestions for structure would be highly appreciated. Much thanks in advance!
java swing user-interface logic
I am relatively new to Java and am struggling to make the GUI work for a
simplified bank application logic. Besides the obvious Customer and Account
classes I have a BankLogic class that contains methods for all the
operations (deposit, creation of new accounts, customers etc), which in most
cases take in a persNumber parameter.
When it comes to the GUI, I have a menu with menu items for all the operations.
What I try to do for most of these items is:
- supply the persNr in a frame with text field.
- if the persNumber corresponds to an existing customer, then it's moving on
to doing the logic operation and displaying the relevant stuff. Display
comes in various types of frames that I create separate classes for.
Example of how this looks like:
public abstract class InputUI extends JFrame
{
private static final int FRAME_WIDTH = 450;
private static final int FRAME_HEIGHT = 100;
private JLabel persNrLabel;
private JTextField persNrInput;
private JButton button;
public InputUI()
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setVisible(true);
createTextField();
createButton();
createPanel();
pack();
}
public void createTextField() {
persNrLabel = new JLabel("Input personal number: ");
final int FIELD_WIDTH = 10;
persNrInput = new JTextField(FIELD_WIDTH);
}
public String getFieldText() {
String x = persNrInput.getText();
return x;
}
public void createButton() {
class PersNoListener implements ActionListener
{
public void actionPerformed(ActionEvent event) {
try
{
if (foundPersNr() == true)
{
performAction();
}
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Unknown error", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
}
button = new JButton("OK");
ActionListener listener = new PersNoListener();
button.addActionListener(listener);
}
public abstract void performAction();
public boolean foundPersNr() {
boolean found = false;
BankLogicInit referenced below is a class that only contains a BankLogic protected static variable (logic) - to get access to the data for the GUI, not sure this is the right approach.
for (String searchPNr : BankLogicInit.logic.getAllCustomers())
{
if (searchPNr.contains(getFieldText()))
{
found = true;
} else
{
JOptionPane.showMessageDialog(null, "Customer does not exist!", "Message", JOptionPane.PLAIN_MESSAGE);
}
}
return found;
}
public void createPanel() {
JPanel panel = new JPanel();
panel.add(persNrLabel);
panel.add(persNrInput);
panel.add(button);
add(panel);
}
}
Then extending this class for operations that require input of persNumber by overriding the performAction() method:
public class CreateSavAccUI extends InputUI
{
public void performAction()
{
String persNoInput=super.getFieldText();
A. this doesn't work, but I need it to work - should return int:
BankLogicInit.logic.createSavingsAccount(persNoInput);
B. providing a value for persNoInput method works properly
BankLogicInit.logic.createSavingsAccount("9000000");
Then finally, displaying the appropriate view:
SavingsAccUI=new SavingsAccUI();
I can't make the connection why B works but not A. What would be a good
solution to make A work with as little structural alterations as possible? If the answer is related to MVC, it's still unknown to me, so all guidance
and suggestions for structure would be highly appreciated. Much thanks in advance!
java swing user-interface logic
java swing user-interface logic
edited Nov 19 at 21:59
asked Nov 19 at 21:13
J.Dow
62
62
what error if any? and why aren't you showing the code tologic.createSavingsAccount()
method?
– T McKeown
Nov 19 at 21:18
The problem is that I can't get the call logic.createSavingsAccount(persNr) to return anything. The method should return int. Code in BankLogic works properly when separated from GUI.
– J.Dow
Nov 19 at 21:24
We can't help if we can't see the code.....
– T McKeown
Nov 19 at 21:25
It's a general problem, the logic.methods(persNr) don't make the proper return if they take a "persNr" reference as parameter. They work if parameters are values of existing customers personal numers (e.g."900205"). Sorry, I can't explain better than this. My first guess is that I need to instantiate BankLogic some ther way in the GUI. What do you think?
– J.Dow
Nov 19 at 21:35
add a comment |
what error if any? and why aren't you showing the code tologic.createSavingsAccount()
method?
– T McKeown
Nov 19 at 21:18
The problem is that I can't get the call logic.createSavingsAccount(persNr) to return anything. The method should return int. Code in BankLogic works properly when separated from GUI.
– J.Dow
Nov 19 at 21:24
We can't help if we can't see the code.....
– T McKeown
Nov 19 at 21:25
It's a general problem, the logic.methods(persNr) don't make the proper return if they take a "persNr" reference as parameter. They work if parameters are values of existing customers personal numers (e.g."900205"). Sorry, I can't explain better than this. My first guess is that I need to instantiate BankLogic some ther way in the GUI. What do you think?
– J.Dow
Nov 19 at 21:35
what error if any? and why aren't you showing the code to
logic.createSavingsAccount()
method?– T McKeown
Nov 19 at 21:18
what error if any? and why aren't you showing the code to
logic.createSavingsAccount()
method?– T McKeown
Nov 19 at 21:18
The problem is that I can't get the call logic.createSavingsAccount(persNr) to return anything. The method should return int. Code in BankLogic works properly when separated from GUI.
– J.Dow
Nov 19 at 21:24
The problem is that I can't get the call logic.createSavingsAccount(persNr) to return anything. The method should return int. Code in BankLogic works properly when separated from GUI.
– J.Dow
Nov 19 at 21:24
We can't help if we can't see the code.....
– T McKeown
Nov 19 at 21:25
We can't help if we can't see the code.....
– T McKeown
Nov 19 at 21:25
It's a general problem, the logic.methods(persNr) don't make the proper return if they take a "persNr" reference as parameter. They work if parameters are values of existing customers personal numers (e.g."900205"). Sorry, I can't explain better than this. My first guess is that I need to instantiate BankLogic some ther way in the GUI. What do you think?
– J.Dow
Nov 19 at 21:35
It's a general problem, the logic.methods(persNr) don't make the proper return if they take a "persNr" reference as parameter. They work if parameters are values of existing customers personal numers (e.g."900205"). Sorry, I can't explain better than this. My first guess is that I need to instantiate BankLogic some ther way in the GUI. What do you think?
– J.Dow
Nov 19 at 21:35
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382720%2flinking-gui-to-logic%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what error if any? and why aren't you showing the code to
logic.createSavingsAccount()
method?– T McKeown
Nov 19 at 21:18
The problem is that I can't get the call logic.createSavingsAccount(persNr) to return anything. The method should return int. Code in BankLogic works properly when separated from GUI.
– J.Dow
Nov 19 at 21:24
We can't help if we can't see the code.....
– T McKeown
Nov 19 at 21:25
It's a general problem, the logic.methods(persNr) don't make the proper return if they take a "persNr" reference as parameter. They work if parameters are values of existing customers personal numers (e.g."900205"). Sorry, I can't explain better than this. My first guess is that I need to instantiate BankLogic some ther way in the GUI. What do you think?
– J.Dow
Nov 19 at 21:35