Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

System look and feel error

Thread view: 
Sardaukary@gmail.com - 11 Dec 2005 22:15 GMT
I have a very basic swing app with a button that when clicked adds some
text to a JTextArea.

It all works fine until I try to use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) to
give the app a Windows XP look and feel.

The program still functions but the button no longer depresses when you
click it.  Is this a bug or am I doing something wrong?

am using jre1.5.0_06  and  JDK 5

Here's some code to see if anybody else gets this.  After you click
Native the Add Text button doesn't animate.  Sorry about the formating,
it's late :)

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test extends javax.swing.JFrame implements ActionListener
    {
    JTextArea messageBox = new JTextArea(20,20);
    JButton addText = new JButton("Add Text");
    JButton changeThemexp = new JButton("Native");
    JButton changeThemecross = new JButton("X platform");

    public test()
        {

            super("Test");

            setDefaultCloseOperation(EXIT_ON_CLOSE);
            messageBox.setLineWrap(true);
            JScrollPane scrollPane= new JScrollPane (messageBox);
            addText.addActionListener(this);
            changeThemexp.addActionListener(this);
            changeThemecross.addActionListener(this);
            JPanel panel = new JPanel();
            panel.add(addText);
            panel.add(changeThemexp);
            panel.add(changeThemecross);
            panel.add(scrollPane);
            add(panel);
            pack();

            setResizable(false);
            setVisible(true);
        }

            public void addText (String texttoadd)
            {
                messageBox.append(texttoadd);
            }

        public void actionPerformed(ActionEvent evt)
            {
                if (evt.getSource()== addText)addText("Testing\n");
                if (evt.getSource()== changeThemexp)
                    try
                            {
                            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                            }
                            catch (Exception e) {};repaint();
                if (evt.getSource()== changeThemecross)
                    try
                            {
                            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                            }
                            catch (Exception e) {};repaint();
    }
                    ;

        public static void main(String[] args)
        {
        test gui = new test();
        }
    }
Thomas Hawtin - 11 Dec 2005 22:59 GMT
> It all works fine until I try to use
> UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) to
> give the app a Windows XP look and feel.
>
> The program still functions but the button no longer depresses when you
> click it.  Is this a bug or am I doing something wrong?

> public class test extends javax.swing.JFrame implements ActionListener

Not particularly relevant to the problem, but it's a bad idea to extend
class that you don't need to. Or break Java naming conventions.

>     {

Tabs don't work particularly well on Usenet (or most other places).

> [...switches UI in event listener...]

I can't test the code, because I don't have a Windows machine (well
there is a friend's 98 machine here, but without a monitor). I guess
that the problem here may be that you are switching UI within an event
listener.

Your listener tears down the old UI and installs the new one. Because
listeners are fired in reverse order to registration, the old
deregistered UI delegates will still be getting events. That will
probably cause some problems. If you change the UI again, the UI
listener will now have been registered after your listener.

Possibly the new UI will miss out on an event, because its listener,
even though registered, wont be fired in the current firing. Quite
possibly UI's don't always unusual initial properties of components.

Some of your problems may disappear by wrapping the setLookAndFeel
within an EventQueue.invokeLater. Just like you should do for main, but
for different reasons.

> [...] catch (Exception e) {};repaint();

Never drop an exception like that. At the very least,
exc.printStackTrace(); would alert you to something going wrong.

>         public static void main(String[] args)
>         {
>         test gui = new test();
>         }

You need the standard nonsense boilerplate here.

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() { public void run() {
                    Test gui = new Test();
        }});
    }

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

zero - 12 Dec 2005 00:38 GMT
Thomas Hawtin <usenet@tackline.plus.com> wrote in news:439cad68$0$1455
$ed2619ec@ptn-nntp-reader01.plus.net:

> > [...] catch (Exception e) {};repaint();
>
> Never drop an exception like that. At the very least,
> exc.printStackTrace(); would alert you to something going wrong.

Completely off topic, but I've had to drop an exception like that to
accomodate for errors in a jar I was using (and didn't have the time to
fix).  It was something like this:

int x = 0;

try
{
  x = classFromJar.getX();
}
catch(NullPointerException e)
{
  // do nothing, x stays 0
}

Repeating the never say never truism - just because I enjoy being pedantic.
;-)

Signature

Beware the False Authority Syndrome

Thomas Hawtin - 12 Dec 2005 01:41 GMT
> Completely off topic, but I've had to drop an exception like that to
> accomodate for errors in a jar I was using (and didn't have the time to
> fix).  It was something like this:

> [...]
>
> Repeating the never say never truism - just because I enjoy being pedantic.
> ;-)

You should have logged the exception with a low logging level. </pedant>

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 12 Dec 2005 01:41 GMT
>Here's some code to see if anybody else gets this.  After you click
>Native the Add Text button doesn't animate.  Sorry about the formating,
>it's late :)

I can't stand working with dirty code so I cleaned it up before
running it.  It works fine for me. There are no "animations", but the
L&F does change and text does add.. The L&F is subtle, just the
backgrounds of the buttons.

I renamed your variables a bit to avoid violating naming conventions.
Your ifs were nested properly. You should not put setVisible in a
JFrame constructor.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class Test extends JFrame implements ActionListener
  {
  JTextArea messageBox = new JTextArea(20,20);
  JButton addText = new JButton("Add Text");
  JButton changeThemeNative = new JButton("Native");
  JButton changeThemeCross = new JButton("X platform");

  public Test()
     {

     super("Test");

     setDefaultCloseOperation( EXIT_ON_CLOSE );
     messageBox.setLineWrap( true );
     JScrollPane scrollPane= new JScrollPane( messageBox );
     addText.addActionListener( this );
     changeThemeNative.addActionListener( this );
     changeThemeCross.addActionListener( this );
     JPanel panel = new JPanel();
     panel.add( addText );
     panel.add( changeThemeNative );
     panel.add( changeThemeCross );
     panel.add( scrollPane );
     add( panel );
     pack();

     setResizable( false );
     }

  public void addText( String texttoadd )
     {
     messageBox.append( texttoadd );
     }

  public void actionPerformed( ActionEvent evt )
     {
     if ( evt.getSource() == addText )
        {
        addText("Testing\n");
        }
     else if ( evt.getSource() == changeThemeNative )
        {
        try
           {
           UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName() );
           }
        catch ( Exception e )
           {
           e.printStackTrace();
           }
        repaint();
        }
     else if ( evt.getSource() == changeThemeCross )
        {
        try
           {
           UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName() );
           }
        catch ( Exception e )
           {
           e.printStackTrace();
           }
        repaint();
        }
     else System.err.println( "unknown event" );
     }

  public static void main(String[] args)
     {
     Test gui = new Test();
     gui.setVisible( true );
     }
  }

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.