Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing Empty Margin Space from JTextField and JLabel

1,173 views
Skip to first unread message

chri...@googlemail.com

unread,
Feb 28, 2009, 2:44:33 PM2/28/09
to
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

unread,
Feb 28, 2009, 4:16:34 PM2/28/09
to
In article
<61dd33ce-9d94-4a45...@q11g2000yqh.googlegroups.com>,
chri...@googlemail.com wrote:

[...]


> 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>

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

Lew

unread,
Feb 28, 2009, 4:44:00 PM2/28/09
to
chri...@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.

--
Lew

chri...@googlemail.com

unread,
Feb 28, 2009, 6:29:43 PM2/28/09
to
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

unread,
Feb 28, 2009, 11:08:47 PM2/28/09
to
In article
<4ea116a7-ea43-44d4...@b16g2000yqb.googlegroups.com>,
chri...@googlemail.com wrote:

[...]

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.

John B. Matthews

unread,
Mar 2, 2009, 8:36:40 PM3/2/09
to
In article <nospam-8DF44D....@news.aioe.org>,

"John B. Matthews" <nos...@nospam.invalid> wrote:

> > 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>

chri...@googlemail.com

unread,
Mar 3, 2009, 8:05:35 PM3/3/09
to

> 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

unread,
Mar 3, 2009, 9:57:04 PM3/3/09
to
In article
<ceef0bf4-e9fe-4cd3...@q27g2000vbn.googlegroups.com>,
chri...@googlemail.com wrote:

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.

0 new messages