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 / August 2007

Tip: Looking for answers? Try searching our database.

Swing Appearance Problems

Thread view: 
matt - 14 Aug 2007 21:19 GMT
Ok so this has been bugging me for a while now and I have no idea what
to search for or what else to do. Basically, I have had java installed
on this computer for a while and it worked fine. But a while back, my
GUIs started getting all weird. I am using the Swing components
(JFrame and such) and what is happening is everything is more spaced
out. I have tried reinstalling the JRE multiple times but the problem
does not fix itself. I have also tried clearing the Java cache through
the control panel. I know it is nothing in my program because when I
move the code to another computer everything looks fine.
This is a hard problem to explain but the best way I can do it is to
say that everything is spaced out in the frames. The text fields are
wider but the text size is the same. In the combo boxes, the items are
spaced apart farther. An analogy would be in HTML the cell padding
field is bigger but obviously there is no place to set this within
Java.
I hope this makes sense, any help would be appreciated.
Daniel Pitts - 14 Aug 2007 21:52 GMT
> Ok so this has been bugging me for a while now and I have no idea what
> to search for or what else to do. Basically, I have had java installed
[quoted text clipped - 12 lines]
> Java.
> I hope this makes sense, any help would be appreciated.

It makes perfect sense, not...
Please provide an SSCCE so that we can make an educated guess at what
might be going wrong.
matt - 14 Aug 2007 22:08 GMT
Unfortunately, I cannot really provide my code as an example, not that
it would make a difference anyways. What I have provided, is two
screenshots side by side of the program, here:

http://matthome.gotdns.com/pic.jpg

The one on the left is on this computer with the spacing really big
and the one on the right is on another computer. Notice that the
dropdown box is more compact, and that the buttons and textfields are
smaller as well. Those are all JComboBox, JButton, and JTextfield
respectively.

Thank you
Andrew Thompson - 15 Aug 2007 01:04 GMT
> Unfortunately, I cannot really provide my code as an example, ..

Why not?

>..not that it would make a difference anyways.

That is a classic statement, coming from someone
who does not yet understand the source of the problem.

>...What I have provided, is two
> screenshots side by side of the program, here:
>
> http://matthome.gotdns.com/pic.jpg

>From that screenshot (as well as your initial
description), I would suggest the source of
the problem is the Layouts that this code either
does not use, or misuses.

Other tell-tale signs for fragile GUI's are
to be (regularly) calling setPreferredSize()
or setSize() or setBounds() on components..

My guess is the 'other computer' is still using
a JRE/screen resolution ..whatever that is suitable
for the original GUI, while something in the
problem PC has changed.

The fix is to reimplement the layouts properly.

Andrew T.
Andrew Thompson - 15 Aug 2007 01:07 GMT
> Unfortunately, I cannot really provide my code as an example,

Note also that Daniel never suggested you post
'your code', but an SSCCE - which is a very
different thing.  Read more here..
<http://www.physci.org/codes/sscce.html>

Andrew T.
matt - 15 Aug 2007 01:58 GMT
Ok. Here is an example of the problem. I wrote a short program to
display a frame and ran it on 2 separate computers, with the same JRE
installed. It happens to be 1.5.0_01.

The code for the program is here: http://matthome.gotdns.com/DoesStuff.java
and the screen shot results are here http://matthome.gotdns.com/sidebyside.jpg

The reason I feel like it is a problem with the JRE rather than my
code is because commercial applets such as Yahoo games and other
things like that have the same problem. Also, when producing a small
program such as this, the problem is still there.

Matt
Andrew Thompson - 15 Aug 2007 04:15 GMT
>Ok. Here is an example of the problem. I wrote a short program to
>display a frame and ran it on 2 separate computers, with the same JRE
>installed. It happens to be 1.5.0_01.
>
>The code for the program is here: http://matthome.gotdns.com/DoesStuff.java

Yes.. there it is, just like I warned against in my first post..
 setBounds(0,0,200,100);

Try this code on both machines, I am guessing it
will look slightly different on each PCs, but should
work well for either.

<sscce>
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import javax.swing.border.EmptyBorder;

public class DoesStuff extends JFrame{

