Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing JTextField on Graphics Context  RSS feed

 
sachin pathak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have following problem.
I have a method which is giving me graphics2d context of the application to draw onto. Now I want to draw a rectangle and also I want to place a Swing JTextField inside this rectangel. I am stuck up with this for last 2 days. If anybody can help me out. The rectangle can be drawn pretty easily, it is just the JTextFiels that is giving me lot of trouble.
To generalise, I want a way to draw a swing component on a graphics context a.k.a. without using any layout manager etc.
Thanking in anticipation
Sachin Pathak
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Though this seems a really roundabout way of doing things...

If all you want to do is draw an image of the component, call the paint method of the component and pass in the graphics context...



You'll probably have to manually setBounds() on the component (and maybe some other things) to get this to work well.

Also, be aware that all this does is paints an image of the component on the graphics context... this *does not* produce a functioning component! You're going to have to get an actual reference to the container component if you want to do that...
 
sachin pathak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will clarify my requirements further. Actually my application has two modes, forst is viewer mode in which User just viewes thw data, and the other is dataentry mode. In the dataentry mode, user can enter the data. Now the logic for viewer is already written. I just want to check the mode of the application in the draw method, then if the mode is dataentry, instead of drawing a rectangle object I want to draw a JTextField so that user can enter the data in it.
Is there any way I can get the container of the Graphics Context I have with me ?
 
sachin pathak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Pruett:


Though this seems a really roundabout way of doing things...

If all you want to do is draw an image of the component, call the paint method of the component and pass in the graphics context...



You'll probably have to manually setBounds() on the component (and maybe some other things) to get this to work well.

Also, be aware that all this does is paints an image of the component on the graphics context... this *does not* produce a functioning component! You're going to have to get an actual reference to the container component if you want to do that...

I will clarify my requirements further. Actually my application has two modes, forst is viewer mode in which User just viewes thw data, and the other is dataentry mode. In the dataentry mode, user can enter the data. Now the logic for viewer is already written. I just want to check the mode of the application in the draw method, then if the mode is dataentry, instead of drawing a rectangle object I want to draw a JTextField so that user can enter the data in it.
Is there any way I can get the container of the Graphics Context I have with me ?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, after hearing what you really wanted to do, I think the best plan would be to use the exact same Swing GUI for both modes. Don't bother drawing widgets to prevent people from editing data. Just make a method on your panel that calls setEnabled() and setEditable() on all the components. Just pass true or false in based on the mode.
 
sachin pathak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Pruett:
OK, after hearing what you really wanted to do, I think the best plan would be to use the exact same Swing GUI for both modes. Don't bother drawing widgets to prevent people from editing data. Just make a method on your panel that calls setEnabled() and setEditable() on all the components. Just pass true or false in based on the mode.

Thanks Nathan,
I can't follow that plan because of some other requrements. In viewer mode we are having a concept called fields which are actually bound to the external XML data OR External Database (Something similar to Access reports connected to databse tables) So the fields can have formatting like it can be bold or it can have color, and a custome background(Which has been created by end user.) etc. Also there can be other Java2d components like rectangles, ellipses etc which needs to be very accutately rendererd for the printing purpose, the data might be displayed vertically etc. So We dont want to change the existing code much.
Anyway We had made some changes in the existing code so that I can get the instance of the JPanel so this problem has been alleviated.
Thanks for your timely help.
Sachin
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!