Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38312 > unrolled thread

Is there a method to update control size?

Started byEric Douglas <e.d.programmer@gmail.com>
First post2018-05-31 07:34 -0700
Last post2018-06-03 15:02 -0500
Articles 13 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Is there a method to update control size? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-31 07:34 -0700
    Re: Is there a method to update control size? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-05-31 17:20 +0200
    Re: Is there a method to update control size? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-05-31 11:28 -0400
      Re: Is there a method to update control size? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-31 09:42 -0700
        Re: Is there a method to update control size? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-05-31 19:22 +0200
          Re: Is there a method to update control size? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-31 10:38 -0700
            Re: Is there a method to update control size? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-05-31 19:45 +0200
              Re: Is there a method to update control size? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-31 10:55 -0700
                Re: Is there a method to update control size? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-05-31 20:05 +0200
                  Re: Is there a method to update control size? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2018-06-03 15:00 -0500
                    Re: Is there a method to update control size? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-06-04 02:25 +0200
        Re: Is there a method to update control size? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-05-31 15:55 -0400
          Re: Is there a method to update control size? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2018-06-03 15:02 -0500

#38312 — Is there a method to update control size?

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-05-31 07:34 -0700
SubjectIs there a method to update control size?
Message-ID<7a7e726d-051d-4f17-93ac-3dabdde80894@googlegroups.com>
I create a JLabel.  I create a JScrollPane.  I call setLayout(new GridLayout()) on the JLabel, then add the JScrollPane to the JLabel.  No Window yet, nothing displaying.  I call setPreferredSize on the JScrollPane.  Is there an easy way to call getPreferredSize on the JLabel to have it's size include the size of the JScrollPane or do I have to manually calculate it at this point?  After simply calling setPreferredSize(282,179) on the JScrollPane, the getPreferredSize() on the JLabel is returning java.awt.Dimension[width=4,height=4].

[toc] | [next] | [standalone]


#38313

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2018-05-31 17:20 +0200
Message-ID<pep3ta$ok0$1@dont-email.me>
In reply to#38312
On 2018-05-31 16:34, Eric Douglas wrote:
> I create a JLabel.  I create a JScrollPane.  I call setLayout(new GridLayout()) on the JLabel, then add the JScrollPane to the JLabel.  No Window yet, nothing displaying.  I call setPreferredSize on the JScrollPane.  Is there an easy way to call getPreferredSize on the JLabel to have it's size include the size of the JScrollPane or do I have to manually calculate it at this point?  After simply calling setPreferredSize(282,179) on the JScrollPane, the getPreferredSize() on the JLabel is returning java.awt.Dimension[width=4,height=4].
> 

Why (and how) in the name of Bob would you
 a) set a layout on a J*Label*
 b) *add* a JScrollPane to a J*Label*

Further, is this again from your code where you are working with
fixed-sized layouts? 'Cause if it isn't, I wouldn't recommend setting
the sizes yourself, and instead letting the LayoutManagers work with
whatever space is allocated to the UI in the Window (either by the user
resizing it, or by yourself by setting the size of the Window, and of
the window only). I personally am a GridBagLayout aficionado, and would
at most size components via weights and fill rules of the various grid
elements.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#38314

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2018-05-31 11:28 -0400
Message-ID<pep4b3$qnp$1@dont-email.me>
In reply to#38312
On 5/31/2018 10:34 AM, Eric Douglas wrote:
> I create a JLabel.  I create a JScrollPane.  I call setLayout(new GridLayout()) on the JLabel, then add the JScrollPane to the JLabel.

     Right here is where I simply must ask: "Why?"  Why not just put
the scroll pane wherever you were going to put the label?  (Or, why
not put the label-plus-scroll as the content of another label, and
put that as the content of yet another label, and ...?)

> I call setPreferredSize on the JScrollPane.

     Odd.  If you're scrolling something, the more typical approach
is to size the viewport (either directly, or by having the scrolled
client state a preference), and just let the scroll pane figure out
the overall size.

     By the way: What kind of client does the scroll pane scroll?

     Have you read

https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

?

-- 
esosman@comcast-dot-net.invalid
Nine hundred sixty-five days to go.

[toc] | [prev] | [next] | [standalone]


#38315

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-05-31 09:42 -0700
Message-ID<959c54c7-9fb9-4bba-93c9-6983553999bf@googlegroups.com>
In reply to#38314
On Thursday, May 31, 2018 at 11:28:43 AM UTC-4, Eric Sosman wrote:
> On 5/31/2018 10:34 AM, Eric Douglas wrote:
> > I create a JLabel.  I create a JScrollPane.  I call setLayout(new GridLayout()) on the JLabel, then add the JScrollPane to the JLabel.
> 
>      Right here is where I simply must ask: "Why?"  Why not just put
> the scroll pane wherever you were going to put the label?  (Or, why
> not put the label-plus-scroll as the content of another label, and
> put that as the content of yet another label, and ...?)
> 
Because the JLabel is a custom class which wraps every control type, which gives them all a common border, which turns blue if they have focus.  This also makes each control a single control where they may otherwise be multiple controls.  ie a US phone number control has an area code input, a prefix input, a suffix input, and text labels to show the ( ) - mask.  One border shows around all of them.  Putting a single control like this JScrollPane in such a JPanel is to avoid duplicating logic like the border objects and focus listeners.

> > I call setPreferredSize on the JScrollPane.
> 
>      Odd.  If you're scrolling something, the more typical approach
> is to size the viewport (either directly, or by having the scrolled
> client state a preference), and just let the scroll pane figure out
> the overall size.
> 
>      By the way: What kind of client does the scroll pane scroll?

The viewport component is a custom class which extends JList.
Yes I need to calculate the exact size for the component because while null layout on a JFrame may not be a popular choice, I am putting components into a 3rd party window which has a custom add method requiring parameters of the component and x/y/width/height.  I got the width figured, having some trouble on the height now.

> 
>      Have you read
> 
> https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
> 
> ?
> 

[toc] | [prev] | [next] | [standalone]


#38316

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2018-05-31 19:22 +0200
Message-ID<pepb3b$bpb$1@dont-email.me>
In reply to#38315
On 2018-05-31 18:42, Eric Douglas wrote:
> On Thursday, May 31, 2018 at 11:28:43 AM UTC-4, Eric Sosman wrote:
>>      Right here is where I simply must ask: "Why?"  Why not just put
>> the scroll pane wherever you were going to put the label?  (Or, why
>> not put the label-plus-scroll as the content of another label, and
>> put that as the content of yet another label, and ...?)
>>
> Because the JLabel is a custom class which wraps every control type, which gives them all a common border, which turns blue if they have focus.  This also makes each control a single control where they may otherwise be multiple controls.  ie a US phone number control has an area code input, a prefix input, a suffix input, and text labels to show the ( ) - mask.  One border shows around all of them.  Putting a single control like this JScrollPane in such a JPanel is to avoid duplicating logic like the border objects and focus listeners.

> Because the JLabel is a custom class which wraps every control type

> Putting a single control like this JScrollPane in *such a* JPanel

Which one is it then? JLabel or JPanel?

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#38317

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-05-31 10:38 -0700
Message-ID<6d179195-f875-4bc0-bd96-465c4b6ca804@googlegroups.com>
In reply to#38316
On Thursday, May 31, 2018 at 1:24:07 PM UTC-4, Daniele Futtorovic wrote:
> On 2018-05-31 18:42, Eric Douglas wrote:
> > On Thursday, May 31, 2018 at 11:28:43 AM UTC-4, Eric Sosman wrote:
> >>      Right here is where I simply must ask: "Why?"  Why not just put
> >> the scroll pane wherever you were going to put the label?  (Or, why
> >> not put the label-plus-scroll as the content of another label, and
> >> put that as the content of yet another label, and ...?)
> >>
> > Because the JLabel is a custom class which wraps every control type, which gives them all a common border, which turns blue if they have focus.  This also makes each control a single control where they may otherwise be multiple controls.  ie a US phone number control has an area code input, a prefix input, a suffix input, and text labels to show the ( ) - mask.  One border shows around all of them.  Putting a single control like this JScrollPane in such a JPanel is to avoid duplicating logic like the border objects and focus listeners.
> 
> > Because the JLabel is a custom class which wraps every control type
> 
> > Putting a single control like this JScrollPane in *such a* JPanel
> 
> Which one is it then? JLabel or JPanel?
> 
> -- 
> DF.

