File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Swings + Event handeling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swings + Event handeling" Watch "Swings + Event handeling" New topic

Swings + Event handeling

Remi Dubey

Joined: Jun 12, 2001
Posts: 8
hi folks.....
pl. guide me in implementing foll:
i hv JDesktopPane(in JFrame) wch has a Jmenubar and Jtooltip
attached to it. The tooltip consists of cut, copy and paste buttons. The Jmenubar has many menuitems wch invoke diff JDialog boxes. each of these dialog boxex hv many textfields and comboboxes.
i want tht whtevr value i select from any of the textfield or comboboxes i shd be able to copy it, cut it and paste it anywhere esle in my whole GUI.
pl reply fast....

Amit Agarwal
Ranch Hand

Joined: May 09, 2001
Posts: 92
Hi Remi,
Firstly is it a Jtooltip or a JToolBar, I hope it is a JToolBar.
Now, for the cut copy paste operation, make a String variable(buffer) in your main class. It maybe a static if you don't have a reference of the main class in your respective dialogs. Initially keep this buffer=null. If a call to paste is made simply check the value of the buffer and if it is not null, paste the tect in the buffer. If a cut or copy is called make a copy of the current text feild's text in the buffer and for the cut, empty the text field's content.
Now, this is the tricky part. Whenever anybody clicks on one of the toolbar buttons, the focus will move that button and you will lose the reference to the textfield you were in. To do this, either you use action keys like ^C ^X ^V and associate them thru keylisteners with the respective textfields and remove the toolbar.
Or make another static reference in the main class that will hold a reference to the last component visited. Associate focus listeners with all your components (text fields . combobox etc.)in all your dialogs where you want this functionality. when you gain focus to a textfield assign a reference of this text field to the static reference and when you lose the focus assign it to null. When any of the toolbuttons is pressed use this reference if it is not null. also you can use JTextField.grabFocus() to divert the focus back to the textfield from the toolbutton.
I agree. Here's the link:
subject: Swings + Event handeling
jQuery in Action, 3rd edition