Could you help on this reflection technique?

S

Shawn

Hi,

I have a very simple GUI program: it has a menu and a text
area(JTextArea). In the meu, it has several meu items: "save memo 1",
"save memo 2", "get memo 1", "get memo 2", "clear", "exit". What the
program does is: when the user types something and click "save memo 1",
that line will be saved into an internal string memo1; when the user
types something else and click "save memo 2", that line will be saved
into another internal string memo2; when the user clicks "get memo 1",
that line will be shown in the text area, etc. It is very simple.


public class MemoGUI extends JFrame implements ActionListener
{

....
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save Memo 1"))
memo1 = theText.getText();
else if (actionCommand.equals("Save Memo 2"))
memo2 = theText.getText();
else if (actionCommand.equals("Clear"))
theText.setText("");
else if (actionCommand.equals("Get Memo 1"))
theText.setText(memo1);
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2);
else if (actionCommand.equals("Exit"))
System.exit(0);
else
theText.setText("Error in memo interface");
}
} // end of class

I want to do something fancy here: using reflection. Unfortunately I
know very little about it and I cannot make it work:
(I have used setActionCommand to save1, save2, get1, get2,etc)

public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();

/*****Seeking help for the following lines. I am very new to reflection */
Class c = MenuAction.class;
Class[] parameterTypes = new Class[] {MenuAction.class};
Method theMethod;

try {
theMethod = c.getMethod(actionCommand, null); //not very sure ?
theMethod.invoke(obj, args) //compiler says error here !!!
} catch (NoSuchMethodException err) {
System.out.println(err);
} catch (IllegalAccessException err) {
System.out.println(err);
} catch (InvocationTargetException err) {
System.out.println(err);
}
}

private class MenuAction
{
void save1()
{
memo1 = theText.getText();
}
void save2()
{
memo2 = theText.getText();
}
void get1()
{
theText.setText(memo1);
}
void get2()
{
theText.setText(memo2);
}
void clear()
{
theText.setText("");
}
void exit()
{
System.exit(0);
}
}

Thank you very much for your help.
 
A

Andrew Thompson

Shawn wrote:
...
I want to do something fancy here: using reflection.

Why? What?

Reflection is generally something reserved for 'when all else fails'.
Servers use it a lot, because they need to. It also might come in
handy for devlopment tools, or for (just heard about this one)
loading clases that were declared in the default package.

What is it's use in a text(/memo) editor?

Andrew T.
 
M

Manish Pandit

IMHO, using reflection in this situation will only help you save some
lines of code, but add the performance overhead.

I agree with Andrew T - I believe reflection exists for 'when all
fails' and is a blessing for system level operations (like autocomplete
on any IDE or the little property editor you see in eclipse, or for
introspection when there is no source code but the binary class). Other
than that, I've seen it being used for generating stubs/skeletons in
RMI/J2EE development by external tools.

-cheers,
Manish
 
S

Shawn

Thank you for your feedbacks. OK, I don't want to use reflection. But I
really hate such coding(if .. else if .. else if ..):
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("save1"))
memo1 = theText.getText();
else if (actionCommand.equals("save2"))
memo2 = theText.getText();
else if (actionCommand.equals("clear"))
theText.setText("");
else if (actionCommand.equals("get1"))
theText.setText(memo1);
else if (actionCommand.equals("get2"))
theText.setText(memo2);
else if (actionCommand.equals("exit"))
System.exit(0);
else
theText.setText("Error in memo interface");
}

Every details is up and front, which is not wise. I hope to suppress
such details (or mess) into lower level so that the level I am working
on is clean. In some procedural language, if I have the procedures
defined before hand:

void save1()
{
....
}

void save2()
{
....
}

void get1()
{
....
}

Now I can do the following:

String actionCommand = e.getActionCommand();

do_it(actionCommand); //passing a procedure as a parameter into
another procedure, the appropriate procedure will be evoked.

I thought to use reflection to achieve the effect above. If not
recommended, what else approach can I think about?

Thank you very much.
 
S

Shawn

Thank you for your feedbacks. OK, I don't want to use reflection. But I
really hate such coding(if .. else if .. else if ..):
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("save1"))
memo1 = theText.getText();
else if (actionCommand.equals("save2"))
memo2 = theText.getText();
else if (actionCommand.equals("clear"))
theText.setText("");
else if (actionCommand.equals("get1"))
theText.setText(memo1);
else if (actionCommand.equals("get2"))
theText.setText(memo2);
else if (actionCommand.equals("exit"))
System.exit(0);
else
theText.setText("Error in memo interface");
}

Every details is up and front, which is not wise. I hope to suppress
such details (or mess) into lower level so that the level I am working
on is clean. In some procedural language, if I have the procedures
defined before hand:

void save1()
{
....
}

void save2()
{
....
}

void get1()
{
....
}

Now I can do the following:

String actionCommand = e.getActionCommand();

do_it(actionCommand); //passing a procedure as a parameter into
another procedure, the appropriate procedure will be evoked.

For example, if I need to add another menu item or button called
"save3", I only need to take care adding the menu item into the gui menu
and provide the procedure "void save3(){ do what I want to do }". I
don't need to get into the mess (if .. else if .. else if ...).

I thought to use reflection to achieve the effect above. If not
recommended, what else approach can I think about?

Thank you very much.
 
S

Shawn

Thank you for your feedbacks. OK, I don't want to use reflection. But I
really hate such coding(if .. else if .. else if ..):
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("save1"))
memo1 = theText.getText();
else if (actionCommand.equals("save2"))
memo2 = theText.getText();
else if (actionCommand.equals("clear"))
theText.setText("");
else if (actionCommand.equals("get1"))
theText.setText(memo1);
else if (actionCommand.equals("get2"))
theText.setText(memo2);
else if (actionCommand.equals("exit"))
System.exit(0);
else
theText.setText("Error in memo interface");
}

Every details is up and front, which is not wise. I hope to suppress
such details (or mess) into lower level so that the level I am working
on is clean. In some procedural language, if I have the procedures
defined before hand:

void save1()
{
....
}

void save2()
{
....
}

void get1()
{
....
}

Now I can do the following:

String actionCommand = e.getActionCommand();

do_it(actionCommand); //passing a procedure as a parameter into
another procedure, the appropriate procedure will be evoked.

For example, if I need to add another menu item or button called
"save3", I only need to take care adding the menu item into the gui menu
and provide the procedure "void save3(){ do what I want to do }". I
don't need to get into the mess (if .. else if .. else if ...).

I thought to use reflection to achieve the effect above. If not
recommended, what else approach can I think about? I am not clear about
how to use an interface Mapper here.

Thank you very much.
 
S

Shawn

I try to use interface Mapper approach to evoke action dynamically. But
I ran into an error:

//in the very top of my program
interface Mapper {
public void menuItemAction();
}

.....


public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand(); //get the string
"save1", or "save2", or "get1", or "get2", etc.

//Try to evoke different action dynamically, but:
((Mapper)actionCommand).menuItemAction(); //Error: cannot cast
a String into a Mapper
}

//Mapper definitions block
{
Mapper save1 = new Mapper()
{
public void menuItemAction()
{
memo1 = theText.getText();
}
};

Mapper save2 = new Mapper()
{
public void menuItemAction()
{
memo2 = theText.getText();
}
};

Mapper get1 ....

}//end of Mapper definition block

Thank you very much for your help. I greatly appreciate it.
 
I

Ingo R. Homann

Hi,

AFAICS, there are two different issues:

(1) different actions (save/clear/get)
(2) different 'sources'/'destinations' (memo1/memo2)

As the others said, you do not need (and should not use) any reflection
for this!

Without knowing your design/source completely, I would suggest:

The first problem can best be solved using an Array of eh... JTextFields
with two elements instead of two single JTextFields:

JTextField memo1, memo2; // Not a good idea!
JTextField memos=new JTextsField[2]; // better!

The second problem can be solved by different solutions:

(A) A "dispatch-method" like you tried:
if(action.equals("save")) {
memos.setText(theText.getText);
} else if(action.equals("clear")) {
theText.setText(memos.getText());
} else if(action.equals("get")) {
memos.setText("");
}

(B) Using different ActionsListeners for each action instead of one
ActionListener for all Events. Anonymous inner classes are a good idea
for that:

saveButton.addActionsListener(void actionPerformed(ActionEvent e){
memos.setText(theText);
});
clearButton.addActionsListener(void actionPerformed(ActionEvent e){
memos.setText("");
});
....

Hth,
Ingo
 
I

Ingo R. Homann

Hi,
String actionCommand = e.getActionCommand(); //get the string
"save1", or "save2", or "get1", or "get2", etc.
((Mapper)actionCommand).menuItemAction(); //Error: cannot cast
a String into a Mapper

Eh... Yes. The error is obvious. A String is not a Mapper and therefore
cannot be casted to one... It's self-explainatory...

Ciao,
Ingo
 
R

Rogan Dawes

Shawn said:
I try to use interface Mapper approach to evoke action dynamically. But
I ran into an error:

//in the very top of my program
interface Mapper {
public void menuItemAction();
}

....


public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand(); //get the string
"save1", or "save2", or "get1", or "get2", etc.

//Try to evoke different action dynamically, but:
((Mapper)actionCommand).menuItemAction(); //Error: cannot cast
a String into a Mapper
}

//Mapper definitions block
{
Mapper save1 = new Mapper()
{
public void menuItemAction()
{
memo1 = theText.getText();
}
};

Mapper save2 = new Mapper()
{
public void menuItemAction()
{
memo2 = theText.getText();
}
};

Mapper get1 ....

}//end of Mapper definition block

Thank you very much for your help. I greatly appreciate it.

It looks like what you want to do is create multiple Mapper instances
(which you have done), then save them in a Map, so you can look them up
using the actionCommand string.

e.g.

Map<String, Mapper> myActions = new HashMap<String, Mapper>();

// create your Mapper instances

myActions.put("save1", save1);
// link the String "save1" with the Mapper instance save1
myActions.put("save2", save2);
//etc

public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
Mapper mapper = myActions.get(actionCommand);
mapper.menuItemAction();
}

Note that there is not really a need to define your own Mapper
interface, you might want to investigate the Action classes which do
this for you already.

Rogan
 
S

Shawn

Thank you all for your generous help. I greatly appreciate it.

I have finished my little gui program. Code is pasted below. Two things
I feel proud about my code:
(1)I wrote one class JMenuPower, subclass of JMenu. I can put objects of
JMenuItem, which will 1)automatically register ActionListener to "this"
object 2)setActionCommand to the String parameter passed in 3)put both
the actionCommand string and the Mapper reference into a HashMap for
later retrieval.

In conclusion, my JMenuPower class is in higher level than JMenu class.
So my brain can have less burden.

(2)When user clicks the different Menu Item, the corresponding operation
is evoked dynamically. I achieved this goal by using an interface
Mapper. So if I need to add one Menu Item, I only need to do two things a)
MenuItem m = new JMenuItem("new one");
memoMenu.addMenuItemAndListener(m, "newone", newoneMapper,
this); //throw it into my JMenuPower object, which takes care of the rest

b) //write the Maper do what I want to do
Mapper newoneMapper = new Mapper()
{
public void menuItemAction()
{
//put code here
}
};

Thank you very much for your feedback.

===============JMenuPower.java==============
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/****************************************************************************************
* My class extends from JMenu: the objects of JMenuItem can be added
into the object of
* this class. It will automatically register the actionlistener so the
lines in the client
* class is reduced and concept is clearer: add the menu item and
register the action listener.
* In another words, JMenuPower class is higher level than JMenu class now.
* @author lix
*
*/
public class JMenuPower extends JMenu
{
public Map<String, Mapper> myActions = new HashMap<String, Mapper>();

public JMenuPower(String s)
{
super(s);
}

void addMenuItemAndListener(JMenuItem mi, ActionListener al)
{
mi.addActionListener(al);
this.add(mi);
}

void addMenuItemAndListener(JMenuItem mi, String str, Mapper
mapper, ActionListener al)
{
mi.addActionListener(al);
mi.setActionCommand(str);
this.add(mi);

myActions.put(str, mapper);
}

public Mapper getActionMapper(String str)
{
return myActions.get(str);
}

}


