Solved

Swing on Windows and Unix

Posted on 2003-11-12
6
418 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

707 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

16 Experts available now in Live!

Get 1:1 Help Now