Results 1 to 3 of 3
  1. #1
    tsantana is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default ActionListener interface

    Hello All,

    I am studying Java as a personal goal for this year. In a attempt to convert a batch time entry program into a visual interface program, I decided to learn about Gui programming on java. I am still getting AWT though. I got my self into the following issue:

    Given the following class, I am not able to get a dialog BOX displayed when I press the "Press Me" button:

    package mainProgram;

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

    public class GraphicTest extends Button implements ActionListener{

    private Frame mainScreen;
    private List mainList;
    private Button mainButton;
    private Dialog dialogBox;

    public void actionPerformed(ActionEvent actEvent){

    String pressedButton = actEvent.getActionCommand();

    if (pressedButton.equals("Press Me")) {

    dialogBox.setVisible(true);

    }

    }

    public void load() {

    mainScreen = new Frame("Main Window");
    mainList = new List();
    mainButton = new Button("Press Me");
    dialogBox = new Dialog(mainScreen, "Dialog", true);

    mainList.add("A", 0);
    mainList.add("List", 1);

    mainScreen.setLayout(new GridLayout(9,2));
    mainScreen.add(new Label("Button"));
    mainScreen.add(mainButton);
    mainScreen.add(new Label("Choice"));
    mainScreen.add(new Choice());
    mainScreen.add(new Label("Label"));
    mainScreen.add(new Label("This is a Label"));
    mainScreen.add(new Label("ScrollBar"));
    mainScreen.add(new Scrollbar());
    mainScreen.add(new Label("TextField"));
    mainScreen.add(new TextField());
    mainScreen.add(new Label("Canvas"));
    mainScreen.add(new Canvas());
    mainScreen.add(new Label("List"));
    mainScreen.add(mainList);
    mainScreen.add(new Label("TextArea"));
    mainScreen.add(new TextArea());

    mainScreen.pack();
    mainScreen.setVisible(true);

    mainButton.setActionCommand("Press Me");
    this.addActionListener(this);

    }

    public static void main(String[] args){

    GraphicTest graphictest = new GraphicTest();
    graphictest.load();

    }
    }




    Could anyone tell me what I did wrong and help me fix it, please?

    Thanks all for the help.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    // change this
    this.addActionListener(this);
    // to this
    mainButton.addActionListener(this);
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class GraphicTestRx extends Button implements ActionListener {
        private Frame mainScreen;
        private List mainList;
        private Button mainButton;
        private Dialog dialogBox;
    
        public void actionPerformed(ActionEvent actEvent){
            String pressedButton = actEvent.getActionCommand();
            if (pressedButton.equals("Press Me")) {
                dialogBox.setVisible(true);
            }
        }
    
        public void load() {
            mainScreen = new Frame("Main Window");
            mainScreen.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            mainList = new List();
            mainButton = new Button("Press Me");
            dialogBox = new Dialog(mainScreen, "Dialog", true);
            dialogBox.setSize(200,200);
            dialogBox.setLocation(200,200);
            dialogBox.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    ((Dialog)e.getSource()).dispose();
                }
            });
    
            mainList.add("A", 0);
            mainList.add("List", 1);
    
    //        mainScreen.setLayout(new GridLayout(9,2));
            mainScreen.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,0,2,0);
            addComponents(new Label("Button"), mainButton, mainScreen, gbc);
            addComponents(new Label("Choice"), new Choice(), mainScreen, gbc);
            addComponents(new Label("Label"),
                          new Label("This is a Label"), mainScreen, gbc);
            addComponents(new Label("ScrollBar"),
                          new Scrollbar(), mainScreen, gbc);
            addComponents(new Label("TextField"),
                          new TextField(), mainScreen, gbc);
            Canvas canvas = new Canvas();
            canvas.setBackground(Color.blue);
            addComponents(new Label("Canvas"), canvas, mainScreen, gbc);
            addComponents(new Label("List"), mainList, mainScreen, gbc);
            addComponents(new Label("TextArea"),
                          new TextArea("hello world"), mainScreen, gbc);
    
            mainScreen.pack();
            mainScreen.setVisible(true);
    
            mainButton.setActionCommand("Press Me");
            mainButton.addActionListener(this);
        }
    
        private void addComponents(Component c1, Component c2, Container c,
                                   GridBagConstraints gbc) {
            gbc.gridwidth = GridBagConstraints.RELATIVE;
            c.add(c1, gbc);
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            c.add(c2, gbc);
        }
    
        public static void main(String[] args){
            GraphicTestRx graphictest = new GraphicTestRx();
            graphictest.load();
        }
    }

  3. #3
    tsantana is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    hardwired,

    Your code looks wonderfull. The frame looks really nice, not to mention the code to close the window which I was missing and looking forward to learn.

    Oh boy, I wish I had your knowledge.

    Thanks a lot!!!

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2009, 10:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •