File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing - Calling repaint in actionPerformed method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing - Calling repaint in actionPerformed method" Watch "Swing - Calling repaint in actionPerformed method" New topic
Author

Swing - Calling repaint in actionPerformed method

Aditya Anil Sharma
Greenhorn

Joined: Jun 15, 2012
Posts: 9
Hi, I'm new to Swing and was going through Head First and came across an example in RMI chapter. Cutting short, I'm trying to create a rectangle on click of a button. My approach is:
1) Create a Frame
2) Create a JPanel (say panel)
3) Put in it a button and a customized JPanel (say myPanel)
4) On click of button, its actionPerformed() will be called. I'm calling repaint() method inside that which in turn should call paintComponent() where I'm drawing my rectangle
Problem is that the rectangle created is of VERY small size. It is obvious that somewhere I'm going wrong, so seeking your advice!

(I'm intentionally adding it to panel and not frame, as later in the more developed code, I'll have to send back panel through RMI. Also, please avoid the JComboBox at this moment as I'll look at it once my rectangle is created!!)



Regards
Aditya Sharma
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Probably a layout problem. You are not seeing the second panel. Remove myPanel from panel. Add panel to the frame with BorderLayout.NORTH. Add myPanel to the frame with BorderLayout.CENTER. See whether that helps.
Aditya Anil Sharma
Greenhorn

Joined: Jun 15, 2012
Posts: 9
Thanks for the reply Campbell!

Actually, I'm intentionally adding 'mypanel' to 'panel' and not frame because later on, the method go() will be called my some other class, which actually will be asking for panel. So, my go() method will return panel, stuffed with all the things (like button, myPanel, JComboBox etc).
(Hope you got what I was trying to say!! )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
There is something strange about passing lots of GUIs round like that.
But try adding a layout to the panel, and add the buttons and second panel in different locations.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
MyPanel has no components, so you need to set a preferredSize for its parent panel to work with
myPanel.setPreferredSize(new Dimension(200,200));

where 200,200 is anything you want.

also, you'll probably need to add this line
super.paintComponent(g);
Aditya Anil Sharma
Greenhorn

Joined: Jun 15, 2012
Posts: 9
Thanks for the replies friends.
I did some more cooking with the code, making 2 sub panels lie inside one main panel, with main having a BoxLayout. Added JComboBox and button in first sub panel and circle in second one (Did these words make any sense?! )
Just to clarify what I said, here is the code which I arrived at:


Now I'm expanding it so that the square below can have as many dots (in a proper fashion) as the number selected in JComboBox. Might come back again asking some more weird queries!! Really thanks for all your support.

Regards
Aditya Sharma
 
Don't get me started about those stupid light bulbs.
 
subject: Swing - Calling repaint in actionPerformed method