Skip to main content

grabbing listeners

13 replies [Last post]
vlie84
Offline
Joined: 2003-12-02

Hi,

thank you for the help for grabbing all window open by user.

now my other problem is , i want to get what caused any event and the command everytime any component in any of the open window is invoking an event (from tree or mouse or list or whatever), then show it in window B. is there anyway to do this?

i know that i could do e.getSource() and e.getActionCommand() for actionperformed method, but what about the other even occured how do i know the source and the command straight away?

at the moment i have my window A to be extends from window B and in the actionPerformed method i called super.actionPerformed. so everytime window A doin anything it will always called window B method. but i dont like this way. is there any simpler way to do it.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vlie84
Offline
Joined: 2003-12-02

I've tried to use your way but its not working too as now it always go to the getWinInfo, never go to the getButtonInfo.

then i tried with my method again but this time i have the if condition for all the Container component (such as JRootPane,JLayeredPane, etc) and in the else part i also have getCompInfo method which
[code]
public void getCompInfo (container x)
{
Component[] group = x.getComponents();
int i;

for (i = 0; i < group.length ;i++) {
getInside((Container)pane.getComponent(i)));}
}
[/code]

do you have any more suggestion why it doesnt want to work?
thank you so much.

shan-man
Offline
Joined: 2006-02-17

Hi,

vlie84 wrote:
> I've tried to use your way but its not working too as
> now it always go to the getWinInfo, never go to the
> getButtonInfo.

Have you tried compiling this stuff? I don't see how it could compile. Your method:
[code]
public void getInside(Container comp) {
if (comp instanceof JApplet) {
return getAppletInfo ((JApplet) comp);
} else if (comp instanceof JWindow) {
return getWindowInfo((JWindow) comp);
} else if ......
[/code]
It doesn't make sense. You can't have "return XXX" from a method with a void return type. My previous response assumed that the "return"s were typos...

> do you have any more suggestion why it doesnt want to
> work?
> thank you so much.

Try this for your getInside() method. First, it looks for each type of component and prints information on them. Then, it checks to see if the component is a container and recurses on their children:
[code]
public void getInside(Component x) {
if (x instanceof JButton) {
getButtonInfo((JButton)x);
} else if (x instanceof JTree) {
getButtonInfo((JTree)x);
} .....etc.....
// notice, NO RETURNS so far

// then
if (x instanceof Container) {
Component[] group = ((Container)x).getComponents();
for (int i = 0; i < group.length; i++) {
getInside(group[i]);
}
}
}
[/code]
Shannon

vlie84
Offline
Joined: 2003-12-02

hey shannon, thanks for your response. it work properly now. the return its ok, doesnt reallly matter

shan-man
Offline
Joined: 2006-02-17

Hi vlie84,

vlie84 wrote:
> hey shannon, thanks for your response. it work
> properly now. the return its ok, doesnt reallly matter

Glad to hear it works!

Are you saying that the code compiles? Your compiler doesn't complain when you try "return xxx" from a void method? Hmmm...Would you mind saving this code to a file (Foo.java) and then trying to compile it? What happens?
[code]
public class Foo {
public void method1() {
return method2();
}

public void method2() {
return;
}
}
[/code]
I try to compile this on 1.4.1:
---
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)
---

I get the following result:
---
Foo.java:3: cannot return a value from method whose result type is void
return method2();
^
1 error
---

What do you get? And what version of Java are you using? (run "java -version" on the command line to find out).

Thanks,
Shannon

vlie84
Offline
Joined: 2003-12-02

Hey ... it does not work. it work only if you put the return after all the if and else.
sorry ... >< .. i dont look carefully ...as im so confused with this code hehe ...
thanks for your help!! ^^

zander
Offline
Joined: 2003-06-13

> hey
> i tried to make that special listener class and
> adding it to each of the component. but apparently it
> doesnt work.
> i dont know why.
> do you have any idea why?

My crystal ball is broken at the moment; I'm afraid you will have to tell us a bit more then that it "does not work".

I'm not sure if you want to just catch ActionEvents, there are other listeners as well.

I suggest you try with some debugging info being printed about source and event type to see if you get the right events.

vlie84
Offline
Joined: 2003-12-02

hey

this is some sort of sample of what ive done
[code]
class A extends JFrame implements ActionListener
{
public A ()
{ JButton btn = new JButton("test");
getContentPane().add(btn);
btn.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{ System.out.println("Test" + e.getID());}
}

class B implements ActionListener
{
public B { C info = new C(this);}

// this is the method that grab all the windows open
// by the user
public void getFrame()
{
......
info.getInside(frame[i]); //frame[i] is one of the
//window open by the user
......
}
}

public class C
{
B list_class;
public C (B list) { list_class = list;}

public void getInside (Container x)
{
if (x instanceof Window)
return getWinInfo((Window)x);
if (x instanceof JButton)
return getBtnInfo((JButton)x);
}

public void getWinInfo (Window x)
{
int i;
Component[] comp = x.getComponents();
for ( i = 0 ; i < comp.length ; i++)
getInside((Container) comp[i]);
return;
}
}

public void getBtnInfo (JButton x)
{ x.addActionListener(list_class); return;}
}
[/code]
so B is the class that getting all the windows open by the user and passing each of them to C. and C is the class that getting all the component inside the window and add the component an ActionListener. B is also my special ActionListener class.

the problem is i thought this way is suppose to be working, but apparently eventhough i add the new actionlistener (from class B) to that button, the button only will do the method supplied by the actionlistener from class A.

what i want is it will do both actionlistener from class B first then class A, or vice versa (it doesnt really matters)

i hope you can help me fix this problem.

Message was edited by: shan-man (added [ code ] tags)

shan-man
Offline
Joined: 2006-02-17

vlie84,

In a situation like this when we're having extreme difficulty understanding what you're asking for, posting [b]compilable[/b] and [b]runnable[/b] code would really help. Also, please wrap code in [ code ] and [/ code ] tags (without the spaces). I've edited your previous message to add them.

In any case, your recursive algorithm is failing because you are assuming that your button is a direct child of its parent frame. But its not...remember that you added it to the content pane.

In fact, your button is in the content pane, which is within the frame's root pane. So you need to recurse through these levels of the hierarchy.

In summary, change the first if-case of getInside() to:[code]
if (x instanceof Container) {
getWinInfo((Container)x);
}[/code]
And change getWinInfo() to take a Container instead of a Window.

It's kind of funny. Your first message seemed to indicate you wanted ALL events of ALL types from ALL components. I've been really scratching my head trying to figure out how to help you detect: ActionEvent, FocusEvent, MouseEvent, KeyEvent, HierarchyEvent, WindowEvent, etc...

Regards,
Shannon

vlie84
Offline
Joined: 2003-12-02

hey Shannon
sorry i make you confused.
the event i want to get is like when user click a button or when user select a cell in a table or when they select something in the menu and stuff like that.

These are all listeners that i interested in
ActionListener,TreeModelListener,ItemListener,ListDataListener,ChangeListener,ListSelectionListener,MenuListener,TableColumnModelListener,TreeSelectionListener
and i've implemented in my special action listener class, that in here is stored in variable m.

[code]
public void getInside(Container comp)
{
if (comp instanceof JApplet)
{
return getAppletInfo ((JApplet) comp);
}
else if (comp instanceof JWindow)
{
return getWindowInfo((JWindow) comp);
}
else if (comp instanceof JDialog)
{
return getDialogInfo((JDialog) comp);
}
else if (comp instanceof JComboBox)
{
return getComboBoxInfo((JComboBox) comp);
}
else if (comp instanceof JLabel)
{
return getLabelInfo((JLabel) comp);
}
else if (comp instanceof JTextComponent)
{
return getTextAreaInfo((JTextComponent) comp);
}
else if (comp instanceof JFrame)
{
return getFrameInfo((JFrame) comp);
}
else if (comp instanceof JPanel)
{
return getPanelInfo((JPanel) comp);
}
else if (comp instanceof JScrollPane)
{
return getScrollInfo((JScrollPane) comp);
}
else if (comp instanceof JScrollBar)
{
return getScrollBarInfo ((JScrollBar) comp);
}
else if (comp instanceof JViewport)
{
return getViewPortInfo((JViewport) comp);
}
else if (comp instanceof JTabbedPane)
{
return getTabbedPaneInfo((JTabbedPane) comp);
}
else if (comp instanceof Box)
{
return getBoxInfo((Box) comp);
}
else if (comp instanceof JToggleButton)
{
return getToggleButtonInfo((JToggleButton) comp);
}
else if (comp instanceof JSplitPane)
{
return getSplitPaneInfo ((JSplitPane) comp);
}
else if (comp instanceof JList)
{
return getListInfo ((JList) comp);
}
else if (comp instanceof JTree)
{
return getTreeInfo ((JTree) comp);
}
else if (comp instanceof JTable)
{
return getTableInfo ((JTable) comp);
}
else if (comp instanceof JSlider)
{
return getSliderInfo ((JSlider) comp);
}
else if (comp instanceof JSpinner)
{
return getSpinnerInfo ((JSpinner) comp);
}
else if (comp instanceof JButton)
{
return getButtonInfo ((JButton) comp);
}
else if (comp instanceof JMenu)
{
return getMenuInfo ((JMenu) comp);
}
else if (comp instanceof JMenuBar)
{
return getMenuBarInfo ((JMenuBar) comp);
}
else if (comp instanceof JMenuItem)
{
return getMenuItemInfo ((JMenuItem) comp);
}
else if (comp instanceof JOptionPane)
{
return getOptionPaneInfo ((JOptionPane) comp);
}
else
{

System.out.println(comp.toString());return;
}
}[/code]

that is the complete version of my getInside method, so basically i went through all of the component that could exist in the contentPane.I don't think i miss any components that could be added to the contentpane there.

[code]
public void getTextAreaInfo(JTextComponent txt)
{ }

public void getButtonInfo (JButton button)
{
button.addActionListener(m);
}

