aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem with actionevent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with actionevent" Watch "problem with actionevent" New topic
Author

problem with actionevent

trinadh reddy
Ranch Hand

Joined: Sep 22, 2005
Posts: 60
in my program why the control is not going to actionPerformed(ActionEvent ae)method. i have debugged by giving the System.out.println statements

import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.awt.event.*;


public class TestPanel1 extends JPanel {

private JLabel blankSpace;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel jLabel7;
private JLabel jLabel8;
private JLabel jLabel9;
private JLabel jLabel10;
private JLabel jLabel11;

public JTextField tdomain;
public JTextField tserver;

public JButton create;

private JLabel welcomeTitle;
private JPanel contentPanel;

private JLabel iconLabel;
private ImageIcon icon;

public TestPanel1() {

iconLabel = new JLabel();
contentPanel = getContentPanel();
contentPanel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));

icon = getImageIcon();

setLayout(new java.awt.BorderLayout());

if (icon != null)
iconLabel.setIcon(icon);

iconLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED));

add(iconLabel, BorderLayout.WEST);

JPanel secondaryPanel = new JPanel();
secondaryPanel.add(contentPanel, BorderLayout.NORTH);
add(secondaryPanel, BorderLayout.CENTER);
}


private JPanel getContentPanel() {

JPanel contentPanel1 = new JPanel();
JPanel jPanel1 = new JPanel();

welcomeTitle = new JLabel();
blankSpace = new JLabel();
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel7 = new JLabel();
jLabel6 = new JLabel();
jLabel8 = new JLabel();
jLabel9 = new JLabel();
jLabel10= new JLabel();
jLabel11= new JLabel();
tdomain = new JTextField(6);
tserver = new JTextField(5);
create = new JButton();


contentPanel1.setLayout(new java.awt.BorderLayout());

welcomeTitle.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 11));
welcomeTitle.setText("Welcome to the Wizard Dialog!");
contentPanel1.add(welcomeTitle, java.awt.BorderLayout.NORTH);

jPanel1.setLayout(new java.awt.GridLayout(0, 1));

jPanel1.add(blankSpace);
jLabel1.setText("This is an example of a wizard dialog, which allows a user to traverse");
jPanel1.add(jLabel1);
jLabel2.setText("a number of panels (while entering data) until the wizard has enough ");
jPanel1.add(jLabel2);
jLabel3.setText("information to perform whatever end function is necessary. Note that");
jPanel1.add(jLabel3);
jLabel4.setText("panels are not necessarily ordered in a linear fashion, but instead in");
jPanel1.add(jLabel4);
jLabel5.setText("a tree-like manner (e.g., there may be more than one panel with a");
jPanel1.add(jLabel5);
jLabel7.setText("'Finish' button, and it depends on the user's entries as to how they ");
jPanel1.add(jLabel7);
jLabel6.setText("traverse the path). That's not the case with this example, however.");
jPanel1.add(jLabel6);
jPanel1.add(jLabel8);
jLabel9.setText("Press the 'Next' button to continue....");
jPanel1.add(jLabel9);
jLabel10.setText("Enter the domain name");
jPanel1.add(jLabel10);
jPanel1.add(tdomain);
jLabel11.setText("Enter the server name");
jPanel1.add(jLabel11);
jPanel1.add(tserver);


create.setText("execute");
jPanel1.add(create);


contentPanel1.add(jPanel1, java.awt.BorderLayout.CENTER);

return contentPanel1;

}
public void actionPerformed(ActionEvent ae)
{
try
{
String s=ae.getActionCommand();
System.out.println(s);
if(s.equals("create"))
{
String s1=tdomain.getText();
System.out.println(s1);
System.out.println("hello");
String str1 = "java -Dweblogic.Domain=" + s1;
FileWriter f= new FileWriter("d:/puvvada/super.bat");
f.write(str1+ "weblogic.Server");
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd /c start d:/puvvada/super.bat");
f.close();
System.out.println("after executing the batch file");
}
}catch(Exception e)
{
e.printStackTrace();
}

}






private ImageIcon getImageIcon() {
return new ImageIcon((URL)getResource("clouds.jpg"));
}

private Object getResource(String key) {

URL url = null;
String name = key;

if (name != null) {

try {
Class c = Class.forName("com.nexes.test.Main");
url = c.getResource(name);
} catch (ClassNotFoundException cnfe) {
System.err.println("Unable to find Main class");
}
return url;
} else
return null;

}

}
please see where i had done wrong
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
You can only have an ActionEvent going to an ActionListener, so you have to get java.awt.event.ActionListener into your class.
Fortunately ActionListener is an interface . . . .
Ram Mohan
Greenhorn

Joined: Oct 21, 2005
Posts: 26
public class TestPanel1 extends JPanel implement ActionListener
................
............
..................
............

create.addActionListener(this);
................
...............

Ram Mohan.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
That is one way to do it. I had a long discussion with somebody on another forum last week, which I hope you will find on this thread, on java(intermediate) about implementing mvc model or observer. There are about three possible ways to implement ActionListeners, and I personally think getting the enclosing JFrame to implement it is an inelegant solution to the problem, although other people will doubtless disagree.

The thing about computing is, that there isn't a "right" way to do things, there can be lots fo "right" ways. But there are also lots of wrong ways.

To decide which format to put you ActionListener in (there are three,
  • addActionListener(this)
  • separate classes, usually private inner classes, but you could use a public class in a separate file for something like "CloseListener" which may be reused frequently
  • and the anonymous inner class
  • ) really depends on two things: will you be using the same listener more than once. If you have listeners which do similar things, eg GreenListener, RedListener, you might be better to add them as,
    Anyway, you pays your money and you takes your choice, as they say in London.
    I have never tried a public class in a different file for ExitListener; I presume it works.

    Try all the different options and see which works . . . .
     
     
    subject: problem with actionevent