=================MemoGui3.java======================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;

interface Mapper {
public void menuItemAction();
}

public class MemoGUI3 extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;

private JTextArea theText;
private String memo1 = "No Memo 1";
private String memo2 = "No Memo 2";

JMenuPower memoMenu;

public MemoGUI3()
{
this.setSize(WIDTH, HEIGHT);
this.addWindowListener(new WindowDestroyer());
this.setTitle("Memo Saver");
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());

memoMenu = new JMenuPower("Memos"); //my own JMenuPower class
JMenuItem m;

//comparing to MemoGUI.java, following code is clearer:
m = new JMenuItem("Save Memo 1");
memoMenu.addMenuItemAndListener(m, "save1", save1Mapper, this);

m = new JMenuItem("Save Memo 2");
memoMenu.addMenuItemAndListener(m, "save2", save2Mapper, this);

m = new JMenuItem("Get Memo 1");
memoMenu.addMenuItemAndListener(m, "get1", get1Mapper, this);

m = new JMenuItem("Get Memo 2");
memoMenu.addMenuItemAndListener(m, "get2", get2Mapper, this);

m = new JMenuItem("Clear");
memoMenu.addMenuItemAndListener(m, "clear", clearMapper, this);

m = new JMenuItem("Exit");
memoMenu.addMenuItemAndListener(m, "exit", exitMapper, this);

JMenuBar mBar = new JMenuBar();
mBar.add(memoMenu);
this.setJMenuBar(mBar);

JPanel textPanel = new JPanel();
textPanel.setBackground(Color.blue);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.white);
textPanel.add(theText);
contentPane.add(textPanel, BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e)
{
String actionStr = e.getActionCommand();

(memoMenu.getActionMapper(actionStr)).menuItemAction();
}

//Mapper definitions

Mapper save1Mapper = new Mapper()
{
public void menuItemAction()
{
memo1 = theText.getText();
}
};

Mapper save2Mapper = new Mapper()
{
public void menuItemAction()
{
memo2 = theText.getText();
}
};

Mapper get1Mapper = new Mapper()
{
public void menuItemAction()
{
theText.setText(memo1);
}
};

Mapper get2Mapper = new Mapper()
{
public void menuItemAction()
{
theText.setText(memo2);
}
};

Mapper clearMapper = new Mapper()
{
public void menuItemAction()
{
theText.setText("");
}
};

Mapper exitMapper = new Mapper()
{
public void menuItemAction()
{
System.exit(0);
}
};


//end of Mapper definition block


public static void main(String[] args)
{
MemoGUI3 gui = new MemoGUI3();
gui.setVisible(true);
}

}
 
S

Shawn

Thank you all for your generous help. I greatly appreciate it.

I have finished my little gui program. Code is pasted below. Two things
I feel proud about my code:
(1)I wrote one class JMenuPower, subclass of JMenu. I can put objects of
JMenuItem, which will 1)automatically register ActionListener to "this"
object 2)setActionCommand to the String parameter passed in 3)put both
the actionCommand string and the Mapper reference into a HashMap for
later retrieval.

In conclusion, my JMenuPower class is in higher level than JMenu class.
So my brain can have less burden.

(2)When user clicks the different Menu Item, the corresponding operation
is evoked dynamically. I achieved this goal by using an interface
Mapper. So if I need to add one Menu Item, I only need to do two things a)
MenuItem m = new JMenuItem("new one");
memoMenu.addMenuItemAndListener(m, "newone", newoneMapper,
this); //throw it into my JMenuPower object, which takes care of the rest

b) //write the Maper do what I want to do
Mapper newoneMapper = new Mapper()
{
public void menuItemAction()
{
//put code here
}
};

Thank you very much for your feedback.

===============JMenuPower.java==============
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/****************************************************************************************
* My class extends from JMenu: the objects of JMenuItem can be added
into the object of
* this class. It will automatically register the actionlistener so the
lines in the client
* class is reduced and concept is clearer: add the menu item and
register the action listener.
* In another words, JMenuPower class is higher level than JMenu class now.
* @author lix
*
*/
public class JMenuPower extends JMenu
{
private Map<String, Mapper> myActions = new HashMap<String, Mapper>();

public JMenuPower(String s)
{
super(s);
}

void addMenuItemAndListener(JMenuItem mi, ActionListener al)
{
mi.addActionListener(al);
this.add(mi);
}

void addMenuItemAndListener(JMenuItem mi, String str, Mapper
mapper, ActionListener al)
{
mi.addActionListener(al);
mi.setActionCommand(str);
this.add(mi);

myActions.put(str, mapper);
}

public Mapper getActionMapper(String str)
{
return myActions.get(str);
}

}


=================MemoGui3.java======================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;

interface Mapper {
public void menuItemAction();
}

public class MemoGUI3 extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;

private JTextArea theText;
private String memo1 = "No Memo 1";
private String memo2 = "No Memo 2";

JMenuPower memoMenu;

public MemoGUI3()
{
this.setSize(WIDTH, HEIGHT);
this.addWindowListener(new WindowDestroyer());
this.setTitle("Memo Saver");
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());

memoMenu = new JMenuPower("Memos"); //my own JMenuPower class
JMenuItem m;

//comparing to MemoGUI.java, following code is clearer:
m = new JMenuItem("Save Memo 1");
memoMenu.addMenuItemAndListener(m, "save1", save1Mapper, this);

m = new JMenuItem("Save Memo 2");
memoMenu.addMenuItemAndListener(m, "save2", save2Mapper, this);

m = new JMenuItem("Get Memo 1");
memoMenu.addMenuItemAndListener(m, "get1", get1Mapper, this);

m = new JMenuItem("Get Memo 2");
memoMenu.addMenuItemAndListener(m, "get2", get2Mapper, this);

m = new JMenuItem("Clear");
memoMenu.addMenuItemAndListener(m, "clear", clearMapper, this);

m = new JMenuItem("Exit");
memoMenu.addMenuItemAndListener(m, "exit", exitMapper, this);

JMenuBar mBar = new JMenuBar();
mBar.add(memoMenu);
this.setJMenuBar(mBar);

JPanel textPanel = new JPanel();
textPanel.setBackground(Color.blue);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.white);
textPanel.add(theText);
contentPane.add(textPanel, BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e)
{
String actionStr = e.getActionCommand();

(memoMenu.getActionMapper(actionStr)).menuItemAction();
}

//Mapper definitions

Mapper save1Mapper = new Mapper()
{
public void menuItemAction()
{
memo1 = theText.getText();
}
};

Mapper save2Mapper = new Mapper()
{
public void menuItemAction()
{
memo2 = theText.getText();
}
};

Mapper get1Mapper = new Mapper()
{
public void menuItemAction()
{
theText.setText(memo1);
}
};

Mapper get2Mapper = new Mapper()
{
public void menuItemAction()
{
theText.setText(memo2);
}
};

Mapper clearMapper = new Mapper()
{
public void menuItemAction()
{
theText.setText("");
}
};

Mapper exitMapper = new Mapper()
{
public void menuItemAction()
{
System.exit(0);
}
};


//end of Mapper definition block


public static void main(String[] args)
{
MemoGUI3 gui = new MemoGUI3();
gui.setVisible(true);
}

}
 
S

Shawn

Thank you all for your generous help. I greatly appreciate it.

I have finished my little gui program. Code is pasted below. Two things
I feel proud about my code:
(1)I wrote one class JMenuPower, subclass of JMenu. I can put objects of
JMenuItem, which will 1)automatically register ActionListener to "this"
object 2)setActionCommand to the String parameter passed in 3)put both
the actionCommand string and the Mapper reference into a HashMap for
later retrieval.

In conclusion, my JMenuPower class is in higher level than JMenu class.
So my brain can have less burden.

(2)When user clicks the different Menu Item, the corresponding operation
is evoked dynamically. I achieved this goal by using an interface
Mapper. So if I need to add one Menu Item, I only need to do two things a)
MenuItem m = new JMenuItem("new one");
memoMenu.addMenuItemAndListener(m, "newone", newoneMapper,
this); //throw it into my JMenuPower object, which takes care of the rest

b) //write the Maper do what I want to do
Mapper newoneMapper = new Mapper()
{
public void menuItemAction()
{
//put code here
}
};

