Quick question regarding Swing objects inside of Applet


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Quick question regarding Swing objects inside of Applet

  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, 09:21 AM
  2. Replies: 3
    Last Post: 07-23-2001, 12:47 PM
  3. Replies: 7
    Last Post: 06-26-2001, 07:29 PM
  4. Quick equivalent to VB "MsgBox" in Java / Swing?
    By Matthew Cromer in forum Java
    Replies: 3
    Last Post: 12-09-2000, 06:34 AM
  5. quick question
    By Nacho Fault in forum Enterprise
    Replies: 1
    Last Post: 03-27-2000, 06:23 PM

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center