Linking GUI to Logic












1















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!










share|improve this question

























  • what error if any? and why aren't you showing the code to logic.createSavingsAccount() method?

    – T McKeown
    Nov 19 '18 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 '18 at 21:24













  • We can't help if we can't see the code.....

    – T McKeown
    Nov 19 '18 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 '18 at 21:35
















1















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!










share|improve this question

























  • what error if any? and why aren't you showing the code to logic.createSavingsAccount() method?

    – T McKeown
    Nov 19 '18 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 '18 at 21:24













  • We can't help if we can't see the code.....

    – T McKeown
    Nov 19 '18 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 '18 at 21:35














1












1








1








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 21:59







J.Dow

















asked Nov 19 '18 at 21:13









J.DowJ.Dow

62




62













  • what error if any? and why aren't you showing the code to logic.createSavingsAccount() method?

    – T McKeown
    Nov 19 '18 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 '18 at 21:24













  • We can't help if we can't see the code.....

    – T McKeown
    Nov 19 '18 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 '18 at 21:35



















  • what error if any? and why aren't you showing the code to logic.createSavingsAccount() method?

    – T McKeown
    Nov 19 '18 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 '18 at 21:24













  • We can't help if we can't see the code.....

    – T McKeown
    Nov 19 '18 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 '18 at 21:35

















what error if any? and why aren't you showing the code to logic.createSavingsAccount() method?

– T McKeown
Nov 19 '18 at 21:18





what error if any? and why aren't you showing the code to logic.createSavingsAccount() method?

– T McKeown
Nov 19 '18 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 '18 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 '18 at 21:24















We can't help if we can't see the code.....

– T McKeown
Nov 19 '18 at 21:25





We can't help if we can't see the code.....

– T McKeown
Nov 19 '18 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 '18 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 '18 at 21:35












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini