DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    75

    Quick question regarding Swing objects inside of Applet

    I've included some code that demonstrates perfectly what I'm asking about. Everytime I try to add a swing object to an applet (JButton, JRadioButton, etc.) it only appears when the mouse moves over it. I need to know how to make it visible at all times. Thanks for any advice

    Here is the start of a project I'm working on...
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class SampleAppletGUI extends JApplet {
    
    	// applet window parameters
    	private final int WIDTH = 700;
    	private final int HEIGHT = 400;
    	private final int DISPLAY_HEIGHT = 150;
    
    	// swing variables
    	private JButton button1;
    
    	// double buffer variables
    	private Image buffer;
    	private Graphics b;
    
    	public void init(){
    
    		// instantiate double buffer objects
    		buffer = createImage(WIDTH, HEIGHT - DISPLAY_HEIGHT);
    		b = buffer.getGraphics();
    
    		// instantiate swing objects
    		button1 = new JButton("button1");
    		button1.setSize(100,25);
    		button1.setLocation(5, DISPLAY_HEIGHT + 5);
    
    		Container p = getContentPane();
    		p.setLayout(null);
    
    		p.add(button1);
    
    		// set size of applet window
    		setSize(WIDTH, HEIGHT);
    	}
    
    	public void paint(Graphics g){
    
    		// draw graphics onto buffer image
    		b.setColor(Color.RED);
    		b.fillRect(0,0,WIDTH, DISPLAY_HEIGHT);
    
    		// display buffer image to this graphics object
    		g.drawImage(buffer,0,0,this);
    	}
    
    	public void update(Graphics g){
    		paint(g);
    	}
    
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    62
    I think you might need to use g.paintChildren(); as the last line in paint() because it looks like you're painting an image over everything.

    Kind regards,
    Noel
    Efficiency is intelligent laziness

  3. #3
    Join Date
    May 2005
    Posts
    75
    Thanks for the reply. I did some looking through the API and found that Container's paintComponents() method does the trick

Similar Threads

  1. Quick DB Backup Question
    By Jim in forum Database
    Replies: 0
    Last Post: 02-12-2002, 08:21 AM
  2. Replies: 3
    Last Post: 07-23-2001, 11:47 AM
  3. Replies: 7
    Last Post: 06-26-2001, 06:29 PM
  4. Quick equivalent to VB "MsgBox" in Java / Swing?
    By Matthew Cromer in forum Java
    Replies: 3
    Last Post: 12-09-2000, 05:34 AM
  5. quick question
    By Nacho Fault in forum Enterprise
    Replies: 1
    Last Post: 03-27-2000, 05:23 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links