Results 1 to 3 of 3
Thread: ActionListener interface
- 03-30-2008, 04:25 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
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.
- 03-30-2008, 07:35 PM #2
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(); } }
- 03-30-2008, 10:24 PM #3
Member
- Join Date
- Mar 2008
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
addActionListener(java.awt.event.ActionListener) in java.awt.Button cannot be applied
By mathias in forum Java AppletsReplies: 3Last Post: 03-26-2009, 10:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks