Listeners - How to callback the object it is listening to



Is there a way for a listener object (ex: myContainerListener) to know
when it is being registered as a listener to a container object (in this case myContainer.addContainerListener(myContainerListener)) and get the reference of the object it is being registered to (i.e myContainerListener
being able to call back methods on myContainer).

Specifically, I am trying to create a windowsMenu object which implements a
container listener and listens to a list of desktoppanes. When Frames are being added to the Desktop, the menu should update itself with menu items for each frame. The menu also shall have titleMenuItem and cascadeMenuItem,
which basically should loop through all the desktopPanes it is listening to (this is where I need to get the reference of all the objects which have registered the menu as their containerListener) and try to cascade/title each of the desktoppane.

I appreciate any answer and also appreciate any better solution for window
management.

Posted On: Tuesday 23rd of October 2012 11:35:18 PM Total Views:  263
View Complete with Replies

RELATED TOPICS OF Swing / AWT / SWT PROGRAMMING LANGUAGE




Attaching listeners to dialog box buttons

Hi I am very new to swing and am having some trouble trying to attach listeners to buttons in a dialog box. My current assignment question is: Create a Java program that employs a MouseListener in a Dialog component. My goal is to have the buttons' text display the current events that are happeneing (e.g. "Button 1" will change to "mouse pointer in 'Button 1'" or "'Button 1' has been clicked" when the different events are happening. Here is my code so far, the problem is trying to assign each button an event. I am getting compiler errors when I try to add events to the buttons array: import javax.swing.*; import java.awt.event.*; import java.util.*; import java.awt.*; public class MouseListenerDialog extends JFrame { public MouseListenerDialog() { super("Using MouseListeners and Dialogs"); setLayout (new FlowLayout()); Object[] buttons = {"Button 1", "Button 2", "Button 3"}; JOptionPane.showOptionDialog(null, "These buttons have MouseListeners attached to them", "MouseListener Dialog", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[2]); MouseHandler mouseH = new MouseHandler(); buttons[0].addMouseListener(mouseH);//
VIEWS ON THIS POST

162

Posted on:

Tuesday 25th September 2012
View Replies!

SWT code not using a receiving object

, i am running this example in eclipse and it works fine, but i'm baffled by the strucuture of a particular statement: Please notice that the: "createMenuBar();" statement below does not have a receiving object, for example something like: "Menu m = createMenuBar(); and notice that the createMenuBar() funtion is returning a "Menu", but there isn't a Menu object on the receiving end! , is this a new 5.0 feature by any chance public Shell open(Display display) { shell = new Shell(display); shell.setLayout(new FillLayout()); shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { e.doit = closeAddressBook(); } }); createMenuBar();
VIEWS ON THIS POST

95

Posted on:

Thursday 18th October 2012
View Replies!

adding objects to Combo Boxes in SWT

In Swing, combo boxes are filled with objects. In SWT, they seem to be filled with strings. Can this be right\t Can anyone tell me how add an object instead of a string to an SWT Combo. Extra
VIEWS ON THIS POST

107

Posted on:

Thursday 18th October 2012
View Replies!

listeners2

