Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help installing a custom look and feel

0 views
Skip to first unread message

Edd Grant

unread,
Mar 22, 2005, 1:12:43 PM3/22/05
to
Hi,

As part of a current Uni assignment I am required to write a
customised Look and Feel(LAF) and implement it in a java app. So far I
have taken an existing LAF and *cough* modified it by renaming all the
class instances and package paths to a package I have created for it.
The idea was to first get this working effectively (i.e. to be applied
to the app GUI) so I could begin to truly modify it and make my own
LAF.

I currently have a combobox which is populated (manually at the moment
from a Vector) with the available system LAFs. On the mouseClicked
event of a JButton the LAF is changed using the Vector content.

This is the code which populates the combo box data Vector called
"lookAndFeels":


lookAndFeels.add(UIManager.getSystemLookAndFeelClassName());
lookAndFeels.add("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
lookAndFeels.add(UIManager.getCrossPlatformLookAndFeelClassName());
lookAndFeels.add("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
lookAndFeels.add("javax.swing.plaf.metal.MetalLookAndFeel");
lookAndFeels.add("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
// This (below) is the LAF that I have created
lookAndFeels.add("customlafs.abclookandfeel.ABCLookAndFeel");

When a jButton (jButton1) is clicked it calls the
UIManager.setLookAndFeel() function passing the relevant Vector
content according to the current selectino of the combo box. The code
for this is below:

void jButton1_mouseClicked(MouseEvent e)
{
if (comboLAF.getSelectedItem().equals("")) {
JOptionPane.showMessageDialog(null, "Error! Please make a
selection!");
}
else {
try {
UIManager.setLookAndFeel( (String)
lookAndFeels.elementAt(comboLAF.
getSelectedIndex()));
SwingUtilities.updateComponentTreeUI(this);
}
catch (Exception ne) {
JOptionPane.showMessageDialog(null, "Error! \n "
+ (String) lookAndFeels.elementAt(comboLAF.getSelectedIndex())
+ " is not available on this system");
}
}

The problem I am having is that while I can access all the standard
LAFs (Windows, Motif etc) I don't seem to be able to *install* my
custom one (customlafs.abclookandfeel.ABCLookAndFeel). Each time I try
to do so it throws the exception in the code above. I imagine this is
because I have to first "install" the LAF before I can use it? I have
searched and searched for documentation relating to this problem but
can't find a definitive answer.

From looking at the UIManager functions I found the
addAuxiliaryLookAndFeel() method which takes a LookAndFeel object as
an argument. I thought this would work but because LookAndFeels are
abstract classes I can't make an instance of my custom LAF to pass as
an argument to this function. As a result I am totally stumped. To be
honest I'm not even sure if this is the right function or approach to
take.

If anyone has any insight as to how I can make my custom LAF available
for use in the App GUI I would be very grateful to hear your thoughts!
I have tried to provide all the necessary info but if I have left
something out please don't hesitate to ask me for it.

Cheers,

Edd
http://www.mredd.co.uk
email:edd@nospam:mredd.co.uk
(remove nospam: to email me)

Thomas Fritsch

unread,
Mar 22, 2005, 1:48:44 PM3/22/05
to
You should do
ne.printStackTrace();
here, or at least
JOptionPane.showMessageDialog(null, ne.toString + "! ...", ...);
in order to know exactly what exception class (and with what reason
message) was thrown. Remember, UIManager.setLookAndFeel(String) can
throw 4 different kinds of exception.
Otherwise, you end up in hopeless guessing about what went wrong.

> JOptionPane.showMessageDialog(null, "Error! \n "
> + (String) lookAndFeels.elementAt(comboLAF.getSelectedIndex())
> + " is not available on this system");

> }
> }
>
> The problem I am having is that while I can access all the standard
> LAFs (Windows, Motif etc) I don't seem to be able to *install* my
> custom one (customlafs.abclookandfeel.ABCLookAndFeel). Each time I try
> to do so it throws the exception in the code above. I imagine this is
> because I have to first "install" the LAF before I can use it? I have
> searched and searched for documentation relating to this problem but
> can't find a definitive answer.
>
> From looking at the UIManager functions I found the
> addAuxiliaryLookAndFeel() method which takes a LookAndFeel object as
> an argument. I thought this would work but because LookAndFeels are
> abstract classes I can't make an instance of my custom LAF to pass as
> an argument to this function. As a result I am totally stumped. To be
> honest I'm not even sure if this is the right function or approach to
> take.
>

May be you missed to call one of the UIManager.installLookAndFeel(...)
methods before calling UIManager.setLookAndFeel(...) .

> If anyone has any insight as to how I can make my custom LAF available
> for use in the App GUI I would be very grateful to hear your thoughts!
> I have tried to provide all the necessary info but if I have left
> something out please don't hesitate to ask me for it.
>
> Cheers,
>
> Edd
> http://www.mredd.co.uk
> email:edd@nospam:mredd.co.uk
> (remove nospam: to email me)


--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

e...@mredd.co.uk

unread,
Mar 28, 2005, 5:11:39 AM3/28/05
to
Hi Thomas,

Thanks for your response. The stack trace print shows the following:

java.lang.InstantiationException
at
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:308)
at java.lang.Class.newInstance(Class.java:261)
at javax.swing.UIManager.setLookAndFeel(UIManager.java:424)
at DemoFrame1.jButton1_mouseClicked(DemoFrame1.java:128)
at DemoFrame1_jButton1_mouseAdapter.mouseClicked(DemoFrame1.java:155)
at
java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:212)
at java.awt.Component.processMouseEvent(Component.java:5103)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3207)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

I'm pretty sure this is because the ABCLookAndFeel class is an Abstract
class so it is not possible to make an instance of it. This being the
case I am stumped again as I don't know how else to do this. Having
looked at the default JVM LAFs they are also all abstract so I'm not
sure what to do here.

If anyone has any ideas they would be appreciated.

Cheers,

Edd

e...@mredd.co.uk

unread,
Mar 28, 2005, 6:26:03 AM3/28/05
to
I have resolved the issue by taking a different approach. I am now
subclassing the Metal LAF and making changes as necessary, this
actually seems more managable as I am only required to override the
necessary bits and pieces!

Cheers.

E

0 new messages