0

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

Aaron
  • 193
  • 4
  • 5
  • 13

1 Answers1

4

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);
    }
}
Community
  • 1
  • 1
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
  • Is there anyway I can fix the code I have now without having to redo pretty much all of it? – Aaron Oct 11 '13 at 15:10
  • Wait... you want to open a new `JFrame` when you click/press any of these buttons? – Branislav Lazic Oct 11 '13 at 15:11
  • I will add a picture of the program when I run it showing all the buttons. – Aaron Oct 11 '13 at 15:13
  • 3
    +1, `Is there anyway I can fix the code I have now without having to redo pretty much all of it?` There is no time like the present to make changes. Another option is to use a [Card Layout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) instead of creating new JDialogs all the time (don't create JFrames). – camickr Oct 11 '13 at 15:21
  • It's saying when I use **button2.addActionListener(this);** it says I cannot use this in a static context. – Aaron Oct 11 '13 at 15:31
  • Then put your buttons outside of your `main` method. I.e. create some separated non-static method and put everything from your `main` in that method, and finally call that method via instance: `new Stocks().methodName();` – Branislav Lazic Oct 11 '13 at 15:35
  • **I forgot to include that I'm new to programming in general**, how exactly would I take it off of my main method? how would I start a new class to put it in? **new Stocks ().methodName();** what do you mean by that? – Aaron Oct 11 '13 at 15:39
  • @brano88 Thank you very much! you've helped a lot. this **Edit** has solved my problem. Greatly appreciated, and Happy Programming! – Aaron Oct 12 '13 at 21:48