For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > Layout/button help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Layout/button help
Si

2004-03-27, 12:30 am

I am trying to display a "row" of information relating to bank account
details. I am using the java.awt package to do this. I am used to displaying
such information in e.g. MS Access, but never Java.

Each bank account row represents a single BankAccount object and there are
methods that return Account Number, Balance etc. to display them in labels.
Each row is generated by looping over an array of BankAccount objects. How
do I:

1) get each new account to start on a new row? All my labels and thus my
accounts are displayed on the one row currently.

2) have a button on each row that calls a method that takes in the account
number as a parameter and allows the user to update the balance? I know how
to fire a button event if the button object has a unique name but as each
button in each row is generated using a for loop then they all have the same
name and thus I can't uniquely identify them from within the actionPerformed
method. Any ideas?

Apologies for the silly questions but this really is my first attempt.

TIA.

My currently broken code is below

import java.awt.*;
import java.awt.event.*;

public class BankMainFrame extends Frame implements ActionListener
{
private Bank _bank;

protected Panel _account_list_panel;

public BankMainFrame()
{
setSize(300,200);
setResizable(true);
setTitle("Bank Frame");
setLocation(150,250);

_bank = new Bank(20);
_bank.openAccount(123, 'K');
_bank.openAccount(456, 'S');
_bank.openAccount(789, 'G');

displayAccountList();

this.pack();
}

private void displayAccountList()
{
_account_list_panel = new Panel();

for (int i = 0; i < _bank.getAccountCount(); i++)
{
BasicAccount acc;
Button deposit = new Button("Deposit");
Button withdraw = new Button("Withdraw");

acc = _bank.getAccount(i);
int acc_number = acc.getAccNumber();
double bal = acc.getBalance();

_account_list_panel.add(new Label(acc_number + " " + bal));
_account_list_panel.add(deposit);
}

this.add("South", _account_list_panel);
}

public void actionPerformed(ActionEvent event)
{
Button clicked_button = (Button)event.getSource();

//if (clicked_button == // NOT SURE HERE!
// I want to be able to use the deposit and withdraw Button objects here
to
// work on a specific account
}

public static void main(String[] args)
{
BankMainFrame bankframe = new BankMainFrame();
bankframe.show();
}
}





antroy

2004-03-27, 12:30 am

Si wrote:
> I am trying to display a "row" of information relating to bank account
> details. I am using the java.awt package to do this. I am used to displaying
> such information in e.g. MS Access, but never Java.
>
> Each bank account row represents a single BankAccount object and there are
> methods that return Account Number, Balance etc. to display them in labels.
> Each row is generated by looping over an array of BankAccount objects. How
> do I:
>
> 1) get each new account to start on a new row? All my labels and thus my
> accounts are displayed on the one row currently.


Give the top level panel (_account_list_panel) Grid layout
(_account_list_panel = new Panel(new GridLayout(0,1));)

Inside the loop create a new Panel object and add each of your labels to
this panel. At the end of the loop add the new panel to _account_list_panel.

> 2) have a button on each row that calls a method that takes in the account
> number as a parameter and allows the user to update the balance? I know how
> to fire a button event if the button object has a unique name but as each
> button in each row is generated using a for loop then they all have the same
> name and thus I can't uniquely identify them from within the actionPerformed
> method. Any ideas?


Use an anonymous inner class for the action and a method taking the
account number as an argument. For example, inside the loop:

ActionListener act = new ActionListener(){
public void actionPerformed(ActionEvent e){
updateBalance(acc_number);
}
};

Button update = new Button("Update Balance");
update.addActionListener(act);

....and in the main body of the class:

private void updateBalance(int account){
//Do stuff like open a dialog and ask for balance or whatever...
}

> Apologies for the silly questions but this really is my first attempt.


Incidentally why not use Swing - it has some much better widgets than
awt, and can make for cleaner code. For example in the above you could
make the ActionListener an AbstractAction instead, and call 'new
AbstractAction("Update Balance"){...};', then simply panel.add(new
JButton(act)); rather than messing with explicitly adding action listeners.

--
Ant...
Si

2004-03-27, 12:30 am

"antroy" <news@ant-roy.co.uk> wrote in message
news:c3rprp$bnh$1@iss-nntp.leeds.ac.uk...
> Si wrote:
displaying[color=darkred]
are[color=darkred]
labels.[color=darkred]
How[color=darkred]
>
> Give the top level panel (_account_list_panel) Grid layout
> (_account_list_panel = new Panel(new GridLayout(0,1));)
>
> Inside the loop create a new Panel object and add each of your labels to
> this panel. At the end of the loop add the new panel to

_account_list_panel.
>
account[color=darkred]
how[color=darkred]
each[color=darkred]
same[color=darkred]
actionPerformed[color=darkred]
>
> Use an anonymous inner class for the action and a method taking the
> account number as an argument. For example, inside the loop:
>
> ActionListener act = new ActionListener(){
> public void actionPerformed(ActionEvent e){
> updateBalance(acc_number);
> }
> };
>
> Button update = new Button("Update Balance");
> update.addActionListener(act);
>
> ...and in the main body of the class:
>
> private void updateBalance(int account){
> //Do stuff like open a dialog and ask for balance or whatever...
> }


Thanks for the excellent answer.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com