Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2008, 05:25 PM
Member
 
Join Date: Mar 2008
Posts: 4
tsantana is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-30-2008, 08:35 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
Code:
// change this this.addActionListener(this); // to this mainButton.addActionListener(this);
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(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-30-2008, 11:24 PM
Member
 
Join Date: Mar 2008
Posts: 4
tsantana is on a distinguished road
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!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
An interface extending an interface(II) JavaForums Java Blogs 0 03-12-2008 04:00 PM
An interface extending an interface(I) JavaForums Java Blogs 0 03-12-2008 04:00 PM
Implementing ActionListener interface JavaForums Java Blogs 0 12-19-2007 10:54 PM
Interface extending Interface JavaForums Java Blogs 0 12-04-2007 02:20 PM
addActionListener(java.awt.event.ActionListener) in java.awt.Button cannot be applied mathias Java Applets 1 08-06-2007 09:49 AM


All times are GMT +3. The time now is 04:09 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org