2

I have the following code which creates a simple window with two buttons which are in turn suppose to open up a window each - the main window opens up just fine but when you click on the buttons nothing happens...

package presentation;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ShowInventory extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 7479750059244371227L;
    private JPanel contentPane;
    private JButton catBtn = new JButton ("Display inventory by category");
    private JButton allBtn = new JButton ("Display all inventory");


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ShowInventory frame = new ShowInventory();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Exception - Sorry");
                }
            }
        });
    }

    /**
     * Default - details to be added
     */
    public ShowInventory() { // title bar name

        // layout here
        Container container = getContentPane();
        FlowLayout layout = new FlowLayout();
        container.setLayout(layout);
        layout.setAlignment(FlowLayout.CENTER);
        container.add(new JButton("Display inventory by category"));
        container.add(new JButton("Display all inventory"));


        catBtn.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                // controller code
                ShowByCategory frame = new ShowByCategory();
                frame.setVisible(true);
            }
        });



        allBtn.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                // controller code
                ShowAllInventory frame = new ShowAllInventory();
                frame.setVisible(true);
            }
        });

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

}
1

1 Answer 1

4

You probably need to replace those lines (which create brand new buttons):

container.add(new JButton("Display inventory by category"));
container.add(new JButton("Display all inventory"));

by this (which uses the buttons of your class, on which you then add the listeners):

container.add(catBtn);
container.add(allBtn);
4
  • and that works now... as soon as it lets me I will accept the answer. Can you tell me why that works?
    – Expecto
    Jun 21, 2012 at 14:23
  • the button in container.add(new JButton("Display inventory by category")); has nothing to do with catBtn, it is a new object.
    – assylias
    Jun 21, 2012 at 14:24
  • @JosepthVodary, It works because now you're adding the correct JButton instances, not entirely new ones. Jun 21, 2012 at 14:25
  • 1
    God you people are helpful, I love this site.
    – Expecto
    Jun 21, 2012 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.