My application allows the user to draw shapes. Say I've got a rectangle (subclass of Rectangle2D.Double) drawn, now I want to allow the user to double click on the rectangle and a text field with flashing cursor should appear. The user can type in the desired text and pressed enter or click outside the shape to stop editing text. The text gets displayed inside the shape.
I was wondering if anyone could point me in the right direction for achieving the result described. For a start I don't want to get preoccupied with the flashing cursor, I just jneed the editable 2D text... My own ideas include creating a new inner class inside the shape class that would represent the text area and would extend the KeyAdapter class... or extend JTextArea... I thought about it but I'm not convinced to any of my ideas and I'm not even completely sure how I could go about implementing them. Would really appreciate some help.
Extending JTextField or JTextArea sounds like a good idea, if those two classes don't provide the necessary functionality out of the box. Being Swing components, they can be positioned at a position of your choosing. And you can add a KeyListener to them in order to provide whatever behavior is desirable in response to keystrokes.
Ok thanks. I'm still not sure though about the specifics of the implementation... for example, how do I make the text area appear inside the desired shape? I mean, I don't think I can just add a JTextArea to a Rectangle2D object?
Ok I tried but for some reason it does not work. I'm posting below my DrawingCanvas class which is where all the shapes are drawn and where the JTextArea should also be drawn.
So in the mouseClicked() method I'm trying to display a JTextArea anywhere (for now) on the drawingCanvas object but it doesn't appear when I double click (and I've got a shape selected). Is there something wrong with this call? The task looks pretty simple, I must be not seeing something obvious...
Suggestions unrelated to your original problem but which I tripped over on reading your code:
1) Don't mix AWT components (i.e., TextField) with Swing components (everything else).
2) I'd rename Shape to be some other name as it conflicts with the Shape interface in the AWT library.
3) Don't declare your enum as several static inner classes. Use one single enum in its own file.
Hopefully more to come,... but it's a lot of code.
4) Simplify your code so that the current code is for testing and solving your current problem only and that's it. Get rid of all unrelated code. This unrelated code may be necessary to the final product sure, but it's only getting in the way of your and our understanding of the problem at hand.
Wow, you guys are fast with replying!
Ulf, I'll try it out in a sec.
Pete, thanks for the suggestions. I was also wondering... don't you think it's a little bit weird that drawing lines is also handled by the RectangularShape class? I had some serious problems trying to think of a good design for handling the shapes... I need all shapes to share a common interface since all of them are stores in one ArrayList (so that they can all be painted together). But I'm thinking maybe I should have two or more lists from which different kinds of shapes would be drawn....
this would mean though that I would have to check for different classes when handling shapes (because, for example, a line would not have a getCenter() method and rectangular shapes would) and I don't know which would be better - awkward design and a single list or more reasonable desing and more than one list containing different classes of shapes.
Ok, so this works, thank you. However, I'm still wondering if there's any way of creating text inside shapes using the graphics' drawString method? It sounds more appealing to me because I would be able to draw the string when the shape is drawn, so they would be bound together. In case of JTextAreas, I need to record the shape which the text area belongs to and the position of the text area... Maybe a text area could appear only for editing and then the text would be recorded and drawn using the drawString() method - do you thing this would be a good approach?
However, I'm still wondering if there's any way of creating text inside shapes using the graphics' drawString method? It sounds more appealing to me because I would be able to draw the string when the shape is drawn, so they would be bound together. In case of JTextAreas, I need to record the shape which the text area belongs to and the position of the text area... Maybe a text area could appear only for editing and then the text would be recorded and drawn using the drawString() method - do you thing this would be a good approach?
Yes, absolutely. The JTextArea is for entering the text, and then it should be drawn using drawString, and the JTextArea removed. I actually assumed that's what you had in mind.
Veronique Kus wrote:Maybe a text area could appear only for editing and then the text would be recorded and drawn using the drawString() method - do you thing this would be a good approach?
That's probably what I would have done. You can even use one single JTextField (or JTextArea for multilines) and reuse it. You create and add it at the start of the program but hide it (setVisible(false)). When you double click on a shape you move it to the right position (setBounds), show it (setVisible(true)) and use it. When done* get its text, store it with the shape, and hide the field again. The shape will then use drawString to draw its text.
* using a FocusListener. With JTextField you can also use an ActionListener to listen for enter presses.