Solved

Swing on Windows and Unix

Posted on 2003-11-12
6
419 Views
Last Modified: 2013-11-23
Hi

I am creating a GUI application using the swing package. I have done it initially on Windows using absolute positioning(not a layout manager), and it all works and looks fine. However, when I run the application on Linux all the GUI aspects are ruined and dont look right. Is there something I am missing?

Thanks
Mike
0
Comment
Question by:mikecorfield
6 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 100 total points
ID: 9731624
You'll probably find that the fonts are different between the two systems, so any components that you have that contain text may be causing problems.

If this is the case, you'll need to do some calculations using FontMetrics in order to work out the sizes of your components dynamically.

This is why you should avoid absolute positioning wherever possible.  Is there a good reason why you can't use any LayoutManagers?
0
 
LVL 1

Expert Comment

by:allelopath
ID: 9731644
Using absolute position is a bad idea and is why it doesn't look right.

This talks about when absolute positioning might be a valid way to go:
http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html
0
 

Assisted Solution

by:dhaya
dhaya earned 25 total points
ID: 9731678
try use this one,

someone helped me in sun forum with this font rendered.

i have commented the morph() and places where it is invoked since i do a validate() and repaint() after all components are added..

depends on whay you need, it may need to be changed

import java.awt.*;
import java.awt.font.*;
import java.text.*;
import javax.swing.*;

public class CustomLabel extends JComponent
{
    private String text;
    private Insets margin = new Insets(5,5,5,5);
    private int maxWidth = Integer.MAX_VALUE;
    private boolean justify;
    private final FontRenderContext frc = new FontRenderContext(null, false, false);


    /*private void morph()
    {
        revalidate();
        repaint();
    }*/


    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        String old = this.text;
        this.text = text;

        firePropertyChange("text", old, this.text);

       // if ((old == null) ? text!=null : !old.equals(text))
         //   morph();
    }

    public int getMaxWidth()
    {
        return maxWidth;
    }

    public void setMaxWidth(int maxWidth)
    {
        if (maxWidth <= 0)
            throw new IllegalArgumentException();
        int old = this.maxWidth;
        this.maxWidth = maxWidth;
        firePropertyChange("maxWidth", old, this.maxWidth);
       // if (old !=  this.maxWidth)
         //   morph();
    }

    public boolean isJustified()
    {
        return justify;
    }

    public void setJustified(boolean justify)
    {
        boolean old = this.justify;
        this.justify = justify;
        firePropertyChange("justified", old, this.justify);
        //if (old != this.justify)
         //   repaint();
    }

    public Dimension getPreferredSize()
    {
        return paintOrGetSize(null, getMaxWidth());
    }

    public Dimension getMinimumSize()
    {
        return getPreferredSize();
    }

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        paintOrGetSize((Graphics2D)g, getWidth());
    }

    private Dimension paintOrGetSize(Graphics2D g, int width)
    {
        Insets insets = getInsets();
        width -= insets.left + insets.right + margin.left + margin.right;
        float w = insets.left + insets.right + margin.left + margin.right;
        float x = insets.left + margin.left, y=insets.top + margin.top;

        if (width > 0 && text != null && text.length() > 0)
        {
              AttributedString as = new AttributedString(getText());
              as.addAttribute(TextAttribute.FONT, getFont());
              AttributedCharacterIterator aci = as.getIterator();
              LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
              float max = 0;
              while (lbm.getPosition() < aci.getEndIndex())
              {
                    TextLayout textLayout = lbm.nextLayout(width);
                    if (g != null && isJustified() && textLayout.getVisibleAdvance() > 0.80 * width)
                        textLayout = textLayout.getJustifiedLayout(width);
                    if (g != null)
                        textLayout.draw(g, x, y + textLayout.getAscent());
                    y += textLayout.getDescent() + textLayout.getLeading() + textLayout.getAscent();
                    max = Math.max(max, textLayout.getVisibleAdvance());
              }
              w += max;
          }

    return new Dimension((int)Math.ceil(w), (int)Math.ceil(y) + insets.bottom + margin.bottom);    }

}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mikecorfield
ID: 9731754
I'm pretty new to java and I always thought that absolute positioning gave you more control over your objects, wheras with a layout manager its out of your hands really. But I'll definately try it out.

dhaya, that looks good, and I can see what its doing, but I think its a little advanced for me just at the moment. I will keep though it and look to develop it in the future.

Thanks for all your help.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9731791
No problem ;-)

If you want "fine grain" control of your layout, use a GridBagLayout.  It's probably the most difficult of all the layout managers to deal with, but it does allow good control of the positioning.

Just in case, did you know that when you apply a layout manager to a JPanel, that JPanel can be included inside another JPanel (etc. etc. etc.).  This way, you could have the standard BorderLayout manager for your main JFrame, inside which you could have another JPanel (attached to the "North") that also uses a BorderLayout and another JPanel (eg. for buttons) attached to the "South" that uses a FlowLayout.

You can get some pretty impressive layouts using nesting in this way.

Best of luck.
0
 
LVL 1

Expert Comment

by:allelopath
ID: 9732292
I recommend GridBagLayout, too.  Its takes a while to get the hang of it, though.
When you first start using it, I recommend drawing your GUI on a piece of paper.
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now