Thank you very much for your feedback.

===============JMenuPower.java==============
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/****************************************************************************************
* My class extends from JMenu: the objects of JMenuItem can be added
into the object of
* this class. It will automatically register the actionlistener so the
lines in the client
* class is reduced and concept is clearer: add the menu item and
register the action listener.
* In another words, JMenuPower class is higher level than JMenu class now.
*
*/
public class JMenuPower extends JMenu
{
private Map<String, Mapper> myActions = new HashMap<String, Mapper>();

public JMenuPower(String s)
{
super(s);
}

void addMenuItemAndListener(JMenuItem mi, ActionListener al)
{
mi.addActionListener(al);
this.add(mi);
}

void addMenuItemAndListener(JMenuItem mi, String str, Mapper
mapper, ActionListener al)
{
mi.addActionListener(al);
mi.setActionCommand(str);
this.add(mi);

myActions.put(str, mapper);
}

public Mapper getActionMapper(String str)
{
return myActions.get(str);
}

}


=================MemoGui3.java======================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;

interface Mapper {
public void menuItemAction();
}

public class MemoGUI3 extends JFrame implements ActionListener
{
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;

private JTextArea theText;
private String memo1 = "No Memo 1";
private String memo2 = "No Memo 2";

JMenuPower memoMenu;

public MemoGUI3()
{
this.setSize(WIDTH, HEIGHT);
this.addWindowListener(new WindowDestroyer());
this.setTitle("Memo Saver");
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());

memoMenu = new JMenuPower("Memos"); //my own JMenuPower class
JMenuItem m;

//Just throw the Menu Item object into the object of JMenuPower class.
It will take care of the rest:
m = new JMenuItem("Save Memo 1");
memoMenu.addMenuItemAndListener(m, "save1", save1Mapper, this);

m = new JMenuItem("Save Memo 2");
memoMenu.addMenuItemAndListener(m, "save2", save2Mapper, this);

m = new JMenuItem("Get Memo 1");
memoMenu.addMenuItemAndListener(m, "get1", get1Mapper, this);

m = new JMenuItem("Get Memo 2");
memoMenu.addMenuItemAndListener(m, "get2", get2Mapper, this);

m = new JMenuItem("Clear");
memoMenu.addMenuItemAndListener(m, "clear", clearMapper, this);

m = new JMenuItem("Exit");
memoMenu.addMenuItemAndListener(m, "exit", exitMapper, this);

JMenuBar mBar = new JMenuBar();
mBar.add(memoMenu);
this.setJMenuBar(mBar);

JPanel textPanel = new JPanel();
textPanel.setBackground(Color.blue);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.white);
textPanel.add(theText);
contentPane.add(textPanel, BorderLayout.CENTER);

}

//even need to add new Menu Item, this part of code need NOT to be
changed !!!
public void actionPerformed(ActionEvent e)
{
String actionStr = e.getActionCommand();

(memoMenu.getActionMapper(actionStr)).menuItemAction();
}

//All the mess (the details of what the program should do when user
clicks the Menu Item) is pushed to here, -- the lowest level

//Mapper definitions

Mapper save1Mapper = new Mapper()
{
public void menuItemAction()
{
memo1 = theText.getText();
}
};

Mapper save2Mapper = new Mapper()
{
public void menuItemAction()
{
memo2 = theText.getText();
}
};

Mapper get1Mapper = new Mapper()
{
public void menuItemAction()
{
theText.setText(memo1);
}
};

Mapper get2Mapper = new Mapper()
{
public void menuItemAction()
{
theText.setText(memo2);
}
};

Mapper clearMapper = new Mapper()
{
public void menuItemAction()
{
theText.setText("");
}
};

Mapper exitMapper = new Mapper()
{
public void menuItemAction()
{
System.exit(0);
}
};


//end of Mapper definition block


public static void main(String[] args)
{
MemoGUI3 gui = new MemoGUI3();
gui.setVisible(true);
}

}
 
I

Ingo R. Homann

Hi,

I think, you can do this in half the code, if you use an Array for the
two memos instead of two single Memos...

Ciao,
Ingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top