Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2009

Tip: Looking for answers? Try searching our database.

Removing Empty Margin Space from JTextField and JLabel

Thread view: 
christidy@googlemail.com - 28 Feb 2009 19:44 GMT
Hi

I am trying to code a Sudoku grid where people can write candidates
(possible values) at the top of each cell. I have done this by each
cell using a border layout where the:
- north area is a JTextField where candidates can be entered
- the centre area is a JTextLabel where the cell value can be entered
(using a click popup menu)

Unfortunately I am struggling to make the cell values fill the entire
vertical space up to where candidates can be written - there seems to
be a margin or gap of a few pixels where the value will not be written
into. Similarly in the north area, there seems a margin of a few
pixels above and below where values are are written.
Is there any way I can remove these extra spaces?
Furthermore, if anyone has any better recommendations of how to lay
this out, so it can be scaled easily (e.g. the font size increases as
the grid is expanded) this would also be most helpful.

Thanks in advance
Chris

p.s
Some code which can be run to show the problem is as follows:

public class CellTest extends JPanel  {

    public CellTest() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createLineBorder (Color.black, 1));

        Font canFont = new Font("Verdana", Font.PLAIN, 10);
        JTextField canTxtField = new JTextField("1");
        canTxtField.setFont(canFont);
        canTxtField.setBorder(null);

        add(canTxtField, BorderLayout.NORTH);

        Font cellValFont = new Font("Times new roman", Font.PLAIN, 32);
        JLabel cellValTxtLabel = new JLabel("2");

        cellValTxtLabel.setFont(cellValFont);
        cellValTxtLabel.setHorizontalAlignment(JLabel.CENTER);
        add(cellValTxtLabel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(200, 200);
        frame.getContentPane().setLayout(new GridLayout(3, 3, 0, 0));
        for(int i=0; i <9; i++){
            frame.add(new CellTest());
        }
        frame.setVisible(true);
    }
}
John B. Matthews - 28 Feb 2009 21:16 GMT
In article
<61dd33ce-9d94-4a45-ab6b-8b4f1ae2a677@q11g2000yqh.googlegroups.com>,

[...]
> Unfortunately I am struggling to make the cell values fill the entire
> vertical space up to where candidates can be written - there seems to
> be a margin or gap of a few pixels where the value will not be written
> into. Similarly in the north area, there seems a margin of a few
> pixels above and below where values are are written.
> Is there any way I can remove these extra spaces?

Here's what I see, modulo some color and font changes to see boundaries:

<http://sites.google.com/site/trashgod/celltest>

> Furthermore, if anyone has any better recommendations of how to lay
> this out, so it can be scaled easily (e.g. the font size increases as
> the grid is expanded) this would also be most helpful.

Using JComponents gives you a lot of built-in functionality, but the
look and feel will vary by platform. It's a trade-off. Here's the
opposite end of the spectrum: a scalable panel with no components at
all. You handle all mouse and keyboard activity in "world" co-ordinates:

<http://sites.google.com/site/drjohnbmatthews/scaledview>

Signature

John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Lew - 28 Feb 2009 21:44 GMT
christidy@googlemail.com wrote:
>> Unfortunately I am struggling to make the cell values fill the entire

Side note:  Make sure all graphics actions occur on the EDT.  The code example
above failed to do that, and that can cause subtle threading bugs.

Signature

Lew

christidy@googlemail.com - 28 Feb 2009 23:29 GMT
Thanks for the help again everyone

> Using JComponents gives you a lot of built-in functionality, but the
> look and feel will vary by platform. It's a trade-off. Here's the
> opposite end of the spectrum: a scalable panel with no components at
> all. You handle all mouse and keyboard activity in "world" co-ordinates:

that looks interesting John but is probably a bit too complex for my
skill level. I initially tried to create my Sudoku game so every cell
was a JButton which implements Icon allowing everything can be painted
on. I however found this got very complicated in terms of having to
constnatly call the repaint() method for any cell highlighting
(painting background colours then painting the values over the top),
adding/removing candidates, adding/removing a cell value and any other
tools that would effect how a cell looked.

>Here's what I see, modulo some color and font changes to see boundaries:
><http://sites.google.com/site/trashgod/celltest>

It certainly seems there is some extra space (or margins) where the
values can not be written i.e. a few pixels above and below where the
candidates are written. Is there any way to be able to modify this
code so that the numbers fill these spaces i.e. so that the cell value
can be largely within the centre of the cell?

kind regards
Chris
John B. Matthews - 01 Mar 2009 04:08 GMT
In article
<4ea116a7-ea43-44d4-b955-2eac2e0afe82@b16g2000yqb.googlegroups.com>,

[...]

I updated the code, to illustrate Lew's EDT reminder:

<http://sites.google.com/site/trashgod/celltest>

> It certainly seems there is some extra space (or margins) where the
> values can not be written i.e. a few pixels above and below where the
> candidates are written. Is there any way to be able to modify this
> code so that the numbers fill these spaces i.e. so that the cell
> value can be largely within the centre of the cell?

Yes, JLabel centers the digit glyph nicely. In most fonts, digits have
minimal descent; but I suspect JLabel is allowing for the maximum, as
it's text is not limited to numbers.

You can always subclass JPanel and center a suitably sized glyph, as
shown here:

<http://groups.google.com/group/comp.lang.java.gui/msg/f5078624dd3e6559>

Of course, JPanels can be nested.

Signature

John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

John B. Matthews - 03 Mar 2009 01:36 GMT
> In article
> <4ea116a7-ea43-44d4-b955-2eac2e0afe82@b16g2000yqb.googlegroups.com>,
[...]
> > Is there any way to be able to modify this code so that the numbers
> > fill these spaces i.e. so that the cell value can be largely within
> > the centre of the cell?

Here's one approach to filling and resizing:

<http://sites.google.com/site/trashgod/celltest>

Signature

John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

christidy@googlemail.com - 04 Mar 2009 01:05 GMT
> Here's one approach to filling and resizing:
>
> <http://sites.google.com/site/trashgod/celltest>

Thanks again John, that should prove very useful, I really appreciate
all the help you've been giving me in the last few weeks. Just to warn
you in advance, I'm currently implementing 2 Sudoku games to test the
effectiveness of different support tools (including implementing
different ways in which candidates are displayed). Therefore if I
later ask on these forums questions about another approach e.g. to add
candidates, please don't think that is because I have simply ignored
all these questions I have asked :)

Kind regards,
Chris
John B. Matthews - 04 Mar 2009 02:57 GMT
In article
<ceef0bf4-e9fe-4cd3-8f73-c4ba2c4a09c0@q27g2000vbn.googlegroups.com>,

> > Here's one approach to filling and resizing:
> >
[quoted text clipped - 8 lines]
> approach e.g. to add candidates, please don't think that is because I
> have simply ignored all these questions I have asked :)

No problem. There are many paths to take, and you're right to explore
them in parallel. If you come up with anything you can share, I'm sure
your results will be instructive.

Signature

John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2012 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.