Have been trying to code a program and i am having problems with the listeners. I have a combobox, a bunch of textfields and a 'ok button'. User picks the combo box, and writes in some text into the textfields and hits the 'ok button'. For now i want to just get all the info in varialbes so i can write more code later to write the results to disk. However i cant get the listeners to work right. here are some of the problem points.. blah blah ButtonListener BL=new ButtonListener(); blah blah` buttoncreate=new JButton("Create .trn file"); buttoncreate.addActionListener(BL); mainPanel.add(buttoncreate); this.add(mainPanel); buttonquit=new JButton("Quit"); mainPanel.add(buttonquit); blah blah public class ButtonListener implements ActionListener{ public void actionPerformed (ActionEvent e){ if(e.getSource()==buttoncreate){ String start=(String)carType.getSelectedItem(); } else{ buttoncreate.setText("bad"); } it will compile but when i enter a value or no value in textfields i just get an error on the Sring start line
VIEWS ON THIS POST

78

Posted on:

Thursday 18th October 2012
View Replies!

listeners

ButtonListener BL=new ButtonListener();
VIEWS ON THIS POST

69

Posted on:

Thursday 18th October 2012
View Replies!

Border object with Title

Is there a java object that you can use which acts as a border and also has a title
VIEWS ON THIS POST

91

Posted on:

Thursday 18th October 2012
View Replies!

Loading table model with a custom object

I am writing a set of objects that will be used to build a JTable. They are as follows : TableLayout contains Vector TableRowLayout contains TableColumnLayout contains Object What I considered doing was have a base model class with a loadModel method which took the TableLayout as a parameter and loading the model data, then extend this class for each table in my application. This class would have a populate method that build the TableLayout, then calls the base loadModel method. I want to try and keep this as generic as possible. Does this approach make sense
VIEWS ON THIS POST

100

Posted on:

Thursday 18th October 2012
View Replies!

Killing...removing object

I currently have an applet that displays a circle object on the screen when i press a button. Whenever I press the button again, it is suppose to clear any circles on the screen and draw a new one. But I can't seem to remove the first one, it just draws it on top of the other one. I'm using AWT. Is there anyway to clear an object off the screen
VIEWS ON THIS POST

88

Posted on:

Thursday 18th October 2012
View Replies!

Zoom in shape object

, I am trying to zoom on a shape object with the AffineTransform class, and would like to know how to zoom from the center of my object graph, and this graphic object can be a circle, rectangle, polygon, etc. .. . I'm lost in logic to perform the transformation.
VIEWS ON THIS POST

53

Posted on:

Sunday 21st October 2012
View Replies!

Change background/foreground color of a disablled object

How I can change background/foreground color of a disablled object (i.e. JCheckBox, JTextField and so on..) Thank you, very much! Domenico
VIEWS ON THIS POST

100

Posted on:

Sunday 21st October 2012
View Replies!

Questin on listeners

Every single book Ive read shows that keylisteners are added to a window or frame only in public static void main(). So I was wondering how do you add a keylistener to a class that does not have the main method, or is this even possible What I want to do is to have a main method that displays a frame and adds a keylistener to it to tell the frame to close when the user clicks the x or when he presses escape. I want to create a separate class that displays objects on screen and that has a keylistener so that the objects can respond to user presses. Im not even sure how I would get that class to draw things to the frame set up by the main method class. The code I have so far for the class (without the main method) is as follows. But how do I add the listener class and will this class be able to draw on the frame displayed by the other class ************************************* import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; public class hoppingballImg implements KeyListener{ Image ball; double posX, posY; double dx=.3, dy=.3; Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); int w=d.width; int h=d.height; JFrame j=new JFrame(); addKeyListener(j); public hoppingballImg(String imgStr){ ball=new ImageIcon(imgStr).getImage(); posX=w*(new Random().nextDouble()); posY=h*(new Random().nextDouble()); } public void keyPressed(KeyEvent ke) { switch (ke.getKeyCode()){ case KeyEvent.VK_UP:{break;}//add ball; case KeyEvent.VK_DOWN:{break;}//remove ball; case KeyEvent.VK_RIGHT:{break;}//add ball; case KeyEvent.VK_LEFT:{break;}//add ball; } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} } ********************************************************************************************** The code I have for the main class (the one with the main method) is ********************************************************************* import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.ImageIcon; public class hoppingballmain extends JFrame{ public static void main(String[] args) { int numBalls=5; String imgString="C:\\Users\\2104304349\\Pictures\\"; hoppingballmain f=new hoppingballmain(); hoppingballImg hb[]=new hoppingballImg[5]; for (int i=0;i
VIEWS ON THIS POST

100

Posted on:

Monday 22nd October 2012
View Replies!

Doubt about user object returned on treeNodesChanged method / TreeModelListener - Beginner

... I have JTree made of DefaultMutableTreeNode nodes. These nodes are created with a very simple user object inside. example: category = new DefaultMutableTreeNode(new Directory(key,hm.get(key))); Every time I trigger the treeNodesChanged event and try to access the Directory object inside treeNodesChanged it is apparently "replaced" by a String object. example: public void treeNodesChanged(TreeModelEvent e) { DefaultMutableTreeNode node; node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent()); System.out.println(node.getUserObject().getClass()); -> AT THIS POINT I WAS EXPECTING TO PRINT THE DIRECTORY CLASS BUT WHAT IS RETURNED IS "class java.lang.String" ......... .... ...... .. . } Can someone help me understand what am I doing wrong
VIEWS ON THIS POST

106

Posted on:

Monday 22nd October 2012
View Replies!

I have placed a Rectangle object in an Applet container.

I have placed a Rectangle object in an Applet container. Is there any pre-defined method to get that object when i give (x,y) point
VIEWS ON THIS POST

85

Posted on:

Monday 22nd October 2012
View Replies!

Can i add object to tree?

I would like to add some custom defined object as a tree item(to perform cut, copy , paste). But tree item has only method setText. Is there any other way to do Please see attached snapshot. I am using SWT Tree, TreeItem object.
VIEWS ON THIS POST

112

Posted on:

Monday 22nd October 2012
View Replies!

Tooltips lost when saving object

I am writing a program that needs to save information periodically (ie a forced user backup). The object being saved has a JCheckBox on which I called setToolTipText. The problem is when the object is saved the tool tip text is lost. However if I quit the program and read in the save file the tool tip text is restored. I looked in the Sun bug database and could not find a match. So is this a new bug for Java 1.6 or is there some special function I need to add to continuing using an object after it was saved.
VIEWS ON THIS POST

82

Posted on:

Monday 22nd October 2012
View Replies!

mouse event for an array of object

Is it possible add moseDragged event for an array of rectangle...
VIEWS ON THIS POST

171

Posted on:

Monday 22nd October 2012
View Replies!

those crazy listeners!

i have a class MyPanel that extends jpanel but implements ActionListener, so i can just add itself to components i throw into MyPanel, and it worked fine but here is where my problem is : a certain button adds another custom component(that also has a button), so in the actionPerformed method im trying to add an actionListener(itself) to it, but the compiler doesnt like this at all and i get an error "cannot find symbol: method addActionListener(MyPanel)" is it that you cant add an actionListener from within the actionListener you want to add whats going on here earlier in the code(outside of the actionListener) i can call button.addActionListener(this) just fine, but i cant figure out why this is wrong here.
VIEWS ON THIS POST

86

Posted on:

Monday 22nd October 2012
View Replies!

Mouselistner not working for a table object

Hi all, I have a JTable object called VTable present in the swing package. For this JTable, none of the listeners gets executed. VTable m_table = new VTable(rowData,colNames,false); m_table.addMouseListener(new java.awt.event.MouseListener(){ public void mouseClicked(MouseEvent e) { System.out.println("TEST!!!"); }
VIEWS ON THIS POST

82

Posted on:

Tuesday 23rd October 2012
View Replies!

JCombo and its listeners

Hallo I have a combo with variable contend, that means I sometimes remove its elements and add new one. The combo has also a action listener(java.awt.event.ActionListener) and via this I am informed fi the selection is changed. The problem comes when I reset the combo and add new elements, in this case after I add the first item to combo an event raises this make GUI logic a little bit "slappy" - I receive an event when I am not expected(I expect to receive enents only when the user change the slection) There are some work around but I not hapy with them. I try with the item listener but I reach the same point. How I can be shore when an event comes that the event comes from the user or form the gui logic(add new items to the combo).
VIEWS ON THIS POST

82

Posted on:

Tuesday 23rd October 2012
View Replies!

when to dispose of widgets/objects

From what I've read, I'll have to dispose of SWT widgets manually as they are calls to native resources. When should I dispose of widgets and are there other resources I need to be disposing of manually
VIEWS ON THIS POST

63

Posted on:

Tuesday 23rd October 2012
View Replies!