March 2nd, 2000, 11:43 AM
hi everybody
I don't know how to change look and feel in java program. I tried smth like that
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
but in both cases the main frame looked the same.
Here is my whole source
class MyClass {
public static void main(String args[]) {
JFrame mainFrame = new JFrame("Yabba dabba doo");
mainFrame.addWindowListener(new WindowAdapter(){
public void windowClosing( WindowEvent e) { System.exit(0); }
});
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Error loading L&F: " + exc);
}
mainFrame.pack();
mainFrame.setSize(600,200);
mainFrame.setVisible(true);
}
}
Please somebody help me with an example how to change between L&F.
thnx Seyo
Erik
March 3rd, 2000, 01:38 AM
Here's the program from my Dietel & Dietel book Java How to Program ( Hope they don't mind )
Notice the looks array that gets the look and feel info then sets it. I think thats what your missing.
// Fig. 13.9: LookAndFeelDemo.java
// Changing the look and feel.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LookAndFeelDemo extends JFrame {
private String strings[] = { "Metal", "Motif", "Windows" };
private UIManager.LookAndFeelInfo looks[];
private JRadioButton radio[];
private ButtonGroup group;
private JButton button;
private JLabel label;
private JComboBox comboBox;
public LookAndFeelDemo()
{
super( "Look and Feel Demo" );
Container c = getContentPane();
JPanel northPanel = new JPanel();
northPanel.setLayout( new GridLayout( 3, 1, 0, 5 ) );
label = new JLabel( "This is a Metal look-and-feel",
SwingConstants.CENTER );
northPanel.add( label );
button = new JButton( "JButton" );
northPanel.add( button );
comboBox = new JComboBox( strings );
northPanel.add( comboBox );
c.add( northPanel, BorderLayout.NORTH );
JPanel southPanel = new JPanel();
radio = new JRadioButton[ strings.length ];
group = new ButtonGroup();
ItemHandler handler = new ItemHandler();
southPanel.setLayout(
new GridLayout( 1, radio.length ) );
for ( int i = 0; i < radio.length; i++ ) {
radio[ i ] = new JRadioButton( strings[ i ] );
radio[ i ].addItemListener( handler );
group.add( radio[ i ] );
southPanel.add( radio[ i ] );
}
c.add( southPanel, BorderLayout.SOUTH );
// get the installed look-and-feel information
looks = UIManager.getInstalledLookAndFeels();
setSize( 300, 200 );
show();
radio[ 0 ].setSelected( true );
}
private void changeTheLookAndFeel( int value )
{
try {
UIManager.setLookAndFeel(
looks[ value ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}
catch ( Exception e ) {
e.printStackTrace();
}
}
public static void main( String args[] )
{
LookAndFeelDemo dx = new LookAndFeelDemo();
dx.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
private class ItemHandler implements ItemListener {
public void itemStateChanged( ItemEvent e )
{
for ( int i = 0; i < radio.length; i++ )
if ( radio[ i ].isSelected() ) {
label.setText( "This is a " +
strings[ i ] + " look-and-feel" );
comboBox.setSelectedIndex( i );
changeTheLookAndFeel( i );
}
}
}
}
/**************************************************************************
* (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
March 5th, 2000, 02:43 AM
thnx man, great help!!