Re: Missing Graphics in JComponent or JPanel

From: A. Bolmarcich (aggedor_at_earl-grey.cloud9.net)
Date: 11/30/04


Date: Tue, 30 Nov 2004 00:00:58 -0000

On 2004-11-29, ThomasH <henrymot@any.net> wrote:
> Hi,
>
> I face a strange problem, which bugged me already back than with
> AWT, Swing seems to have the same issue:
>
> I extend JPanel and in the constructor I try to calculate
> its preferred size. This size is related to size of used
> fonts. Thus I do:
>
> Font f;
> FontMetrics=fm;
> construct()
> {
> f=new Font(something);
> setFont(f);
> Graphics g=getGraphics();
> if (g==null) {
> System.out.print("No graphics in contr!!");
> // add ad hoc values to dimension
> } else {
> fm=g.getFontMetrics()
> // perf Form calculations
> }
> }
>
> Now later I can (or rather must) obtain fm and calculate
> the values in the paintComponent();
>
> getGraphics() returns null silently, no exception is
> being thrown. Browsing through the Swing information
> 'booklets,' such as Geary 1200 pages, or the Manning
> Swing 2nd edition 1000 pages yields no help.
>
> What do I do wrong? :-)

What you are doing wrong is trying to get information about a native
screen resource associated with a component before the component's
peer has been created. You should override the addNotify() method
of your extension of JPanel with something like

  public void addNotify() {
    super.addNotify();
    FontMetrics fm = getFontMetrics(getFont());
    // calculate the size using the FontMetrics
  }

Geary explains it with the FontSelectionPanel example in "Graphic
Java 1.1 Mastering the AWT".



Relevant Pages

  • Re: New applet project
    ... A web start application (or applet) ... object moving in a window; I have also some text attached to this work ... Note that (java.awt.)Canvas is part of the AWT, ... looking to Swing for good GUI's. ...
    (comp.lang.java.help)
  • Re: Frustrated by AWT..need some help
    ... every confidence that Matisse can use normal J2SE layouts, so it should be possible to write this code* using Matisse ... between Swing and AWT yet, and the replies to this thread have done little to help. ... AWT based GUI Development, come from the AWT. ... The major difference is in the set of *GUI* components offered by each. ...
    (comp.lang.java.help)
  • Re: Swing better than Awt?
    ... Swing is based on AWT. ... The main difference between AWT and Swing is that Swing paints components by itself while AWT let the native system paint them. ... private ActionListener listener = new ActionListener{ ... public void actionPerformed(ActionEvent e) { ...
    (comp.lang.java.programmer)
  • Re: Swing vs .NET
    ... I didn't know that Swing is a set of AWT. ... "Java, used to develop a business data driven application rich of GUI, ... When you compare Javaworld to .Net, ...
    (comp.lang.java.gui)
  • Re: JScrollPane in Applet
    ... The convention for class names in Java is to have ... Note that Applet is an AWT based class (even though ... ..but both JTable and JScrollPane are part of Swing. ... Swing and AWT ...
    (comp.lang.java.gui)