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

JTextField shrinking to (almost) nothing

100 views
Skip to first unread message

Hendrik Maryns

unread,
Jun 13, 2008, 8:04:33 AM6/13/08
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I have this JTextField next to a button in a panel.

If I resize the window, such that the text field gets below its
preferred size, it totally disappears. I’d like it to shrink.

Even better: it should expand and shrink to fit its contents, since it
is not editable, only to show the user what he’s working on.

Or should I use something else here? I though of using a label, but
that doesn’t look good either.

SSCCE, not entirely minimal, but good illustration:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class UIBuilder {
~ public UIBuilder() {
~ super();
~ // Look-and-feel
~ try {
~ UIManager.setLookAndFeel(UIManager
~ .getSystemLookAndFeelClassName());
~ } catch (final Exception exc) {
~ // fall back to default
~ exc.printStackTrace();
~ }
~ final JFrame frame = new JFrame();
~ final JPanel np = new JPanel(true);
~ final JButton treebankButton = new JButton("Treebank");
~ treebankButton.setToolTipText("Choose a treebank.");
~ treebankButton.addActionListener(new ActionListener() {
~ @Override
~ public void actionPerformed(final ActionEvent e) {
~ // some useful stuff
~ }
~ });
~ np.add(treebankButton);
~ final JTextField treebankField = new JTextField(
~ "Please choose a treebank.", 30);
~ treebankField.setToolTipText("The treebank being queried.");
~ treebankField.setName("Treebank");
~ treebankField.setEditable(false);
~ np.add(treebankField);
~ frame.getContentPane().add(np, "North"); //$NON-NLS-1$
~ frame.pack();
~ frame.setVisible(true);
~ }

~ public static void main(final String[] args) {
~ SwingUtilities.invokeLater(new Runnable() {
~ public void run() {
~ new UIBuilder();
~ }
~ });
~ }
}

Grateful for any comments, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIUmJRe+7xMGD3itQRAq4qAJ9g9vbKzcaHe4gpZ1gcTU7tMJ/WZwCfdAjj
G9AAD9cUyAuxUrQf3dpJbn8=
=TU98
-----END PGP SIGNATURE-----

Andrew Thompson

unread,
Apr 27, 2011, 11:46:01 AM4/27/11
to
To: comp.lang.java.gui

On Jun 13, 10:04=A0pm, Hendrik Maryns <gtw37b...@sneakemail.com> wrote:

> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import javax.swing.*;
>
> public class UIBuilder {

> ~ =A0public UIBuilder() {
> ~ =A0 =A0super();
=2E..
> ~ =A0}


>
> }
>
> Grateful for any comments, H.

Does 'comments' include questions?
Where did all the ~ characters come from?

<sscce>


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class UIBuilder {
public UIBuilder() {
super();
// Look-and-feel
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (final Exception exc) {


// fall back to default

exc.printStackTrace();
}
final JFrame frame =3D new JFrame();
final JPanel np =3D new JPanel(true);
final JButton treebankButton =3D new JButton("Treebank");


treebankButton.setToolTipText("Choose a treebank.");

treebankButton.addActionListener(new ActionListener() {
@Override


public void actionPerformed(final ActionEvent e) {

// some useful stuff
}
});
np.add(treebankButton);
final JTextField treebankField =3D new JTextField(


"Please choose a treebank.", 30);

treebankField.setToolTipText("The treebank being queried.");

treebankField.setName("Treebank");
treebankField.setEditable(false);
np.add(treebankField);
frame.getContentPane().add(np, "North"); //$NON-NLS-1$
frame.pack();

// not the answer to the wider question, but
// one way to solve it for the displayed UI
frame.setMinimumSize( frame.getPreferredSize() );
// please specify a close operation, before setting a PLAF!
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setVisible(true);
}


public static void main(final String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UIBuilder();
}
});
}
}
</sscce>

--
Andrew T.

---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Flo 'Irian' Schaetz

unread,
Jun 13, 2008, 1:25:48 PM6/13/08
to
And thus spoke Hendrik Maryns...

> If I resize the window, such that the text field gets below its
> preferred size, it totally disappears. I d like it to shrink.

Try using another LayoutManager? For example a BorderLayout...

final JPanel np = new JPanel(new BorderLayout(), true);
...
np.add(treebankButton, BorderLayout.WEST);
...
np.add(treebankField, BorderLayout.CENTER);

Flo

Evans

unread,
Jun 13, 2008, 4:51:40 PM6/13/08
to
Hi,

Its important that you know that components like Labels, Buttons and
Textfields do not expand once created. Even a TextArea will not expand
expand without ScrollPanes.

That said, if you're looking for something to help you display that
sort of 'progress' notification to your users, I would suggest you use
a textArea and set the Background to the default color:
textArea.setBackground(getBackgound());

That should give you a smooth background that fades to the surrounding
component; then you can display your message on it.

Note: I did not text that piece of code, so it might not be right.

Thats my 2-pence worth ;-)

Evans
http://www.onyxtic.com

Hendrik Maryns

unread,
Jun 16, 2008, 8:46:42 AM6/16/08
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Thompson schreef:


| On Jun 13, 10:04 pm, Hendrik Maryns <gtw37b...@sneakemail.com> wrote:
|
|> import java.awt.event.ActionEvent;
|> import java.awt.event.ActionListener;
|> import javax.swing.*;
|>
|> public class UIBuilder {

|> ~ public UIBuilder() {
|> ~ super();
| ...
|> ~ }


|>
|> }
|>
|> Grateful for any comments, H.
|
| Does 'comments' include questions?

Of course.

| Where did all the ~ characters come from?

I have no idea. I guess they are transformed tab characters from
pasting in. Sorry for that.

| <sscce>


| // not the answer to the wider question, but
| // one way to solve it for the displayed UI
| frame.setMinimumSize( frame.getPreferredSize() );

Almost. This makes the text field disappear at exactly the minimal size.

| // please specify a close operation, before setting a PLAF!
| frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

I do this of course, it just got snipped out in making the SSCCE,
unfortunately.

| </sscce>

I used Flo’s suggestion, to my content, thanks both for the answers.

Cheers, H.


- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIVmCye+7xMGD3itQRAlINAJwNy85Ifte/sDjQpx7UaogOYytaaQCggoqN
QondZItqGfn94WCq///iQPw=
=cjh1
-----END PGP SIGNATURE-----

Hendrik Maryns

unread,
Jun 16, 2008, 8:54:12 AM6/16/08
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Evans schreef:

It isn’t, indeed. What object is getBackground() defined on? It’s
pretty pointless to do it on the JTextArea itself.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIVmJ0e+7xMGD3itQRAlyEAJ9gWjpzImEAPqJtKi36fb35fRkJcACfV4XA
ypjTAeMXpnHMALxpDvK0oUs=
=3mhi
-----END PGP SIGNATURE-----

0 new messages