This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Graphics and Swing?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Graphics and Swing??" Watch "Graphics and Swing??" New topic
Author

Graphics and Swing??

Desiree Morriseau
Greenhorn

Joined: Mar 18, 2002
Posts: 10
I am working on my first official project and I have drawn a grid using graphics and now I would like the user to click on a row and the whole row will be highlighted, Any ideas?
I have created this grid on a JPanel and then created a coordinate area which should map points on the grid to detect the click position within the grid, but the API is soo large I am not sure which class to implement to highlight the row and not paint right over top??
I thought since the master is here he would be so kind as to point me in the right direction?
Thanking you in advance, Desiree
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
Try using a JTable instead. It is the component of choice when dealing with grids. It is a bit unwieldy but very powerful.

HTH,


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
Desiree Morriseau
Greenhorn

Joined: Mar 18, 2002
Posts: 10
Thank You for the suggestion!
I agree that JTable would be one solution, except that the display form is not exactly what I am looking for. (eg - Headers etc.)
I thought a mouseEvent handler would contain a method call to do this passing in the graphics context as an argument to repaint a 'transparent' colr over the row without painting over top the text drawn within the cells??
Ivor Horton
Author
Ranch Hand

Joined: Mar 22, 2002
Posts: 67
I'm not quite sure what your 'rows' are. Are they just goemetry/test that you have draw using a graphics context? If so, you will need to define how you associate things together in a row, maybe just by where they are on the component on which you have drawn them. You also need to decide how to highlight a row, maybe by changing the color. In this case use a separate variable to store the color for each row, then you can change the color for a row by changing one value.
You will need to handle the MOUSE_CLICKED event (in a MouseListener - you can use the MouseAdapter class as a base), get the cursor location, and decide from that which row should be highlighted. You will also need to remember which row is highlighted so you can unhighlight it when necessary.
It is much easier to code highlighting components as you don't need to figure out which component was clicked. No messing about with the cursor position You might therefore consider putting each row in a separate JPanel, then you just need to detect a click on a particular panel which you can do easily by having a serate listener for each panel. This will tell you directly which row to highlight.


Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
You can create headers with a JTable. They would look like those on Excel. Alternatively, you can just set the first row with data and then make it uneditable in you tablemodel.
HTH,
Desiree Morriseau
Greenhorn

Joined: Mar 18, 2002
Posts: 10
Thanks for the suggestion, unfortunately this will not work either as I would have to draw(paint) for each panel then but you have given me an idea about how to determine where the cursor is! I am convinced that the drawing of the lines for each column and row is the way to go for this one, just worried about how to highlight? I like the idea of using a variable to contain the color value, and toggling between the two, I am afraid of it overpainting though? Do you think this will be a problem?
Ivor Horton
Author
Ranch Hand

Joined: Mar 22, 2002
Posts: 67
Repainting should be no problem. Since you want to highlight a row in response to a mouse click, just change the color for the elements to be highlighted, then repaint the panel by calling its repaint() method.
If you were highlighting in response to moving the cursor, you could not afford to be repainting the panel each time so you would want to redraw just the highlighted elements. In this case you would need to code it differently, probably doing the redraw by calling getGraphics() for the panel in the event handler and using that to redraw the elements in the new color.
Desiree Morriseau
Greenhorn

Joined: Mar 18, 2002
Posts: 10
Thank you very much, this sounds like a very good solution. Exactly what I was looking for.
BTW I am honored to have your input on this!! What a lucky Day.
Desiree
[ March 26, 2002: Message edited by: Desiree Morriseau ]
 
 
subject: Graphics and Swing??
 
Similar Threads
Tile constraints
Grids with selectable rows in JQuery
table row selected issue
Problem in hiding the row of data
Could you please help me?