Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

gui won't show up  RSS feed

 
mike weister
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a gui which usess a few jpanels, scrollpanes, jtables. One of my classes extends jpanel, and on this component I want to draw some lines, rectangles,etc.
When I run this on my windows machine (in a jar file) the gui comes up perfectly. However when include the code to draw the rectangle, it does not display.

all the code, works great, breaks when I add this to the constructor to GraphicalView class:
graphics2D.draw3DRect(30,30,50,50,true);

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PleaseUseCodeTags.

You have to do the painting in the paint method of your own component, you can't get and store the graphics object. Please see here.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your code to put it in code tags if you want anyone to read it!

Your method doesn't work because you're using the Graphics instance in the wrong way. You need to override the paintComponent method of the JPanel and use the Graphics instance that is passed to that method.

Edit to say: Or the easier way is just to add a JLabel to your panel with your desired text.
 
mike weister
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that was just what I needed. I was thinking there was something I needed to override, but could not find the documentation. works like a charm now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!