Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Java has got this newbie stumped...

Java has got this newbie stumped...
Thread Tools
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 6, 2001, 05:41 PM
 
I have recently started learning JAVA and starting to have fun with it. But I've run into a little problem, at least for me being such a beginner :-)

I have a grid of JTextFields that each contain a string. I would like to be able to mouseclick in a JTextField and have it display the corresponding string. Kind of like choosing a box, clicking it, and have its contents display in a separate window.

So far I am stuck on how to do the above using the mouse only. I know all about adding an addMouseListener to each JTextField and having a class for event handling. But what method do I use that achieves the above?

I already went through the Java docs but could only come up with using the keyboard enter key and the getActionCommand. Is there a similar method for the mouse listener?

Any hints are much appreciated!

     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 6, 2001, 05:45 PM
 
Oops. Meant to mention that I am using Swing.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 6, 2001, 08:34 PM
 
Hey-

I'm a little sketchy on exactly what you want to do, but I think I kind of know what you want to do. If you want to click on a JTextField with a string in it, and then have it displayed in another JTextField off somewhere else, here's what I think you want to do.

DISCLAIMER: I did not test any of this code, this is just off the top of my head. If you have any problems, email me/post again and I'll try and help out.

Code:
... beginning of class, implements java.awt.event.MouseListener ... JTextField source = new JTextField("This is only a test"); JTextField display = new JTextField(); display.addMouseListener(this); // use this class as the listener ... add them to the window or applet, etc ... public void mouseClicked(MouseEvent e) { /* gets the source JTextField for the event. You probably want *much* * better error checking to make sure that it really *is* a JTextField */ JTextField src = (JTextField)e.getSource(); // getSource() works for // any AWT/Swing event /* this will set the display JTextField's text to that of whichever * JTextField was click on/sent the event */ display.setText(src.getText()); }
Hope this helps!
mccullocht

[This message has been edited by mccullocht (edited 05-06-2001).]
     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 7, 2001, 05:48 AM
 
Thank you. That was exactly what I was looking for!

A separate question but also related:

If you have say a 5x5 gridLayout of JTextFields and you mouseclick in a textfield is it possible to have the gridLayout coordinates of the textfield returned?

For example clicking the top right textfield woudl give you (5,1) bottom right would be (5,5) etc and I don't mean the x,y coordinates of where the mouseclick occured.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 7, 2001, 07:23 AM
 
Originally posted by Quantum8:
If you have say a 5x5 gridLayout of JTextFields and you mouseclick in a textfield is it possible to have the gridLayout coordinates of the textfield returned?

For example clicking the top right textfield woudl give you (5,1) bottom right would be (5,5) etc and I don't mean the x,y coordinates of where the mouseclick occured.[/B]
Hmmm...that one would be a little more tricky. It the contents of the JTextField's is fixed, then you could base it on the contents of the field. For instance:

Code:
Point lastClicked; // coordinates of last text field clicked on JTextField aTextField = new JTextField("mmmm...chocolate..."); //upper right aTextField.setEditable(false); // so the user can't change the contents public void mouseClicked(MouseEvent e) { JTextField src = (JTextField)e.getSource(); if (src.getText() == "mmmm...chocolate...") lastClicked = new Point(5, 1); }
Of course, this would get kind of complicated, you'd have to check to see if the point had changed. A simple way to handle this would be to use a notification mechanism such as java.util.Observable and java.util.Observer. Check it out in sun's java docs. Note that you can also download the documents available there, but that requires either a fast connection or a lot of patience, it comes in at ~30 MB.

mccullocht
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:59 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2