Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

This is a way to override the paint method using a factory and an anonymous class. This is opposed to creating a separate, named class that extends JFrame.

import javax.swing.*;
import java.awt.*;
 
public class Test {
    public static void main(String[] args) {
        new Test().run();
    }
    public void run() {
        JFrame frame = FrameFactory.getInstance();
        frame.setBounds(50, 50, 300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
class FrameFactory {
    private FrameFactory () {}
    public static JFrame getInstance() {
        return new JFrame() {
            public void paint(Graphics graphics) {
                Graphics2D g = (Graphics2D) graphics;
                Dimension size = getSize();
                g.clearRect(0, 0, (int)size.getWidth(), (int)size.getHeight());
                g.drawLine(0, 0, (int)size.getWidth(), (int)size.getHeight());
            }
        };
    }
}
 

Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  best/using_a_factory_jpanel.txt · Last modified: 2006/05/26 19:25 by 67.114.54.66 (tietyt)
 
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources
FitNesse Resources