System look and feel error

S

Sardaukary

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();
}
}
 
T

Thomas Hawtin

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
 
Z

zero

[...] 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.
;-)
 
R

Roedy Green

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 );
}
}
 
T

Thomas Hawtin

zero said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top