JTextField border part of preferred size?

In an attempt to set the height of a JPanel just tall enough to accommodate its tallest component, a JTextField, I accounted for the preferred height of the JTextField and the insets of the JPanel. However, this failed to provide sufficient height to see (at least) the bottom of the JTextField border.
I had ignored the height of that border (i.e. the border insets), believing that its height would have been included in the preferred height of the JTextField. Was I wrong?
JTextField.getPreferredSize()
Returns the preferred size Dimensions needed for this TextField.
JComponent.getBorder()
Returns the border of this component or null if no border is currently set.
In the Swing component set, borders supercede Insets as the mechanism for creating a (decorated or plain) area around the edge of a component.
Border.getBorderInsets(Component c)
Returns the insets of the border.
Alan Feldstein
Cosmic Horizon
http://www.alanfeldstein.com/

Alan_Feldstein wrote:
4gui wrote:
Which layout do you use?
you must use the layout to locate the componet on the place you want.The JTextFields's container ... has a flow layout.4gui got me thinking about what I should have been thinking about after all. In fact, method FlowLayout.getVgap gives the "vertical gap ... between the components and the borders of the Container." I needed to compensate for that too.
>
When I inquire about the JTextField's preferred size, will the numbers include the JTextField's border or not?I think so. It has always made more sense to me to consider the borders to be part of a Component. Now I have seen more evidence in the documentation to support this.
For example, method FlowLayout.getVgap "gets the vertical gap ... between the components and the borders *of the* Container." Those borders belong to the Container, not to anything outside the Container.
Method JComponent.getInsets says, "If a border has been set on this component, returns the border's insets ...". My Container is a JComponent and I said before that I had already compensated for its Insets (i.e. the size of its border).
Since the Container's border is part of the Container, for consistency the JTextField's border would be part of the JTextField.
I had been looking for a way to explain a discrepancy, by considering the theory that the JTextField's preferred size did not include its border, a theory I was never comfortable with. It turns out that the discrepancy came from failure to compensate for the vertical gap in FlowLayout.
Since 4gui was on the right track, all of the remaining Duke Stars go to him/her.

