JohnSnow
JohnSnow

Reputation: 173

how to create new window with swing when clicking JButton

I need to create a bank account management program for school, I created the "skeleton" of the first page but I do not understand some things:

  1. How to open a new window with Swing when I click a button?
  2. How can I have different ActionListener for each button?
  3. If I change strategy and I want to use just one big window and let appear and disappear the working text fields/Labels, how would I do it?

Code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

public class CreditUnion extends JFrame
{
//declare buttons
private JButton openAccount;
private JButton closeAccount;
private JButton makeLodgement;
private JButton makeWithdrawal;
private JButton requestOverdraft;

//constructor
public CreditUnion()
{
    super("ATM@CreditUnion");

    Container c = getContentPane();
    c.setLayout(new FlowLayout() );

    openAccount = new JButton("Open account");
    c. add(openAccount);

    closeAccount = new JButton("Close account");
    c. add(closeAccount);

    makeLodgement = new JButton("Make lodgement");
    c. add(makeLodgement);

    makeWithdrawal = new JButton("Make withdrawal");
    c. add(makeWithdrawal);

    requestOverdraft = new JButton("Request overdraft");
    c. add(requestOverdraft);

    /*create instance of inner class ButtonHandler
    to use for button event handling*/
    ButtonHandler handler = new ButtonHandler();
    openAccount.addActionListener(handler);
    closeAccount.addActionListener(handler);
    makeLodgement.addActionListener(handler);
    makeWithdrawal.addActionListener(handler);
    requestOverdraft.addActionListener(handler);

    setSize(800,600);
    show();
}


public static void main (String args[])
{
    CreditUnion app = new CreditUnion();
    app.addWindowListener(
    new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }
    );
}

//inner class for button event handling

private class ButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        JOptionPane.showMessageDialog(null, "You Pressed: " + e.getActionCommand() );
    }
}

Upvotes: 2

Views: 1451

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

1- how to open a new window with Swing when I click a button?

The same way that you would display any window. In the button's ActionListener, create a new window -- I would suggest that you create a JDialog and not a JFrame, but this will depend on your assignment requirements too, and display it

2- how can I have different ActionListener for each button?

Add a different ActionListener to each button. An anonymous inner class (search on this) would work great for this.

3- if I change strategy and I want to use just one big window and let appear and disappear the working Textfields/Labels, how would I do it?

Use a CardLayout to swap "views", usually JPanels with components that you want to swap.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347204

how to open a new window with Swing when I click a button?

Don't, use a CardLayout, it's less distracting to the user

Have a look at How to use CardLayout for more details

how can I have different ActionListener for each button?

You can use a separate class, inner class or anonymous class depending on what you to achieve

Have a look at Nested classes for more details

if I change strategy and I want to use just one big window and let appear and disappear the working Textfields/Labels, how would I do it

See the first point

Upvotes: 1

Related Questions