Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Gradient in an applet

import java.awt.*;
import java.applet.*;
import javax.swing.*;

public class JavaAppDe extends JApplet {
	
    public void init() {
    }
	
    public void paint (Graphics g) {
        super.paint(g);
	int base = 0;
	while(base<255) {
		g.setColor(new Color(base,base,base));
		g.drawLine(0,base,255,base);
		base++;
	}
    }
}

Use AWTEventListener for Debugging Events

For debugging to help find where events are coming from, one can use AWTEventListeners to listen to all events that are dispatched. The following code shows an example that will print out the source component for all key events that are processed by AWT (and consequently Swing):

        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
 
            public void eventDispatched(AWTEvent event) {
                System.out.println(event.getSource());
 
            }
 
        }, AWTEvent.KEY_EVENT_MASK);

Enable custom FocusTraversalPolicy in Swing

Every container in Swing/AWT can be given a FocusTraversalPolicy with:
public void setFocusTraversalPolicy(FocusTraversalPolicy policy)

Remember to enable the container as a FocusCycleRoot:
setFocusCycleRoot(true)

Together it looks like:
container.setFocusTraversalPolicy(myCustomPolicy);
container.setFocusCycleRoot(true);
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS