java - Return a double value from JDialog to JFrame? -



java - Return a double value from JDialog to JFrame? -

i'm making bank project , came problem. have depositwindow class extends jdialog , accountwindow class extends jframe. problem how update accountwindow balance adding previous balance , deposit amount in depositwindow class , display in accountwindow balance?

here accountwindow:

import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowevent; import java.awt.event.windowfocuslistener; public class accountwindow extends jframe { private final jbutton jbtdeposit; private final jbutton jbtexit; private final business relationship acct; public accountwindow(account acct) { this.acct = acct; // create panel p1 buttons , set gridlayout // set borderlayout horizontal gap 5 , vertical gap 10 setlayout(new borderlayout(5, 10)); jpanel pnlbutton = new jpanel(); pnlbutton.setborder(new titledborder("actions")); pnlbutton.setlayout(new flowlayout(flowlayout.right, 1, 1)); jbtdeposit = new jbutton("deposit"); jbtexit = new jbutton("exit"); pnlbutton.add(new jbutton("balance")); pnlbutton.add(jbtdeposit); pnlbutton.add(new jbutton("withdraw")); pnlbutton.add(new jbutton("apply charges")); pnlbutton.add(jbtexit); this.add(pnlbutton, borderlayout.south); // create panel p2 hold text field , p1 jpanel p2 = new jpanel(new gridlayout(2, 2)); p2.setborder(new titledborder("account")); p2.add(new jlabel("account number")); p2.add(new jlabel(acct.getacctnum())); p2.add(new jlabel("balance")); p2.add(new jlabel(string.format("%.2f",acct.getbalance()))); // add together contents frame add(p2, borderlayout.center); depositlistenerclass depositlistener = new depositlistenerclass(); jbtdeposit.addactionlistener(depositlistener); this.addwindowfocuslistener(new windowlistenerclass()); jbtexit.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { closewindow(); } }); } //default focus name field when window first displayed. private class windowlistenerclass implements windowfocuslistener { @override public void windowgainedfocus(windowevent e) { jbtexit.requestfocusinwindow(); } @override public void windowlostfocus(windowevent e) { } } /* * fire window closing window event calling window can * refresh. */ private void closewindow() { system.out.println("exiting"); windowevent wev = new windowevent(this, windowevent.window_closing); toolkit.getdefaulttoolkit().getsystemeventqueue().postevent(wev); } class depositlistenerclass implements actionlistener { public void actionperformed(actionevent e) { depositwindow frame = new depositwindow(); frame.settitle("deposit window"); //frame.setsize(400, 250); frame.setmodal(true); frame.pack(); frame.setlocationrelativeto(null); // center frame frame.setvisible(true); double amount = frame.getamount(); } } }

here's depositwindow:

import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.borderlayout; import java.awt.event.*; public class depositwindow extends jdialog { private jbutton jbtok; private jbutton jbtcancel; private jlabel jlbaccountnumber; private jtextfield jtfamount; public depositwindow() { setmodal(true); // create panel p1 buttons , set gridlayout // set borderlayout horizontal gap 5 , vertical gap 10 setlayout(new borderlayout(5, 10)); jpanel p1 = new jpanel(); p1.setborder( new titledborder("actions")); p1.setlayout(new flowlayout(flowlayout.right, 1, 1)); jbtok = new jbutton("ok"); jbtcancel = new jbutton("cancel"); p1.add(jbtok); p1.add(jbtcancel); this.add(p1,borderlayout.south); jpanel p2 = new jpanel(new gridlayout(2,2)); p2.setborder( new titledborder("account")); p2.add(new jlabel("account number")); jlbaccountnumber = new jlabel("*****-56"); p2.add(jlbaccountnumber); p2.add(new jlabel("amount")); jtfamount = new jtextfield(10); p2.add(jtfamount); // add together contents frame add(p2, borderlayout.center); jbtok.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e){ setvisible(false); } }); } public double getamount(){ homecoming double.parsedouble(jtfamount.gettext()); } /** main method */ public static void main(string[] args) { depositwindow frame = new depositwindow(); frame.settitle("deposit window"); frame.pack(); frame.setlocationrelativeto(null); // center frame frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }

account class:

class="snippet-code-js lang-js prettyprint-override"> import java.util.arraylist; public class business relationship { private string acctnum; protected double balance; private string name; //customer name private arraylist<transaction> transactions; private string password; @override public string tostring() { homecoming this.getclass().getname() + "[acctnum="+acctnum+ ",balance="+balance+ ",name="+name+"]"; } @override public boolean equals(object o) { if (o instanceof account) { homecoming (this.acctnum.equals(((account)o).acctnum)); } homecoming false; } public account() { transactions = new arraylist<transaction>(); }; public account(string acctnum, double balance) { this(); setacctnum(acctnum); setbalance(balance); } public account(string name, string acctnum, double balance) { this(); setacctnum(acctnum); setbalance(balance); setname(name); } public account(string name, string password, string acctnum, double balance) { this(); setpassword(password); setacctnum(acctnum); setbalance(balance); setname(name); } public string getacctnum() { homecoming acctnum; } public double getbalance() { homecoming balance; } public void setacctnum(string acctnum) { this.acctnum = acctnum; } public void setbalance(double balance) { this.balance = balance; } public void withdraw(double amt) { balance -= amt; transactions.add(new transaction('w',amt,balance,"withdrawal")); } public void deposit(double amt) { depositwindow d = new depositwindow(); if (d != null){ amt = d.getamount(); balance += amt; transactions.add(new transaction('d',amt,balance,"deposit")); } } /** * @return name */ public string getname() { homecoming name; } /** * @param name name set */ public void setname(string name) { this.name = name; } /** * @return transactions */ public arraylist<transaction> gettransactions() { homecoming transactions; } /** * @return password */ public string getpassword() { homecoming password; } /** * @param password password set */ public void setpassword(string password) { this.password = password; } }

if create dialog modal, can phone call depositwindow#getamount after setvisible(true) phone call returns (as block until user dismisses window)...

public class depositwindow extends jdialog { //... public depositwindow() { setmodal(true); //...

then...

public void actionperformed(actionevent e) { depositwindow frame = new depositwindow(); frame.settitle("deposit window"); //frame.setsize(400, 250); frame.setmodal(true); frame.pack(); frame.setlocationrelativeto(null); // center frame frame.setvisible(true); double amount = frame.getamount(); // update business relationship details }

see how create dialogs more details

java swing jdialog

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -