View unanswered posts | View active topics It is currently Thu Mar 23, 2017 11:02 pm



Reply to topic  [ 3 posts ] 
 Problem aligning JTextField and JTextAreas in Mac OSX 
Author Message

Joined: Mon Apr 19, 2010 10:47 pm
Posts: 2
Post Problem aligning JTextField and JTextAreas in Mac OSX
Hi all,

I posted this first on Stack Overflow but no one really seems to know what's going on, so I thought I'd try straight from the source.

Basically I'm trying to have JTextAreas and JTextFields laid out on top of each other, in columns, but their widths do not match.


Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import net.miginfocom.swing.MigLayout;

public class Test extends JPanel {

    private static final int NUM_CHARACTERS_WIDTH = 20;
    private static final int NUM_ROWS = 5;
    public Test() {

        setLayout(new MigLayout(
                "wrap 2, debug",
                // Align text labels on the so their right edge meets left edge of the text fields
                "[right][fill]"
                ));

        Border b = new JTextField().getBorder();

        add(new JLabel("Text field:"));
        add(new JTextField(NUM_CHARACTERS_WIDTH));

        add(new JLabel("No scrollpane text area:"));
        add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));

        add(new JLabel("Scrollpane text area:"));
        JScrollPane p = new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
        p.setBorder(b);
        add(p);

        add(new JLabel("Text field:"));
        add(new JTextField(NUM_CHARACTERS_WIDTH));
    }



    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new Test();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

}


In debug mode: Image

In no-debug mode: Image

As you can see, the text area has a different width than does the text field, despite both being set to the same size. It looks like the JTextField has an invisible border that takes up some space... any way around this?


Mon Apr 19, 2010 10:53 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Problem aligning JTextField and JTextAreas in Mac OSX
Hello,

Apple has a nasty habbit of compensating by default and not let the developer decide. This is such a case where they have added a border to make it more visually like OS X. This should be the choice of the layout manager...

MigLayout can compensate for visual bounds like this but it is only done for JTabbedPane in Windows XP. I'm not sure it can be done 100% good in OS X though. I'll have to check. We don't want the text field to just grow into the bounds.

I have added this to the todo list for the next version.

Cheers,
Mikael


Wed Apr 21, 2010 10:10 pm
Profile WWW

Joined: Mon Apr 19, 2010 10:47 pm
Posts: 2
Post Re: Problem aligning JTextField and JTextAreas in Mac OSX
Thank you! I was afraid I was doing something wrong.


Thu Apr 22, 2010 4:25 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.