Solved

Swing on Windows and Unix

Posted on 2003-11-12
6
422 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 42
ejb wildfly example 2 22
Convert from a json string array to a Java object 3 53
ejb mdb examples 1 8
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

861 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