Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Swing on Windows and Unix

Posted on 2003-11-12
6
Medium Priority
?
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 400 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 100 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

670 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