 public DoesStuff() {
   super("Frame");

   JPanel layout = new JPanel( new FlowLayout() );

   // I will want to use a Border, so will
   // .. use the panel above..
   // setLayout(new FlowLayout());
   setContentPane( layout );

   //setBounds(0,0,200,100);
   layout.add(new JButton("Button1"));
   String[] stuff = {"Option1","Option2","Option3"};
   layout.add(new JComboBox(stuff));

   // add spacing around the panel..
   layout.setBorder( new EmptyBorder(10,50,10,50) );

   setDefaultCloseOperation(EXIT_ON_CLOSE);

   // packing a layout will both validate
   // it, and change the root component to the
   // size it *needs* *to* *be*.
   pack();

   // if you insist* on calling setBounds/setSize,
   // do it after the call to pack or validate.

   // setSize( 200,100 );

   // though note that such 'spacing' can best
   // be achieved by adding an EmptyBorder, like above

   setLocationRelativeTo(null);

   setVisible(true);
 }

 public static void main(String[] args) {
   Thread t = new Thread() {
     public void run() {
       new DoesStuff();
     }
   };
   SwingUtilities.invokeLater( t );
 }
}
</sscce>

...
>The reason I feel like it is a problem with the JRE rather than my
>code is because commercial applets such as Yahoo games and other
>things like that have the same problem.

There are lots of extremely poor layouts, mostly those
designed in IDE's by people that think they can drag
components anywhere they like and assume that will
'just work' for anyone else's VM version, screen
size/resolution, default font face & size, PLAF..

>...Also, when producing a small
>program such as this, the problem is still there.

Try the altered code.  Note that I do not actually set
the size of any component in that code, as it is.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

matt - 15 Aug 2007 07:21 GMT
Knute:
 No, both operating systems are the same. XP Pro w/ SP2 installed. I
am not sure if I am able to upgrade to 1.6, I was having trouble
finding a link.

Andrew:
 The code you provided was correct syntactically but didnt fix the
appearance problem. The only difference was that the button and combo
box were centered in the frame.

Any other ideas?
Andrew Thompson - 15 Aug 2007 08:10 GMT
>Knute:
>  No, both operating systems are the same. XP Pro w/ SP2 installed.

There were a number of other potential differences (I)
listed, including screen size and resolution, default
font size, and PLAF.

While some of those might be identical for both
PC's..

>...I
>am not sure if I am able to upgrade to 1.6,

..The PLAF migh be slightly different even between
1.5 and 1.6.  Though of course, the really important
thing for me to discuss is..

>...I was having trouble finding a link.
>
>Andrew:
>  The code you provided was correct syntactically but didnt fix the
>appearance problem.

..that's a bummer.  And really the relevant (to my point)
negation of what I was saying.   Or to put that another,
simpler way.

"Seems I was wrong"

>...The only difference was that the button and combo
>box were centered in the frame.

<weakly>
Looks neat on my PC (XP Pro, running 1.6, using
screen res. of 1024x786 and relatively 'default' settings
otherwise).  
</weakly>

>Any other ideas?

Can you give us a screen shot of the new GUI?

Also I would be interested to hear any critique of my
new (or your original) layouts.  Though more interested
in hearing critiques of mine, because I was *really*
confident that would sort the problem, and would like
to get to the bottom of what went wrong.

Come on you GUI gurus, where are you hiding?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Michael Jung - 15 Aug 2007 18:26 GMT
> >Knute:
[...]
> > No, both operating systems are the same. XP Pro w/ SP2 installed.

> There were a number of other potential differences (I)
> listed, including screen size and resolution, default
> font size, and PLAF.

