Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #1391 > unrolled thread
| Started by | "martinog2" <martinog2@THRWHITE.remove-dii-this> |
|---|---|
| First post | 2011-04-27 15:33 +0000 |
| Last post | 2011-04-27 15:33 +0000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.java.gui
How to prevent a long JLa "martinog2" <martinog2@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
Re: How to prevent a long "A. Bolmarcich" <a..bolmarcich@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
Re: How to prevent a long "visionset" <visionset@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
Re: How to prevent a long "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
Re: How to prevent a long "martinog2" <martinog2@THRWHITE.remove-dii-this> - 2011-04-27 15:33 +0000
| From | "martinog2" <martinog2@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:33 +0000 |
| Subject | How to prevent a long JLa |
| Message-ID | <1175721430.203113.150600@l77g2000hsb.googlegroups.com> |
To: comp.lang.java.gui
Hi All,
I want to have a long JLabel, but I dont want my entire dialog to be
sized based on this long JLabel -- I want the JLabel to wrap, & I want
the dialog size to be set by other controls.
So I want something like this if the control (for ex. a JTextField) is
long:
A really really really really really long JLabel
<--------JTextField-------------------------------->
And something like this if the JTextField is short:
A really really really
really really long
JLabel
<---JTextField------->
I found out that I need to use <html> tags to get the JLabel to wrap.
So that was one hurdle. Now, I almost got what I want with the
following code:
[CODE]
import javax.swing.*;
import java.awt.*;
public class x
{
private static void createAndShowGUI()
{
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
// Change content pane to JPanel set to BoxLayout
JPanel framePanel = new JPanel();
framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.Y_AXIS));
frame.setContentPane(framePanel);
JLabel label = new JLabel("<html>Here is a label. It can get to be
very very very long aaaaaaa bbbbbbbbbbbbbbbb ccc dd eeeeeeee ffffff
gggggggggg hhhh.</html>");
frame.add(label);
// CHANGE JTEXTFIELD SIZE TO SEE PROBLEM
//frame.add(new JTextField(80));
frame.add(new JTextField(10));
// THIS LINE ALMOST DOES WHAT I WANT
label.setPreferredSize(new Dimension(0, 100));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() { createAndShowGUI(); }
});
}
}
[/CODE]
With this, I can get the effect I want. To see it, I change the size
of the JTextField. JTextField(10) -- it wraps. JTextField(80) -- it
doesnt wrap. Thats good.
I got it to work like this with this line:
label.setPreferredSize(new Dimension(0, 100));
The problem is the height. For the width, I can use '0' and it will
size correctly. I cant do the same with height. If I set it to '0' (or
-1, 1, 2, etc) then thats the height it uses. With '100', its ok when
the text field is small, but when its big, theres too much vertical
space around the label.
I dont know how to say:
'set a preferred width, but leave the preferred height alone'
Or
'set preferred width to 0 (and you'll later re-adjust that),
and set preferred height to the height after you've wrapped it'
I had originally used <br> tags to force the wrapping myself, but
later found out my gui looks bad when Windows is set to 120 DPI. So I
basically want to avoid all hard coded sizes, or line breaks.
Thanks
---
* 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
[toc] | [next] | [standalone]
| From | "A. Bolmarcich" <a..bolmarcich@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:33 +0000 |
| Subject | Re: How to prevent a long |
| Message-ID | <slrnf18ch4.28n9.aggedor@earl-grey.cloud9.net> |
| In reply to | #1391 |
To: comp.lang.java.gui
On 2007-04-04, martinog2@gmail.com <martinog2@gmail.com> wrote:
> Hi All,
>
> I want to have a long JLabel, but I dont want my entire dialog to be
> sized based on this long JLabel -- I want the JLabel to wrap, & I want
> the dialog size to be set by other controls.
[snip]
> With this, I can get the effect I want. To see it, I change the size
> of the JTextField. JTextField(10) -- it wraps. JTextField(80) -- it
> doesnt wrap. Thats good.
>
> I got it to work like this with this line:
> label.setPreferredSize(new Dimension(0, 100));
>
> The problem is the height. For the width, I can use '0' and it will
> size correctly. I cant do the same with height. If I set it to '0' (or
> -1, 1, 2, etc) then thats the height it uses. With '100', its ok when
> the text field is small, but when its big, theres too much vertical
> space around the label.
>
> I dont know how to say:
> 'set a preferred width, but leave the preferred height alone'
You say that by writing and using a class that extends JLabel that
overrides the getPreferredSize() method to be like
Dimension getPreferredSize() {
return newDimension(WIDTH_YOU_WANT, super.getPreferredSize().height);
}
> Or
> 'set preferred width to 0 (and you'll later re-adjust that),
> and set preferred height to the height after you've wrapped it'
This can be done by a more complicated getPreferredSize() method
that can somehow determine what its width will be set to (say,
by getting the preferred width of the JTextField in the same
column) and then determine what its height should be for that width.
---
* 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
[toc] | [prev] | [next] | [standalone]
| From | "visionset" <visionset@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:33 +0000 |
| Subject | Re: How to prevent a long |
| Message-ID | <GA6Rh.1837$gr2.969@newsfe4-gui.ntli.net> |
| In reply to | #1391 |
To: comp.lang.java.gui <martinog2@gmail.com> wrote in message news:1175721430.203113.150600@l77g2000hsb.googlegroups.com... > Hi All, > > I want to have a long JLabel, but I dont want my entire dialog to be > sized based on this long JLabel -- I want the JLabel to wrap, & I want > the dialog size to be set by other controls. Just use a JTextArea JTextArea messArea = new JTextArea(message); messArea.setFont(messArea.getFont().deriveFont(Font.BOLD)); // or get a JLabels font messArea.setEditable(false); messArea.setOpaque(false); You may want to disable selection as well? -- Mike W --- * 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
[toc] | [prev] | [next] | [standalone]
| From | "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:33 +0000 |
| Subject | Re: How to prevent a long |
| Message-ID | <1175733199.831818.116870@o5g2000hsb.googlegroups.com> |
| In reply to | #1391 |
To: comp.lang.java.gui
On Apr 4, 2:17 pm, martin...@gmail.com wrote:
> Hi All,
>
> I want to have a long JLabel, but I dont want my entire dialog to be
> sized based on this long JLabel -- I want the JLabel to wrap, & I want
> the dialog size to be set by other controls.
>
> So I want something like this if the control (for ex. a JTextField) is
> long:
>
> A really really really really really long JLabel
> <--------JTextField-------------------------------->
>
> And something like this if the JTextField is short:
>
> A really really really
> really really long
> JLabel
> <---JTextField------->
>
> I found out that I need to use <html> tags to get the JLabel to wrap.
> So that was one hurdle. Now, I almost got what I want with the
> following code:
>
> [CODE]
> import javax.swing.*;
> import java.awt.*;
>
> public class x
> {
> private static void createAndShowGUI()
> {
> JFrame frame = new JFrame("test");
> frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
>
> // Change content pane to JPanel set to BoxLayout
> JPanel framePanel = new JPanel();
> framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.Y_AXIS));
> frame.setContentPane(framePanel);
>
> JLabel label = new JLabel("<html>Here is a label. It can get to be
> very very very long aaaaaaa bbbbbbbbbbbbbbbb ccc dd eeeeeeee ffffff
> gggggggggg hhhh.</html>");
> frame.add(label);
>
> // CHANGE JTEXTFIELD SIZE TO SEE PROBLEM
> //frame.add(new JTextField(80));
> frame.add(new JTextField(10));
>
> // THIS LINE ALMOST DOES WHAT I WANT
> label.setPreferredSize(new Dimension(0, 100));
>
> frame.pack();
> frame.setVisible(true);
> }
>
> public static void main(String[] args)
> {
> javax.swing.SwingUtilities.invokeLater(
> new Runnable() {
> public void run() { createAndShowGUI(); }
> });
> }}
>
> [/CODE]
>
> With this, I can get the effect I want. To see it, I change the size
> of the JTextField. JTextField(10) -- it wraps. JTextField(80) -- it
> doesnt wrap. Thats good.
>
> I got it to work like this with this line:
> label.setPreferredSize(new Dimension(0, 100));
>
> The problem is the height. For the width, I can use '0' and it will
> size correctly. I cant do the same with height. If I set it to '0' (or
> -1, 1, 2, etc) then thats the height it uses. With '100', its ok when
> the text field is small, but when its big, theres too much vertical
> space around the label.
>
> I dont know how to say:
> 'set a preferred width, but leave the preferred height alone'
>
> Or
> 'set preferred width to 0 (and you'll later re-adjust that),
> and set preferred height to the height after you've wrapped it'
>
> I had originally used <br> tags to force the wrapping myself, but
> later found out my gui looks bad when Windows is set to 120 DPI. So I
> basically want to avoid all hard coded sizes, or line breaks.
>
> Thanks
It depends on the layout manager being used.
You COULD set maximum size on the label though.
---
* 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
[toc] | [prev] | [next] | [standalone]
| From | "martinog2" <martinog2@THRWHITE.remove-dii-this> |
|---|---|
| Date | 2011-04-27 15:33 +0000 |
| Subject | Re: How to prevent a long |
| Message-ID | <1175796258.196380.188470@y80g2000hsf.googlegroups.com> |
| In reply to | #1391 |
To: comp.lang.java.gui Thanks guys. Someone on codeguru.com gave me a solution to this. The link is here: http://www.codeguru.com/forum/showthread.php?t=419858 Thanks again :) --- * 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.gui
csiph-web