• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing Look and Feel not working  RSS feed

John Palmieri
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem with the look and feel methods. I have a applet that can run either as an applet or an application. If it is run as a application, The code puts the applet into a JFrame.
This works, but the look and feel always seems to be metal. I want the look and feel to be windows. How can I accomplish this ?
This is code that was built with Jbuilder 5 with some additions
from me trying to accomplish what I want. when I switch to Jbuilders UI designer the screen pick up the windows look and feel.
I am using JDK 1.3.
Here is the code...
package untitled29;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Applet2 extends JApplet {
boolean isStandalone = false;
JButton jButton1 = new JButton();
JComboBox jComboBox1 = new JComboBox();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
/**Construct the applet*/
public Applet2() {
/**Initialize the applet*/
public void init() {
try {
catch(Exception e) {
/**Component initialization*/
private void jbInit() throws Exception {
jButton1.setBounds(new Rectangle(110, 39, 103, 27));
jComboBox1.setBounds(new Rectangle(87, 103, 156, 21));
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jComboBox1, null);
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
/**Main method*/
public static void main(String[] args) {
Applet2 applet = new Applet2();
applet.isStandalone = true;
JFrame frame;
frame = new JFrame() {
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
public synchronized void setTitle(String title) {
String laf = UIManager.getSystemLookAndFeelClassName();
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);
Frame[] frames = frame.getFrames();
for(int f = 0; f < frames.length; f++) {
Window[] windows = frames[f].getOwnedWindows();
for(int w = 0; w < windows.length; w++) {

frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
Matt Hansen
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not worked with applets before. But, when I run this it runs the init method instead of main. Maybe this is normal for applets? Anyway, when I run your code in jBuilder5 the main method is never called so the look and feel is never set.
[This message has been edited by Matt Hansen (edited October 03, 2001).]
glenn mason
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

main is the starting point for an application; applets have a couple of automatically called methods: init, start, stop, destroy. You could put the PLAF code in a method and call it from both main and init.

getSystemLookAndFeelClassName() returns the Metal classname if it can't find a System one. Whether a system LAF is available depends on your Java install, maybe. Try UIManager.getAuxiliaryLookAndFeels() (returns an array), then getName on each of the LookAndFeel objects in the array. Just to see what LAFs you are getting back?

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!