oops yes the wrapper class extends JLabel.

Am I mistaken that JLabel and JPanel are often interchangeable?

[toc] | [prev] | [next] | [standalone]


#38318

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2018-05-31 19:45 +0200
Message-ID<pepcdg$jr8$1@dont-email.me>
In reply to#38317
On 2018-05-31 19:38, Eric Douglas wrote:
> On Thursday, May 31, 2018 at 1:24:07 PM UTC-4, Daniele Futtorovic wrote:
>> On 2018-05-31 18:42, Eric Douglas wrote:
>>> On Thursday, May 31, 2018 at 11:28:43 AM UTC-4, Eric Sosman wrote:
>>>>      Right here is where I simply must ask: "Why?"  Why not just put
>>>> the scroll pane wherever you were going to put the label?  (Or, why
>>>> not put the label-plus-scroll as the content of another label, and
>>>> put that as the content of yet another label, and ...?)
>>>>
>>> Because the JLabel is a custom class which wraps every control type, which gives them all a common border, which turns blue if they have focus.  This also makes each control a single control where they may otherwise be multiple controls.  ie a US phone number control has an area code input, a prefix input, a suffix input, and text labels to show the ( ) - mask.  One border shows around all of them.  Putting a single control like this JScrollPane in such a JPanel is to avoid duplicating logic like the border objects and focus listeners.
>>
>>> Because the JLabel is a custom class which wraps every control type
>>
>>> Putting a single control like this JScrollPane in *such a* JPanel
>>
>> Which one is it then? JLabel or JPanel?
>>
> 
> oops yes the wrapper class extends JLabel.
> 
> Am I mistaken that JLabel and JPanel are often interchangeable?

In my view, you are. A JLabel is for displaying text (and an optional
icon). A JPanel is a generic container. You should never have to add any
children to a JLabel. Your custom wrapper should extend JPanel instead.
Then everything else you wrote would make sense.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#38319

FromEric Douglas <e.d.programmer@gmail.com>
Date2018-05-31 10:55 -0700
Message-ID<c176ec49-e0b0-42f4-8b68-b2941e47fe48@googlegroups.com>
In reply to#38318
On Thursday, May 31, 2018 at 1:46:37 PM UTC-4, Daniele Futtorovic wrote:
> > Am I mistaken that JLabel and JPanel are often interchangeable?
> 
> In my view, you are. A JLabel is for displaying text (and an optional
> icon). A JPanel is a generic container. You should never have to add any
> children to a JLabel. Your custom wrapper should extend JPanel instead.
> Then everything else you wrote would make sense.
> 
> -- 
> DF.

So far it appears to be working in this usage.  I don't recall if I specifically chose JLabel for a reason.  I'll try JPanel.

[toc] | [prev] | [next] | [standalone]


#38320

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2018-05-31 20:05 +0200
Message-ID<pepdje$sq2$1@dont-email.me>
In reply to#38319
On 2018-05-31 19:55, Eric Douglas wrote:
> On Thursday, May 31, 2018 at 1:46:37 PM UTC-4, Daniele Futtorovic wrote:
>>> Am I mistaken that JLabel and JPanel are often interchangeable?
>>
>> In my view, you are. A JLabel is for displaying text (and an optional
>> icon). A JPanel is a generic container. You should never have to add any
>> children to a JLabel. Your custom wrapper should extend JPanel instead.
>> Then everything else you wrote would make sense.
>>
> So far it appears to be working in this usage.  I don't recall if I specifically chose JLabel for a reason.  I'll try JPanel.

IIRC the default LayoutManager on a JPanel is BorderLayout, and a
BorderLayout automatically fills its central component to its bounds. So
you wouldn't need to touch the LM; just add the JScrollPane to the
JPanel with BorderLayout.CENTER constraint, then set the preferred size
on the JPanel (the container), and you can pass it as such to your
custom Window thingie.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#38334

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2018-06-03 15:00 -0500
Message-ID<pf1hc4$7n8$1@dont-email.me>
In reply to#38320
On 5/31/2018 13:05, Daniele Futtorovic wrote:
> IIRC the default LayoutManager on a JPanel is BorderLayout, and a

I don't think so...

import java.awt.*;
import javax.swing.*;