public void getProgressBarInfo (JProgressBar bar)
{
bar.addChangeListener(m);
}

public void getSliderInfo (JSlider slider)
{
slider.addChangeListener(m);
}

public void getSpinnerInfo (JSpinner spinner)
{
spinner.addChangeListener(m);
}

public void getLabelInfo(JLabel lbl)
{
}

public void getComboBoxInfo(JComboBox box)
{
ComboBoxModel cm = box.getModel();

box.addActionListener(m);
cm.addListDataListener(m);
}

public void getToggleButtonInfo (JToggleButton button)
{
button.addActionListener(m);
}

public void getMenuItemInfo (JMenuItem menuitem)
{
menuitem.addActionListener(m);
}

public void getMenuBarInfo (JMenuBar menubar)
{
for (int i= 0 ; i < menubar.getMenuCount() ; i++)
{
getInside(menubar.getMenu(i));
}

}

public void getMenuInfo (JMenu menu)
{
menu.addActionListener(m);
menu.addMenuListener(m);
for (int i= 0 ; i < menu.getMenuComponentCount() ; i++)
{
getInside((Container)menu.getMenuComponent(i)));
}
}

public void getAppletInfo (JApplet applet)
{
Container group = applet.getContentPane();

getInside(group);

}

public void getWindowInfo (JWindow window)
{
Container group = window.getContentPane();

getInside(group);
}

public void getDialogInfo (JDialog dialog)
{
Container group = dialog.getContentPane();

getInside(group);
}

public void getFrameInfo (JFrame frame)
{
Container group = frame.getContentPane();

getInside(group);

}

public void getPanelInfo (JPanel pane)
{
Component[] group = pane.getComponents();
int i;

for (i = 0; i < group.length ; i++)
{
getInside((Container)pane.getComponent(i));
}

}

public void getScrollInfo (JScrollPane scroll)
{
Component[] group = scroll.getComponents();
int i;

for (i = 0; i < group.length ; i++)
{
getInside((Container)group[i]);
}
}

public void getTabbedPaneInfo (JTabbedPane tab)
{

int j;

tab.addChangeListener(m);

for (j = 0 ; j < tab.getTabCount(); j++)
{
Component group = tab.getComponentAt(j);
getInside((Container)group);

}
}

public void getOptionPaneInfo (JOptionPane op)
{
}

public void getViewPortInfo (JViewport vp)
{
Component[] group = vp.getComponents();
int i;

vp.addChangeListener(m);

for (i = 0; i < group.length ; i++)
{
getInside((Container)group[i]);
}
}

public void getBoxInfo (Box box)
{

Component[] group = box.getComponents();
int i;
for (i = 0; i < group.length ; i++)
{
getInside((Container)group[i]);
}
}

public void getSplitPaneInfo (JSplitPane split)
{
Component[] group = split.getComponents();
int i;
for (i = 0; i < group.length ; i++)
{
getInside((Container)group[i]);
}

}

public void getListInfo (JList list)
{
ListModel ls = list.getModel();
int i;

list.addListSelectionListener(m);
ls.addListDataListener(m);

}

public void getTableInfo (JTable table)
{
TableModel tm = table.getModel();
int i,j;
String text;

TableColumnModel tcm = table.getColumnModel();
ListSelectionModel lm = table.getSelectionModel();

lm.addListSelectionListener(m);
tcm.addColumnModelListener(m);
}

public void getScrollBarInfo (JScrollBar scroll)
{}

public void getTreeInfo (JTree tree)
{
TreeModel tm = tree.getModel();
int i;

tree.addTreeSelectionListener(m);
tm.addTreeModelListener(m);

Object root = tm.getRoot();
String index = "";
getChildInfo(tm,root,tree,index);

}

public void getChildInfo (TreeModel tm,Object parent,JTree tree,String index)
{
if (tm.isLeaf(parent) == false)
{
String text = parent.toString();

int j;
for (j = 0 ; j < tm.getChildCount(parent) ; j++)
{
Object child = tm.getChild(parent,j);
getChildInfo(tm,child,tree,index));
}
}
else
{
String text = parent.toString();
}

}
[/code]

if you think there is more component or listener that i need to add , please do tell me. Because i want to go through each of the component that exist in the window component and call the get..Info for each of the component.

with this way do you think my approach is still wrong?

thank you so much.

zander
Offline
Joined: 2003-06-13

What about creating a special listener and adding it to each and every component of a window?

To add it you can use the getComponents call on each widget to recursively walk over each widget in the whole window.
See:
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Container.html#getComponents()

vlie84
Offline
Joined: 2003-12-02

yeah thats what im thinking too.
but is there any easier way?
i look at EventQueue and i try it but for some reason it doesnt want to registered the event for action event.

vlie84
Offline
Joined: 2003-12-02

hey
i tried to make that special listener class and adding it to each of the component. but apparently it doesnt work.
i dont know why.
do you have any idea why?

vlie84
Offline
Joined: 2003-12-02

is it possible because for one component you cannot add more than one actionlistener attached to it?