Overriding updateUI() ?

If I extend a JPanel and override updateUI() I get a null pointer exception?

Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException

at ExtendedPanel.updateUI(Example.java:17)

at javax.swing.JPanel.<init>(JPanel.java:64)

at javax.swing.JPanel.<init>(JPanel.java:87)

at javax.swing.JPanel.<init>(JPanel.java:95)

at ExtendedPanel.<init>(Example.java:10)

at Example$1.run(Example.java:37)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

class ExtendedPanelextends JPanel

{

protected JLabel l;

public ExtendedPanel()

{

super();

this.l =new JLabel("Whatever");

this.add(this.l);

}

publicvoid updateUI()

{

this.l.setText("Updated");

super.updateUI();

}

}

Am I doing something wrong? :)

[1865 byte] By [d11wtqa] at [2007-11-26 17:48:45]
# 1
> Am I doing something wrong?The error message tells you the problem. You have a null variable, which mean the component hasn't been created yet.Don't override updateUI. There is no need to.
camickra at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 2
updateUI is called from the super constructor. As said before, find another place to put your functionality - don't override this method.
kirillga at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 3

To explain my reasoning for the method a little better, I have written a Date picker component which I am going to release under LGPL because it's useful and others I've found were commercial licenses. Anyway, the JTable in the calendar dispatches events when the user clicks on a cell, which in turn updates the model (i.e. the java.util.Calendar object). I've made it possible for developers to modify the java.util.Calendar object itself for a little more flexibility, but in order for the view (a selection of ComboBoxes Spinners and a JTable) to graphically show those results I created a redraw() method which works nicely. My desire to call the method updateUI() is purely cosmetic since it's probably what developers would first try to do before they read the documentation. Am I stuck with my redraw() method? :)

d11wtqa at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 4

> That was exactly the problem I solved it about 10 minutes ago and treated myself to a break.

No, you have no reason to override or manually invoke updateUI(). The method is time consuming because it it used for LAF changes.

Custom painting is done in paintCompnent() and you tell the component to repaint itself by using the repaint() method.

camickra at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 5
Ah ha, thank you :)
d11wtqa at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 6

Can I just double check I understood you? :)

class ExtendedPanel extends JPanel

{

protected JLabel l;

public ExtendedPanel()

{

super();

this.l = new JLabel("Whatever");

this.add(this.l);

}

public void paintComponent(Graphics g)

{

this.l.setText("Updated");

super.paintComponent(g);

}

}

This works as I want (obviously that code is purely for demonstration!) but have a gone a round-about way of doing it?

d11wtqa at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 7

I don't really understand what you are doing.

The updateUI() method is invoked when a component is created or when a LAF change is made, so basically it should only ever be invoked once in a normal application.

The paintComponent() method is invoked whenever the Swing PaintManager determines that a component needs to be repainted, (ie. if you minimize then restore the frame), so it will be invoked multiple times in the life of the application.

I have no idea why you are trying to set the value of a label in either of those methods.

camickra at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...
# 8

LOL... I'm not. It's just me being an idiot and giving a **** example.

See this calendar: http://www.w3style.co.uk/~d11wtq/calendar_demo.png

When you change the month in the JComboBox the grid needs updating. Currently I can fire all that myself. However, I've given the ability for the developer using the date picker to manually change the values in the Calendar object (java.util.Calendar). In which case, the table does not know it needs repainting. There are a few things that need to be done before it will repaint successfully (although I'm looking at improving the code to avoid these "few things") so I wanted to run a few methods before calling the super class' usual painting operations.

I think I may be able to avoid having to do all this anyway by refactoring the code which sets the values in the table :)

d11wtqa at 2007-7-9 5:01:09 > top of Java-index,Desktop,Core GUI APIs...