public class test8 {
     public static void main(String... args) {
         EventQueue.invokeLater(() ->
          System.out.println(new JPanel().getLayout()));
     }
}

C:\Users\Knute Johnson>java test8
java.awt.FlowLayout[hgap=5,vgap=5,align=center]

-- 

Knute Johnson

[toc] | [prev] | [next] | [standalone]


#38336

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2018-06-04 02:25 +0200
Message-ID<pf210i$5dt$1@dont-email.me>
In reply to#38334
On 2018-06-03 22:00, Knute Johnson wrote:
> On 5/31/2018 13:05, Daniele Futtorovic wrote:
>> IIRC the default LayoutManager on a JPanel is BorderLayout, and a
> 
> I don't think so...
> 
> import java.awt.*;
> import javax.swing.*;
> 
> public class test8 {
>     public static void main(String... args) {
>         EventQueue.invokeLater(() ->
>          System.out.println(new JPanel().getLayout()));
>     }
> }
> 
> C:\Users\Knute Johnson>java test8
> java.awt.FlowLayout[hgap=5,vgap=5,align=center]
> 

Blimey. All right. Thank you.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#38321

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2018-05-31 15:55 -0400
Message-ID<pepk05$8ku$1@dont-email.me>
In reply to#38315
On 5/31/2018 12:42 PM, Eric Douglas wrote:
> On Thursday, May 31, 2018 at 11:28:43 AM UTC-4, Eric Sosman wrote:
>> On 5/31/2018 10:34 AM, Eric Douglas wrote:
>>> I create a JLabel.  I create a JScrollPane.  I call setLayout(new GridLayout()) on the JLabel, then add the JScrollPane to the JLabel.
>>
>>       Right here is where I simply must ask: "Why?"  Why not just put
>> the scroll pane wherever you were going to put the label?  (Or, why
>> not put the label-plus-scroll as the content of another label, and
>> put that as the content of yet another label, and ...?)
>>
> Because the JLabel is a custom class which wraps every control type, which gives them all a common border, which turns blue if they have focus.  This also makes each control a single control where they may otherwise be multiple controls.  ie a US phone number control has an area code input, a prefix input, a suffix input, and text labels to show the ( ) - mask.  One border shows around all of them.  Putting a single control like this JScrollPane in such a JPanel is to avoid duplicating logic like the border objects and focus listeners.

     Even without the customized JLabel (aside: Why call all your
classes "custom" classes?), there's no need to duplicate the focus
management logic, the border-changing, or the border objects.  You
can attach one focus listener to as many components as you like.

-- 
esosman@comcast-dot-net.invalid
Nine hundred sixty-five days to go.

[toc] | [prev] | [next] | [standalone]


#38335

FromKnute Johnson <nospam@rabbitbrush.frazmtn.com>
Date2018-06-03 15:02 -0500
Message-ID<pf1hhg$7n8$2@dont-email.me>
In reply to#38321
On 5/31/2018 14:55, Eric Sosman wrote:
> Even without the customized JLabel (aside: Why call all your
> classes "custom" classes?), there's no need to duplicate the focus
> management logic, the border-changing, or the border objects.  You
> can attach one focus listener to as many components as you like.
> 

I'm with you, I've used this for years with no trouble at all.

package com.knutejohnson.classes;

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

/**
  * FocusListener to change the background color of a JComponent when it
  * acquires or loses the focus.
  */
public class ColorFocusListener extends FocusAdapter {
     /** Background color of component when it is in focus */
     private final Color in;

     /** Background color of component when it is not in focus */
     private final Color out;

     /**
      * Create a new {@code ColorFocusListener}
      *
      * @param in Color when component is in focus
      * @param out Color when component is not in focus
      */
     public ColorFocusListener(Color in, Color out) {
         this.in = in;
         this.out = out;
     }

     /**
      * Changes the background color when focus is gained
      *
      * @param fe the {@code FocusEvent}
      */
     public void focusGained(FocusEvent fe) {
         JComponent c = (JComponent)fe.getSource();
         c.setBackground(in);
     }

     /**
      * Changes the background color when focus is lost
      *
      * @param fe the {@code FocusEvent}
      */
     public void focusLost(FocusEvent fe) {
         JComponent c = (JComponent)fe.getSource();
         c.setBackground(out);
     }
}



-- 

Knute Johnson

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web