Aaron
Aaron

Reputation: 193

Adding Action Events to JButtons

Yesterday I had a problem with my code being nested and having too many static classes. I have cleaned up the code and am now trying to add an action listener to the JButtons. i have 5 different buttons that is on the GUI, "Profile, Market, Users, Notes, Information." each one will be a button on a GUI. The user will be able to click on one of the JButtons such as "Profile" and it will open another GUI. "I'm writing this in eclipse.". "I'm also not using a GUI builder." I have made another GUI that will open when one of the buttons are clicked :

public void actionPerformed (ActionEvent e) {
    JFrame frame2 = new JFrame("Your Stocks");
    frame2.setVisible(true);
    frame2.setSize(600,600);
    JLabel label = new JLabel("Your Personal Stocks");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);   
}

I just cannot figure out how to add it to each JButton. If there is anyways someone could give a visual idea or link on how to add the GUI above to all of the JButtons, that would be greatly appreciated. Here is the code with all of my JButtons, and the GUI ActionEvent :

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stocks {

public static void main(String [] args) {

    JFrame frame = new JFrame ("Java Stocks");
    frame.setSize(700,700);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel (new GridBagLayout());
    frame.add(panel);
    frame.getContentPane().add(panel, BorderLayout.WEST);
    GridBagConstraints c = new GridBagConstraints ();

    JButton button1 = new JButton("Profile");
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(40, 40, 40, 40);
    panel.add(button1, c);
    button1.addActionListener(new Action());

    JButton button2 = new JButton("Market");
    c.gridx = 0;
    c.gridy = 1;
    panel.add(button2, c);  
    button2.addActionListener(new Action());

    JButton button3 = new JButton("Users");
    c.gridx = 0;
    c.gridy = 2;
    panel.add(button3, c);
    button3.addActionListener(new Action());

    JButton button4 = new JButton("Notes");
    c.gridx = 0;
    c.gridy = 3;
    panel.add(button4, c);
    button4.addActionListener(new Action());

    JButton button5 = new JButton("Information");
    c.gridx = 0;
    c.gridy = 4;
    panel.add(button5, c);
    button5.addActionListener(new Action());
}

public void actionPerformed (ActionEvent e) {
    JFrame frame2 = new JFrame("Your Stocks");
    frame2.setVisible(true);
    frame2.setSize(600,600);
    JLabel label = new JLabel("Your Personal Stocks");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);   
}
}

enter image description here Here is a picture of the buttons. The user will be able to click one of the buttons and it will open a new GUI

Upvotes: 0

Views: 31015

Answers (1)

Branislav Lazic
Branislav Lazic

Reputation: 14816

As I understood, you want to open a new JFrame when you click any of these buttons. If so then make next two changes:

First: Implement ActionListener interface in your Stock class

public class Stock implements ActionListener {
    //Rest of the code...
}

Second: Pass this keyword in addActionListener method's for each of your buttons:

button1.addActionListener(this);
button2.addActionListener(this);
....

Also, I almost forgot obligatory answer: The Use of Multiple JFrames: Good or Bad Practice?

EDIT: There you go, whole solution (if this doesn't help, I give up):

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Stocks implements ActionListener {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Stocks().createGui();
            }
        });

    }

    public void createGui() {
        JFrame frame = new JFrame("Java Stocks");
        frame.setSize(700, 700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridBagLayout());
        frame.add(panel);
        frame.getContentPane().add(panel, BorderLayout.WEST);
        GridBagConstraints c = new GridBagConstraints();

        JButton button1 = new JButton("Profile");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(40, 40, 40, 40);
        panel.add(button1, c);
        button1.addActionListener(this);

        JButton button2 = new JButton("Market");
        c.gridx = 0;
        c.gridy = 1;
        panel.add(button2, c);
        button2.addActionListener(this);

        JButton button3 = new JButton("Users");
        c.gridx = 0;
        c.gridy = 2;
        panel.add(button3, c);
        button3.addActionListener(this);

        JButton button4 = new JButton("Notes");
        c.gridx = 0;
        c.gridy = 3;
        panel.add(button4, c);
        button4.addActionListener(this);

        JButton button5 = new JButton("Information");
        c.gridx = 0;
        c.gridy = 4;
        panel.add(button5, c);
        button5.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        JFrame frame2 = new JFrame("Your Stocks");
        frame2.setVisible(true);
        frame2.setSize(600, 600);
        JLabel label = new JLabel("Your Personal Stocks");
        JPanel panel = new JPanel();
        frame2.add(panel);
        panel.add(label);
    }
}

Upvotes: 4

Related Questions