Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing Look and Feel not working

 
John Palmieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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 {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(110, 39, 103, 27));
this.getContentPane().setLayout(null);
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) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
String laf = UIManager.getSystemLookAndFeelClassName();
try
{
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(frame);
} 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++) {
SwingUtilities.updateComponentTreeUI(frames[f]);
frames[f].validate();
Window[] windows = frames[f].getOwnedWindows();
for(int w = 0; w < windows.length; w++) {
SwingUtilities.updateComponentTreeUI(windows[w]);
windows[w].validate();
}
}

frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}
 
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
Greenhorn
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