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