Similar Messages

  • Preferred size of JEditorPane/ HTML page

    Hi,
    I've loaded an HTML page in a JEditorPane. Since I'm using a layout manager with absolute positioning, I need to know the pane's preferred size.But the preferred size I get is just about (6, 6), although it has a big picture in it. I tried validate() first or to add it with (1,1) to have it set up properly (visible etc.) and then resize it afterwards, but it had no effect. Is there a way to get a preferred size of an HTML page?
    Thanks a lot.
    Greets
    Puce
    Note: Using another layout manager is not a option, since this is a very specialized task.

    I could use this as well. I'm tiling JEditorPanes vertically inside a JPanel using BoxLayout. The problem is that I need to limit the width of the panel, so I need to adjust the sizes accordingly. Any suggestions? I'll give another duke...
    Max

  • JScrollPane content changes preferred size; how to show the bars

    Before I have to delve deeply into the code of JScrollPane... I have a component inside a JScrollPane with as-needed scrollbars that changes its preferred size. I can invalidate and repaint every component in the tree, but the scrollbars don't show until I do a hide and reshow the window. Why?

    if you are happy with an answer - even your own self-muttering <g> please mark the question as answered.
    Cheers
    Jeanette

  • How preferred size of JAVA GUI is calculated when the parent component is invisible?

    Hi
    Suppose a Panel has a tabbed pane with two tabs inside it.  If I do setVisible(false) for 2nd tab but not for its child components,
    Will the size of child component's of 2nd tab be considered to calculate the preferred size of the TopPanel ?
    Similarly,  If a Panel has a sub-panel and sub-panel has few child components. If sub-panel is invisible but its child components are visible, they will not be shown on the screen.
    In this case, Will the sizes of sub-panel's child components be considered for calculating the preferred size of the outer panel ?
    Please let me know
    Thanks in advance.

    Are you talking about Java or another language?
    However, when I run this report, JVM tries to allocate these 10 MB objects, but causes an Allocation Failure (AF). Java does get such an error. It can get an OutOfMemoryError.
    At this point, the unusable heap size due to fragmentation is ~100 MB. Again Java doesn't get unusable heap space as the GC will repack the memory to remove fragmentation.
    - is 10 MB object size okay? Is it normal to have object size of 10 MB?In Java it is normal.
    - How can I reduce the fragmentation in the memory?Use Java.

  • Determine preferred size to be set for the JPanel attached to JScrollBar

    Determine what preferred size to be set for the JPanel attached to JScrollBar.
    Hello all, I am having a JPanel, where new components will be added into it during run-time. When there are too many components inside the JPanel, I need to re-set the JPanel preferred size so that the JScrollBar is scrollable.
    I was wondering what is the good method to determine the preferred size of JPanel during runtime?
    Currently, I am using
    myJPanel.setPreferredSize(new Dimension(
    numberOfNewComponent * preferred width of each component,
    numberOfNewComponent * preferred height of each component
    ));Is there any better alternative?
    Thank you very much.
    yccheok

    Usually a container such as JPanel will calculate its own preferred size from its layout manager - calling setPreferredSize is often unnecessary.
    When you add a component to your JPanel its preferred size will change automatically.
    What's wrong with the behaviour when you simply add your JPanel to a JScrollPane and let it manage its own preferred size?
    Hope this helps.

  • Update JScrollBar Extent when JScrollPane Component Changes Preferred Size

    Hi folks,
    I have an interesting, but concise problem that I've been working on for a few days but haven't had any luck.
    In Java 1.5 or Java6, I have a JScrollPane which contains a JPanel. The settings of the scrollbar (for example, the Extent [the width of the "thumb" or "slider" on the scrollbar]) are determined based on the dimension of the underlying contained component, in particular, the Preferred Size.
    My problem is this. The underlying component has a "zoom" capability, such that the actual size of the component can and does change (i.e., zooming out reduces its preferred size).
    Happily, the consequence of this design is that the "size" or extent of the scrollbar sliders/thumbs adjusts to give visual indication of the proportion of the current view (ViewPort View dimension) to the underlying component's dimension.
    The problem is, the scrollbar sliders do NOT automatically update their size in response to programatically changing the JScrollPane's contained component's PreferredSize. They WILL be updated if I RESIZE the parent JFrame manually.
    But for the life of me, I can't get those sliders to update programatically. I've tried repaint(), update, validate(), etc. on the JScrollPane but no luck.
    I've done a debug to get into the stack trace of the Sun code during run time, and there's a lot going on... there's a doLayout(), a reshape() (deprecated), firing various property changes, but I just can't seem to find a good hook into getting the scrollbar to update its internal Bounds model and repaint accordingly. Calling setBounds() on the JScrollPane I think would trigger it, however, looking at the code.. it seems to ignore firing property events and repainting of the bounds themselves didn't actually change (i.e. no action happens if the current dimension and specified dimension in the argument to setBounds() are the same).
    Any ideas here on how to this to get those sliders to update programatically with a new value for the extent?
    Thanks,
    --Mike                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Understood! It was my intention to give credit and now I'm happy to do so! I've now assigned the Duke Points. Minor usability issue, it was not obvious how to do this the first time (and I did poke around a little before I gave up earlier in the day, reverting to just assigning him the correct question). I've got it now though! Thanks again--definitely knocked out an issue I was having today and allowed me to move on to add'l development work today.
    All the best!
    --Mike                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How does JEditorPane determine its preferred size?

    Could someone explain how JEditorPane determines the initial preferred
    size? Its default sizing is not giving me what I need and I'm not sure
    how to control it, other than explicitly setting the size with
    setPreferredSize().
    A simple example:
            String s = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 10 20" +
                       "21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39";
            JEditorPane ed = new JEditorPane("text",s);
            JPanel pan = new JPanel();
            pan.setLayout(new BoxLayout(pan,BoxLayout.Y_AXIS));
            pan.add(ed);
            JFrame f = new JFrame();
            f.getContentPane().add(pan);
            f.pack();
            f.setVisible(true);This code generates a window that shows one line, with "1 2 3 4 5 6 7 8 9"
    showing. I'd like to have a larger area showing initially.
    Does the choice of layout manager effect this? (I actually prefer to use
    TableLayout; the above example with a 1x1 TableLayout and widths set to
    PREFERRED yields exactly the same results.)
    Thanks,
    bw

    Set your ruler increments to pixels Preferences>Units & Increments. You can fill the text box with placeholder text Type>Fill with Placeholder text and get a word count from the Info panel with Show Options turned on from the flyout.
    From the Transform panel you can set a text box's width and height

  • Use of minimum, maximum and preferred size?

    Hi,
    What is the use of the three sizes (maximum size, minimun size and preferred size) of component and container?
    And which class use them?
    Thanks for any ideas and input,
    Wing

    I read somewhere on this forum that most layout managers pretty much ignore these properties... just one of them would properly use it (don't know which one, I think it was one of the 'less-common' ones like Card or BoxLayout).
    If somebody has more info I would wanna read it too!

  • Problem with setting preferred size

    Why in the following code the preferred size doesn't change ?
    import java.awt.*;
    import javax.swing.*;
    public class MyTest extends JFrame {
         private GridBagLayout gridBagLayout = new GridBagLayout();
         private JPanel panel = new JPanel(gridBagLayout);
         private JScrollPane scrollPane = new JScrollPane(panel);
         private JTable table;
         public MyTest() {
              table = new JTable(2, 3);
              panel.add(table);
              add(scrollPane);
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              pack();
              setVisible(true);
         public static void main(String[] args) {
              MyTest frame = new MyTest();
              frame.scrollPane.setPreferredSize(new Dimension(700, 500));
    }

    Hi...
    The problem in your code is this:MyTest frame = new MyTest();
    frame.scrollPane.setPreferredSize(new Dimension(700, 500));Here you give the PreferredSize after create the frame... you have to do this before create the frame...
    Try to use like this:import java.awt.*;
    import javax.swing.*;
    public class MyTest extends JFrame {
         private GridBagLayout gridBagLayout = new GridBagLayout();
         private JScrollPane scrollPane;
         private JTable table;
         public MyTest() {
              table = new JTable(2, 3);
              scrollPane = new JScrollPane(table);
                    scrollPane.setPreferredSize(new Dimension(700, 500));
              add(scrollPane);               
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              pack();
              setVisible(true);
         public static void main(String[] args) {
              MyTest frame = new MyTest();          
    }Regards...

  • Why is my panel's preferred size changing as I resize the window?

    I have a JEditorPane inside a JPanel, which is wrapped in a JScrollPane. The JEditorPane contains a wide image and I expect to see horizontal scrollbars if the viewport is smaller than the minimum size needed to display the image.
    When my panel is displayed for the first time and the viewport size is smaller than the image, the horizontal scrollbars appear and the size of the scrollable area is set to the width of the image. This is the expected behaviour. When I resize the panel so that its is wider than the image and the scrollbars are no longer needed, the scrollbars disappear. However, when I shrink the panel back, the scrollbars appear at the correct "time" (when the edge of the image is reached), but the scrollable area is too wide, similar to the size I had stretched the panel out to.
    I am printing out the preferred size that the panel is returning (which will be used to set the scrollable area), and it seems to keep changing. The preferred size seems to grow with the panel's actual size, but it does not shrink back when I make the panel smaller.
    Anyone has any idea on what might be happening?

    Welcome to the Sun forums.
    >
    Anyone has any idea on what might be happening?>It probably has to do with the Java code used. Can you prepare an SSCCE that shows the behaviour?

  • ScrollPane fill width and preferred size

    I suspect this is going to end up as a feature request, but does anyone know if there is an existing way to have a ScrollPane stretch its content if the available space is greater than the preferred size of the content, but also not shrink the content if the scrollpane is smaller than the preferred size of the content?
    If we have this simple example:
    public void start(Stage stage) throws Exception
        BorderPane contentPane = new BorderPane();
        contentPane.setPrefSize(400, 300);
        contentPane.setStyle("-fx-background-color: blue");
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(contentPane);
        Scene scene = new Scene(scrollPane, 800, 600);
        stage.setScene(scene);
        stage.show();
    } When the window is big, our content pane is left at its preferred size with white space around it and no scroll bars showing. When the window is small our contentPane is still at its preferred size with scrollbars on it and no white space. Much as we'd expect.
    If we add this line of code:
    scrollPane.setFitToWidth(true);Then when the window is big then our contentPane is stretched nicely to fill the available space. When the window is small, the scroll bars don't kick-in (effectively scrolling is turned off) and the contentPane is shrunk to fit the available space.
    What I'd rather have is when the window is small we get scrolling, when the window is big, we get stretching. Possible?
    Cheers,
    zonski

    It is actually possible to do this in markup as well. For example, the following markup creates an observable array list and attaches a controller-based change event handler to it:
    <FXCollections fx:factory="observableArrayList" onChange="#handleListChangeEvent"/> The controller method should look something like this:
    @FXML
    protected void handleListChangeEvent(ObservableListChangeEvent<?> event) {
        // Handle the change event
    }[edit] Note that ObservableListChangeEvent is currently a private class - please feel free to submit a feature request to make this class public if you would like.

  • Determining preferred size of a container

    Hi,
    Does anyone know how to determine the preferred size of a
    container that hasn't had it's height/width set?
    There are two aspects to this:
    1. I would like to resize a container from 0,0 height/width
    to it's preferred size.
    2. Sometimes the children of the container will be larger
    than any arbitrary size that I specify so I need to know when this
    occurs so I can expand the size accordingly.
    Anyone any ideas how to do this?
    I've tried properties:
    height/width
    explicitHeight/explicitWidth
    measuredHeight/measuredWidth
    all to no avail. They all return 0,0 even if I don't specify
    the initial height/width to 0 and have added it to the parent
    container (in this case a WindowedApplication).
    Any suggestions would be appreciated :)
    Thanks,
    Gary

    I got around item 1, by using a zoom effect, thus:
    var z:Zoom = new Zoom (container);
    z.duration = 2000;
    z.easingFunction = Bounce.easeOut;
    z.zoomWidthFrom = 0.01;
    z.zoomHeightFrom = 0.01;
    z.zoomHeightTo = 1;
    z.zoomWidthTo = 1;
    z.play ();

  • Container preferred size (or size in general)

    If you can get a containers preferred size why can't you set it using
    setPreferredSize(new Dimension(int, int));
    or
    size(int, int)Specific use...
    Container screen; //instance var
    screen=getContentPane(); // init
    I would like to know how to set A containers size given the following
    Have tried many different variations.... This container is in a class in a Java application. (it isn't the main class) It won't auto size itself to a JPanel included inside. (which was added to the Container) (Also plan to add some buttons outside of this panel, but on the container)

    The first error message is telling us that, in the Container api, the method setPreferredSize cannot be resolved, ie, the compiler can't find it. This is when we turn to the api for some research.
    In the Container [url http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Container.html]api, look for setPreferredSize. We scroll down to the Method Summary section and again down to the Methods inherited from the class java.awt.Component section...
    It doesn't appear in either section >> not used/available for the Container class.
    The next two errors are telling us that the size method cannot be used with (int,int) or (dimension) arguments.
    So, in the Component [url http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html]api, scroll down to the Method Summary section and look for size:
    we see that the size() method takes no arguments. Okay, that explains the complaints from the compiler.
    Also, we see that the size() method is deprecated - not to be used anymore. It has been replaced by getSize.
    So let's try the setSize method.

  • FlowLayout  &  preferred size

    ".....FlowLayout always honors the components's preferred size..."
    what is the meaning of this phrase ? what is prefered size ? i know that flowlayout puts components from left to right .( centre allign).
    what does that phrase means ?

    Each component has a method called getPrefferedSize and also one called getMinimumSize which asks the compent what size is wants to be, and what size it needs to be.
    In fact if you want to change the size of a component within a layout the proper way is to create your own subclass of the componet class which overrides these methods.
    The layout manager asks each child component of the container it's laying out for their preffered sizes before deciding how to lay them out and what actual size to set for them.

  • Message body part inputstream wrong size

    Hi to everyone,
    I'm trying to download a message (created with JavaMail) from a server: the attachment is 345.302 byte, but when I get the InputStream of the part, the available() method returns "354388".
    This is the code:
    // getting the message multipart
    Multipart mp = (Multipart)message.getContent();
    // getting the stream
    InputStream in = mp.getBodyPart(1).getInputStream();
    // showing the attachment size
    System.out.println(in.available());
    // reading the content
    byte[] data = new byte[in.available()];
    in.read(data);
    // closing the stream
    in.close();When I download the message with Thunderbird, the size of the attachment is correct... and when I store the attachment with Java, there are a few bytes at the end of the file (empty values).
    Is it a bug? Am I using wrong methods?
    Thank you for intresting

    Just to be clear, the available() method always returns only an estimate
    of the number of bytes that can be read without blocking. In the case of encoded
    data in a mail message, there's no way to determine how many bytes exist
    after decoding without reading all the data and decoding it. You certainly
    wouldn't want JavaMail to do that and throw away all the data so that you
    could read it again yourself. The approach you figured out is the correct
    approach, although you might want to consider reading a buffer at a time
    instead of a byte at a time.

Maybe you are looking for

  • Can not refresh the metrics in schedular

    Post Author: sachinddalal CA Forum: Performance Management and Dashboards Hi, When I refresh the metric using schedular, the metric is not refreshed , nor schedular return any error. But when checked in metrics proparties, the last refresh date is no

  • Locating source file NAME from the executable file?

    Hi all,  I wrote a vi a couple of year ago (call it file.vi), and created an executable out of it using the Application Builder (call it newfile.exe). I know I renamed the file when I created the executable and now I cannot remember what I called the

  • Editable Title Bar in a JFrame?

    I have a standalone application that launches a JFrame. Is there some way to allow the user to set the title of the JFrame?

  • PPCC 2014 is not showing up for download

    in fact, a LOT is not showing up at the cloud menu for download, nor is it listing my currently downloaded products. what's up?

  • SAP-SHRWEB and custom application

    Hi, I have been going through the Development and Extension guide and everything looks logical. However it looks to me that if you want to change a jsp file ( for instance the landing page) you will have to change it in the standard SAP-SHRWEB (Figur