user3344370
user3344370

Reputation:

Link a button to another method in Java

Im wanting to link a button to another method in my java program. I have this code so far

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

public class Contacts extends JFrame
{


public static Contacts cont;
public static JFrame canvas;

public static void main(String[] args)
{
    cont = new Contacts();

    canvas = new JFrame("Contacts");
    //canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    canvas.show();
    canvas.getContentPane().add(cont.viewOne());
    canvas.pack();
}


/**
 * T.B.C
 */
private JPanel viewAll()
{
    int noOfContacts = 3; //3 will be replaced with number of tasks found in database

    JPanel viewAllPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    viewAllPanel.setPreferredSize(new Dimension(500,500));
    viewAllPanel.setBackground(Color.white);

    //label for TITLE 
    JLabel title = new JLabel("CONTACTS"); 
    title.setForeground(Color.black);
    c.gridx = 0;
    c.gridy = 0;     
    viewAllPanel.add(title,c);

    //button for add new contact
    JButton add = new JButton("ADD");
    add.setForeground(Color.black);
    c.gridx = 1;
    c.gridy = 0;
    viewAllPanel.add(add, c);

    //buttons for all contacts
    for(int i = 0; i < noOfContacts; i++){
        JButton name = new JButton("CONTACT " + i); //name of contact will be gathered from database using get method
        name.setForeground(Color.black);
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = i+1;     
        viewAllPanel.add(name,c);
    }



    return viewAllPanel;
}


/**
 * T.B.C
 */
private JPanel viewOne()
{
    int details=5;

    JPanel viewOnePanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    viewOnePanel.setPreferredSize(new Dimension(500,500));
    viewOnePanel.setBackground(Color.white);

    //button for back
    JButton back = new JButton("BACK");
    back.setForeground(Color.black);
    c.gridx = 0;
    c.gridy = 0;
    viewOnePanel.add(back, c);


    //label for TITLE 
    JLabel title = new JLabel("CONTACTS"); 
    title.setForeground(Color.black);
    c.gridx = 1;
    c.gridy = 0;     
    viewOnePanel.add(title,c);

    //button for edit new contact
    JButton edit = new JButton("EDIT");
    edit.setForeground(Color.black);
    c.gridx = 2;
    c.gridy = 0;
    viewOnePanel.add(edit, c);  

    //buttons for single contact titles
    for(int i = 0; i < details; i++){
        JLabel titles = new JLabel("DETAIL " + i); //name of contact will be gathered from database using get method
        titles.setForeground(Color.black);
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = i+1;     
        viewOnePanel.add(titles,c);
    }

    //buttons for single contact answers
    for(int i = 0; i < details; i++){
        JLabel answers = new JLabel("ANSWER " + i); //name of contact will be gathered from database using get method
        answers.setForeground(Color.black);
        c.gridwidth = 2;
        c.gridx = 1;
        c.gridy = i+1;     
        viewOnePanel.add(answers,c);
    }

    return viewOnePanel;
}

}

I want to link the 'BACK' button from the viewOne method to the viewAll method. How can this be achieve? Thanks

Upvotes: 0

Views: 2846

Answers (3)

Stephen
Stephen

Reputation: 46

consider using a CardLayout. With a CardLayout you can flip different panels. Each of those panels is added to a parent container with an identifier object. you can switch between the panels using layout.show(parentContainer, id). To "connect" the flip with a button just use an ActionListener. The actionPerformed method will be called if the user presses the button.

Hope this helps.

public class Contacts extends JPanel {

private enum EView {ALL, DETAILS};

private CardLayout layout;
private EView showing = EView.ALL;
private JPanel content;
private JButton btn;

public Contacts(){
    setLayout(new BorderLayout());

    //set up a panel with button triggering the flip
    JPanel btnPanel = new JPanel(new FlowLayout());
    btn = new JButton("Show Details");
    btnPanel.add(btn);      
    this.add(btnPanel, BorderLayout.NORTH);

    //the flip container
    //sets the cardlayout
    content = new JPanel(layout = new CardLayout());

    //just a red panel. that is where you might want to put all contacts
    JPanel panelAll = new JPanel();
    panelAll.setBackground(Color.RED);

    //a green panel. that is where you might want to put details about a contact
    JPanel panelDetails = new JPanel();
    panelDetails.setBackground(Color.GREEN);


    //add the panels
    content.add(panelAll, EView.ALL.toString());
    content.add(panelDetails, EView.DETAILS.toString());

    //show all contacts
    layout.show(content, EView.ALL.toString());



    this.add(content, BorderLayout.CENTER);


    btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (showing == EView.ALL){
                //showing all -> now show details

                layout.show(content, EView.DETAILS.toString());
                showing = EView.DETAILS;
                btn.setText("Back");
            } else {
                //showing details -> now show all

                layout.show(content, EView.ALL.toString());         
                showing = EView.ALL;
                btn.setText("Show Details");
            }
        }
    });
}


public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.getContentPane().add(new Contacts());
    frame.setVisible(true);

}

  }

Upvotes: 1

RKC
RKC

Reputation: 1874

Try adding this in your viewOne() method,

    back.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            canvas.add(viewAll());
        }
    });

Upvotes: 0

Boris the Spider
Boris the Spider

Reputation: 61148

In Java 8 you can use a method reference:

final JButton button = new JButton();
button.addActionListener(this::doStuff);

Where the method doStuff must have the following signature:

public void doStuff(final ActionEvent e) {
}

In earlier versions of Java you must use an anonymous class:

final JButton button = new JButton();
button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        doStuff();
    }
});

Where doStuff can have any signature you want.

Upvotes: 2

Related Questions