In order to check, whether this is a PLAF problem or not, set the PLAF at the
beginning and see, whether this problem persists for more than the OS-default
PLAF.  (You could also play around with the fonts, but I doubt that this has
to do with either that or the screen resolution.

Someone else already guessed that it may have to do with some accessibility
hints.

You may also compare the LOCALEs.  To me it looks as if someone was trying to
add an additional line at the end of text.  Maybe the LOCALE force-codes CR or
LF or CR/LF at the end of strings?

You may also write a simple AWT program to display a label and compare those
to see whether this is a Swing problem or lies deeper.

Michael
matt - 16 Aug 2007 07:06 GMT
> > >Knute:
> [...]
> > > No, both operating systems are the same. XP Pro w/ SP2 installed.
> > There were a number of other potential differences (I)
> > listed, including screen size and resolution, default
> > font size, and PLAF.

Ok, I changed screen resolutions and changed the fonts around but
nothing seemed to work. I tried reinstalling my video drivers but I'm
not totally sure if I did it correctly since all of those drivers were
packaged with my laptop on one disc.

> In order to check, whether this is a PLAF problem or not, set the PLAF at the
> beginning and see, whether this problem persists for more than the OS-default
> PLAF.  (You could also play around with the fonts, but I doubt that this has
> to do with either that or the screen resolution.

I am interested in this PLAF stuff though. However, I'm not really
familiar with any of it. From what I have looked up on google (I
thought PLAF was a kind of rice) the only stuff I have ever done with
it is using the setDefaultLookAndFeel method that comes in most swing
components. Do you think you could get me started in a way to change
this?

> Someone else already guessed that it may have to do with some accessibility
> hints.
>
> You may also compare the LOCALEs.  To me it looks as if someone was trying to
> add an additional line at the end of text.  Maybe the LOCALE force-codes CR or
> LF or CR/LF at the end of strings?

I have no idea what any of those things are. How would I look for
force codes? You saw the code I have and Andrew's code is posted in a
previous message. Is that something that is in the code, or an
internal setting?

> You may also write a simple AWT program to display a label and compare those
> to see whether this is a Swing problem or lies deeper.

I am assuming this means something other than a swing program. Labels
tend to look the same for some reason. Most of these sizing issues
only occur on buttons and combo boxes. Do you know of anyway to create
those using AWT?

Thanks for your help guys. I'm glad I'm not just doing something
stupid, although I wouldn't rule that out yet
matt - 16 Aug 2007 07:15 GMT
Ok I figured out the AWT thing. Not as hard as I thought it would be.
I made a simple class located here: http://matthome.gotdns.com/AWTFrame.java
and everything looks ok. I guess that means its a swing problem then???
Michael Jung - 16 Aug 2007 16:16 GMT
> > In order to check, whether this is a PLAF problem or not, set the PLAF at
> > the beginning and see, whether this problem persists for more than the
> > OS-default PLAF.  (You could also play around with the fonts, but I doubt
> > that this has to do with either that or the screen resolution.

> I am interested in this PLAF stuff though. However, I'm not really
> familiar with any of it. From what I have looked up on google (I
> thought PLAF was a kind of rice) the only stuff I have ever done with
> it is using the setDefaultLookAndFeel method that comes in most swing
> components. Do you think you could get me started in a way to change
> this?

LAF = Look and Feel. PLAF = Platform LAF. What I meant was that you should
change that.  Take a look at the Java tutorials to see how to change the
LAF.  You have at least two at your disposal: Native (Windows) and
Metal.  Try Metal.

> > Someone else already guessed that it may have to do with some
> > accessibility hints.  You may also compare the LOCALEs.  To me it looks as
> > if someone was trying to add an additional line at the end of text.  Maybe
> > the LOCALE force-codes CR or LF or CR/LF at the end of strings?

Accessibility means preparing your application for people with disabilities,
mostly reading in this case. I'm not really sure how Java interacts with OS
hints here. Mostly such things include big letters, black and white only
screens.  It doesn't appear to be in your case.

"Locale" is actually a class in java.util.  Read the Javadoc in print out the
result of "getDefault".

> I have no idea what any of those things are. How would I look for
> force codes? You saw the code I have and Andrew's code is posted in a
> previous message. Is that something that is in the code, or an
> internal setting?

force-code isn't a technical entity. I meant that somehow Swing might attempt
to use "any_string\n" instead of "any_string" in it's representation on the
GUI (forcing a "\n" at the end of every string).  For whatever reasons.  From
what I'm writing here you should infer that I am guessing and my guess isn't
better than anybody elses. The Locale is just a possibility (to which maybe
only my imperfect understanding of them lead me).

What I would also try, just curiousity, how multiline strings are displayed in
your swing elements.

You could also print out the FontMetrics of your GUI elements to see whether
they have oversized descents (getDescent and getMaxDescent). Compare that on
your machines.  But that should also show in AWT.

Or force it to use a built-in font, such as "Lucida sans" and see whether
there is a difference. (See the Java2D FAQ on how to check your fonts.)

> > You may also write a simple AWT program to display a label and compare
> > those to see whether this is a Swing problem or lies deeper.

Since you get an AWT program to run, we have cornered the problem in swing. Is
it possible that you install another JDK on your machine at another location?
Try and run your programs from there.  Maybe the orginal installation was
botched.

Michael
Knute Johnson - 15 Aug 2007 03:07 GMT
> Unfortunately, I cannot really provide my code as an example, not that
> it would make a difference anyways. What I have provided, is two
[quoted text clipped - 9 lines]
>
> Thank you

Are the examples being run on different operating systems?  Is the first
a Linux or maybe Vista?  If it is Vista, I would be very curious to see
this on a 1.6 JRE.

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 15 Aug 2007 10:41 GMT
>http://matthome.gotdns.com/pic.jpg

I would try this voodoo.
1. Reinstall your video driver.
2. change the screen resolution and font size in the OS control panel.

It is almost as if you have accidentally turned some sort of feature
for people with poor vision.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.