Solved

Swing on Windows and Unix

Posted on 2003-11-12
6
421 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

806 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