Container resizing when dynamically showing/hiding components

I work with Swing for quite some time, but there's one thing that's bugging me all the time - when you dynamically show/hide some components, the container is not resized appropriately. That means that some components are cut off or hidden beyond the container edge. Maybe I'm just doing something wrong, can somebody help me?
The easiest example is here. I'll create a label that is hidden by default. When I dynamically show it, the frame/panel is not enlarged and therefore the label is not visible until the user manually resizes the frame/panel. Only after that you can see it.
(Usually I use the layout manager used when designing UIs in NetBeans, but I hope these default layout managers will demonstrate the same problem.)
import java.awt.event.*;
import javax.swing.*;
public class DynamicComponentDemo {
    private static void createAndShowGUI() {
        final JFrame frame = new JFrame("Dynamic Component Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        final JPanel panel = new JPanel();
        frame.add(panel);
        final JCheckBox checkbox = new JCheckBox("Show label");
        final JLabel label = new JLabel("This is a label!");
        checkbox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                label.setVisible(checkbox.isSelected());
        label.setVisible(false);
        panel.add(checkbox);
        panel.add(label);
        frame.pack();
        frame.setVisible(true);
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
}What can I do to force the frame/panel to resize appropriately when some new component is shown? Moreover, can you point me to some documentation regarding these matters?
Thanks.

Kleopatra wrote:
no. no. no. no. Never-never-ever call setXXSize - XX for min/pref/max - in application code.Thanks for the correction.
Unfortunately,
http://wiki.java.net/bin/view/Javadesktop/SwingLabsImperialRules
is a dead link, so I'm not sure if this is better than my previous example :(
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DynamicComponentDemo3 {
  private static void createAndShowGUI() {
    final JFrame frame = new JFrame("Dynamic Component Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    final JPanel panel = new JPanel(new FlowLayout() {
      @Override public Dimension preferredLayoutSize(Container target) {
        //synchronized (target.getTreeLock()) {
        Dimension ps = super.preferredLayoutSize(target);
        Dimension cs = target.getSize();
        ps.width  = Math.max(cs.width,  ps.width);
        ps.height = Math.max(cs.height, ps.height);
        return ps;
    frame.add(panel);
    final JCheckBox checkbox = new JCheckBox("Show label");
    final JLabel label = new JLabel("This is a label!");
    checkbox.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
          label.setVisible(checkbox.isSelected());
          frame.pack();
    panel.add(checkbox);
    panel.add(label);
    frame.pack();
    label.setVisible(false);
    frame.setVisible(true);
  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
}

Similar Messages

  • Need to resize window to show all components !!

    I created a simple java application but there is a wiered problem. Unless i resize or maximise the window, I am unable to see any component I added into it. What is the problem with my code.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    public class CdApp implements ActionListener
         //Frame and Panel:-     
         JFrame cdFrame;
         JPanel cdPanel;
         static PreparedStatement val;
         static ResultSet result;
         int flag;
         //Buttons:-
         // Labels :-
         // Text Fields & Areas:-
         // Strings :-
         // Misc Components:-
         // The MainMethod :-
         public static void main(String[] args)
              CdApp cd_object1 = new CdApp();
              cd_object1.main_page();          
         // Methods:-
         // Main Page:-
         void main_page()
              cdPanel = new JPanel();
              cdFrame = new JFrame("CD Catalog Manager ===> Sukrit Mehra!!");
              cdFrame.setSize(800,600);
              cdFrame.setVisible(true);
              cdFrame.getContentPane().add(cdPanel);
              cdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        

    can you cut and paste verbetim the following code into a CdApp.java file and compile it and run it and see if you can see a window 800x600 that displays "Hello I'm a JTextArea!" in the top middle to see if it displays without resizing?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    public class CdApp implements ActionListener{
         JFrame cdFrame;
         JPanel cdPanel;
            JTextArea jta;
            public static void main(String[] args){
                CdApp cd_object1 = new CdApp();
                cd_object1.main_page();
         void main_page(){
                cdPanel = new JPanel();
                cdPanel.add(new JTextArea("Hi I'm a JTextArea!"));
                cdFrame = new JFrame("CD Catalog Manager ===> Sukrit Mehra!!");
                cdFrame.setSize(800,600);                       
                cdFrame.getContentPane().add(cdPanel);
                cdFrame.setVisible(true);                       
                cdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            public void actionPerformed(ActionEvent ae){       
    }

  • When keyboard show-up view didn't resize in Android, ADF Mobile

    Hi Guys,
    Now I was facing with amx page didn't resize when keyboard pop-up, so user cannot scroll for more fields below,
    Btw iOS is normal to resize when keyboard shown.
    How to fix this issue ?

    Hi Frank
    Thanks for reply, I have another issue about android startup performance with release mode, I will send it to you.
    Btw about Android resize view when keyboard show-up, I can fix it but it should include in next release of ADF Mobile.
    My fix is (Thanks for JoeHuang's way to browse Android template file : Google Play Store does not accept ADF Mobile apps for tablets)
    - Navigate to file <JDev Install>/JDeveloper/JDev/Extensions/oracle.adf.mobile/Android/Oracle_ADFmf_Framework.zip
    - Make a backup copy of the zip file
    - Unzip the file, which would extract all files into a "framework" directory
    - Navigate to framework/template/AndroidManifest.template.xml file
    - Change android:windowSoftInputMode value to "adjustResize" Ex. android:windowSoftInputMode="adjustResize"
    - Zip up the framework directory and name zip file as Oracle_ADFmf_Framework.zip, replacing the old one.  Please make sure the zip file has the exact same directory structure as before.
    - In JDeveloper, before deploying the application again, please do a "Clean All" to clean out all old deployment artifacts.
    - Deploy the app again
    After fix, views is automatically resize when keyboard pop-up, but seems weird with navigation bar that will show as it include in view too.
    I don't know why ADF Mobile put android:windowSoftInputMode value as "adjustPan" in manifest file, as Android document said as view will not resize when keyboard showing (Ref: http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft)

  • Resizing height width of adf  components when maximizing browser window

    Hi All,
    I am using JDeveloper 11.1.1.4. I have one one panelCollection and an af:table inside it in my page. The browser window that contains the page will initially have a width and height of 1030,340. When user maximizes the window i want to stretch both the width and height of the components inside it. While doing maximize i want to increase the height and width of the panelcollection and the table inside it.I am able to stetch the width using stretch layout and AFStretchWidth style. But resizing the height of the components is the problem.
    First of all is it possible ? Please help me to get to a conclusion for this.
    Thanks,
    Priya.

    Hi,
    Even if i use panelstretch layout i need two or more compoennts (table,panelgrouplayout,...etc) need to aligned horizontally inside the stretch layout so it is not stretching the height. To solve this problemm i wrote a javascript which catches the resize event of the window and in that i am setting the height of the compoents depending upon the size of the outer most layout. The javascript that i used is
    $(window).bind('resize', function() {             
    var height = document.getElementById('pt1:pt_psl1').offsetHeight;
    var pcHeight = height - 50;
    document.getElementById('pt1:pgl3').style.height = pcHeight + 'px';
    document.getElementById('pt1:pc1').style.height = pcHeight + 'px';
    This works fine in Firefox and chrome browser, but in IE it is not working. That is in IE the method is not getting invoked while clicking on maximize button of the window. Does anybody have any solution for this ?

  • Dynamic text resizes when loaded?

    I made sure to set the stage to noScale but I am still having
    a strange problem that is driving my nuts. If I make a static text
    box it will not resizing when I compile and preview or upload the
    swf. However, if I make a dynamic text box, the text size increases
    slightly when i complile the swf. This makes fine tuning the layout
    of web pages extremely difficult because what i see on the stage is
    not the same size as what i see when the swf is complied. Has any
    one else had this problem or know how to solve it because its
    really starting to drive me nuts?

    The only time you should see a size difference is when you
    take text out of a movie clip that has been scaled. Hit Crtl-t to
    get the Transform window and see if the object is %100

  • Hi,  We are developing quite a few excel worksheet reports. The reports dont show the cells if contain formulas ( when opened in iPad or ipod  ),  is there any good solutions .

    Hi, 
    We are developing quite a few excel worksheet reports.
    The reports dont show the cells if contain formulas ( when opened in iPad or ipod  ),
    Is there any good solutions .
    Thanks.

    There is no version of Excel for iPads.  iPads come with something that lets you view the contents of Excel files but it implements only the common easy features and there are (literally) hundreds of things you can do in Excel that it doesn't know how to show on the screen, including some functions.
    If you intend to work with spreadsheets on an iPad I suggest you buy Numbers for iPad
    http://www.apple.com/apps/iwork/numbers/
    which understands more (but still not 100%) of the things that Excel does.  It is so cheap that you can probably afford to buy a copy just to see whether it does what you want.
    If you are doing things with spreadsheets and intend to be primarily working on iPads then I suggest you actually use Numbers on a Mac as well, rather than using Excel at all.

  • LV2012 crashes when opening LV2010 project containing OOP and dynamic FPGA references

    I have a project that was written under LV2010 and has no problems that I am aware of. This project contains OOP and dynamic FPGA references. I am trying to upgrade this project to LV2012. When opening this project under LV2012 (or any subVI containing OOP) I get an error message, as shown in the attached screenshot. Has anyone else observed this problem? I haven't found any relevant postings on this forum yet.
    My project is quite large, with many accessors.
    Thank you for any assistance you can offer.
    Attachments:
    LV2012_Crash.PNG ‏22 KB

    Thank you for your response, but the Discussion Forums link that you provided refers to a problem that occurs when exiting a running executable. My problem is different. I am simply opening up a LV2010 project using LV2012. No running of my program is involved.
    Anyway, after several days of work, I narrowed down the cause of the LabVIEW crash. My code contained a Disable structure, and within that Disable structure there was a subVI that had an unwired object terminal (set to dynamic dispatch). Deleting the subVI in the Disable structure eliminated the LabVIEW crashing. Weird behavior.

  • Showing/Hiding Dashboard Prompts dynamically based on User logged on

    Hi
    I have query reg a showing/hiding dashboard prompts based on Users logged in.
    What is the best way???
    Please help
    And also anothe quick question
    Is there a way that we can change the Label of Dashboard prompt "ABC" for few users and "DEF" for other users
    Thanks in advance
    Cheers

    Hello,
    Using guided navigation properties for section, we can achieve your first requirement.
    How many prompts to be shown?
    depends on that number, you have to create those many reports in answers like,
    take one column to criteria and change functionaityt to case when 1=0 then Markets. Region else user() end
    apply filter on this equals to UserA (for userA), say ReportA
    for UserB filter has to change to UserB, say ReportB...etc
    Now, add all the prompts to dashboard, but each section (dashboard object) for each prompt.
    Go to section properties of each and select guided navigation property. The section which is holding the PromptA (should be shown to userA) then browse the reportA for guided navigation and click ok
    LIke this do for all prompts.
    Hope you understood...
    if any queries ask me..
    And for 2nd Q, in prompt, you have Select captio option which is the last option for all prompts,
    Write something there and check

  • Dynamically show/hide pages in a workset for special users at runtime

    Hi all,
    I want to create a workset that contains pages for different countries. The portal users should only be able to see the countries where they do business. I also would like masterdata from R/3 to drive the display of content on the portal.
    - Is there a way how I could dynamically show/hide certain pages of the workset for the specific users?
    - Would this showing/hiding of pages affect the workset for all users or only the instance for the specific user?
    - Would there be a difference when I want to dynamically show/hide iviews in a page instead of showing/hiding pages in a workset?
    Thanks a lot in advance.

    Hi Timo,
    Check out the article located at:
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/ep/portal_content/filtering role and workset content.htm
    It talks to exactly what you're trying to do.  I'd imagine you could extend this simplistic example to have the service pull the users country from his/her master record in R/3 and set their filtering expression accordingly.
    Hope this helps,
    Marty

  • Slideshow image resizing when adding new images

    I am creating a series of slideshows on multiple pages. I created one slide show using the "basic" slideshow and resized it to the dimensions and settings I wanted. I have many pictures all of different proportion, therefore, I selected the "fill frame proportionally" so they would all fit the dimension I set. I wanted to use this first slideshow as a template for all of the rest. I added images to this first slideshow with no problems. All of my different sized images scaled or cropped to fit within the dimension I set. The problem comes in when I do two things: 1) When I add other images of different dimensions to this same slideshow gallery, they come smaller that the intended dimensions I set previous. I check the setting and it is still on "fill frame proportionally" similar to the first batch of pictures. 2) The second issue is when copy this slideshow as a template to other pages. When I try to replace or add to the slideshow gallery, the images come in cropped or smaller rather than filling the frame. Again, the settings are still the same from my very first slideshow that worked just as i intended.
    I could resize all the images to all the same dimensions using another program like photoshop, but that is another step that is very tedious and it would seem that it should be something built into Muse.
    Is there a way around this?. Am I doing something wrong? Or is this just one of those glitches that happens with Muse? I appreciate any help that I can get.
    Thanks!

    Hi, I got it to work like this:
    Using background colours in Photoshop so that all sizes are the same in pixels. Then manually adjusting thumbnails by double-clicking on them so that a red square appears.
    Cheers,
    Elsemiek
    Op 26 dec. 2014, om 00:50 heeft MediaGraphics <[email protected]> het volgende geschreven:
    slideshow image resizing when adding new images
    created by MediaGraphics <https://forums.adobe.com/people/MediaGraphics> in Adobe Muse Bugs - View the full discussion <https://forums.adobe.com/message/7043933#7043933>
    Hi there Elsemiekagain,
    I had to fiddle around with my slide show to get it to work. That is, it worked at first, then went funky, and I had to fiddle. So much fiddling that I can't possibly know what actually made it start to work again.
    And to some degree, this is the way that I find Muse to be in general. That it requires finessing to get it to work as expected. This adds a good deal of time to every development project, though I am getting better at this with practice and experience.
    Most of it is not even things that could be easily put in words as instructions, as many are nanced. But in fairness, this version of Muse is a complete code re-write this year. So we do need to cut Adobe some slack, and give the team time to iron things out.
    If the reply above answers your question, please take a moment to mark this answer as correct by visiting: https://forums.adobe.com/message/7043933#7043933 and clicking ‘Correct’ below the answer
    Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
    Please note that the Adobe Forums do not accept email attachments. If you want to embed an image in your message please visit the thread in the forum and click the camera icon: https://forums.adobe.com/message/7043933#7043933
    To unsubscribe from this thread, please visit the message page at , click "Following" at the top right, & "Stop Following"
    Start a new discussion in Adobe Muse Bugs by email <mailto:[email protected]ftware.com> or at Adobe Community <https://forums.adobe.com/choose-container.jspa?contentType=1&containerType=14&container=47 59>
    For more information about maintaining your forum email notifications please go to https://forums.adobe.com/thread/1516624 <https://forums.adobe.com/thread/1516624>.

  • Containers of Container - Resize

    My application contains one HBox component and HBox has 3
    (these numbers are not fixed) VBox as its children. VBox in turn
    contains Canvas, Button & Label. Something like the following:
    Application
    -->HBox
    ------>VBOX
    ------------>
    Canvas
    ------------>Button
    ------------>Label
    ------>VBOX
    ------------>
    Canvas
    ------------>Button
    ------------>Label
    ------>VBOX
    ------------>
    Canvas
    ------------>Button
    ------------>Label
    On runtime I need to add another copy the same HBox (which
    has 3 or more VBox) into the Canvas which is inside the VBox. After
    adding the application will be something like the following
    Application
    -->HBox
    ------>VBOX
    ------------>
    Canvas
    ---------------------->HBox
    -------------------------->VBOX
    -------------------------------->
    Canvas
    -------------------------------->Button
    -------------------------------->Label
    -------------------------->VBOX
    -------------------------------->
    Canvas
    -------------------------------->Button
    -------------------------------->Label
    -------------------------->VBOX
    -------------------------------->
    Canvas
    -------------------------------->Button
    -------------------------------->Label
    ------------>Button
    ------------>Label
    ------>VBOX
    ------------>
    Canvas
    ------------>Button
    ------------>Label
    ------>VBOX
    ------------>
    Canvas
    ------------>Button
    ------------>Label
    Until here is fine.
    I need to resize the canvas depends upon the size of HBox
    after adding the Vbox Component. The size of the HBox will vary
    what is there in the VBox.
    Depends on the XML data I need to adding HBox into the Canvas
    of VBox so I need a logic to built a custom component.
    Please let me know how do I know the width & height of
    HBox component while I add it to a Canvas so that I can resize the
    Canvas.
    I hope I explained my query well.
    Thanks

    "MVKVIVEK" <[email protected]> wrote in
    message
    news:[email protected]...
    > My application contains one HBox component and HBox has
    3 (these numbers
    > are
    > not fixed) VBox as its children. VBox in turn contains
    Canvas, Button &
    > Label.
    > Something like the following:
    >
    > Application
    > -->HBox
    > ------>VBOX
    > ------------>
    Canvas
    > ------------>Button
    > ------------>Label
    > ------>VBOX
    > ------------>
    Canvas
    > ------------>Button
    > ------------>Label
    > ------>VBOX
    > ------------>
    Canvas
    > ------------>Button
    > ------------>Label
    >
    > On runtime I need to add another copy the same HBox
    (which has 3 or more
    > VBox)
    > into the Canvas which is inside the VBox. After adding
    the application
    > will be
    > something like the following
    >
    >
    > Application
    > -->HBox
    > ------>VBOX
    > ------------>
    Canvas
    > ---------------------->HBox
    > -------------------------->VBOX
    > -------------------------------->
    Canvas
    > -------------------------------->Button
    > -------------------------------->Label
    > -------------------------->VBOX
    > -------------------------------->
    Canvas
    > -------------------------------->Button
    > -------------------------------->Label
    > -------------------------->VBOX
    > -------------------------------->
    Canvas
    > -------------------------------->Button
    > -------------------------------->Label
    > ------------>Button
    > ------------>Label
    > ------>VBOX
    > ------------>
    Canvas
    > ------------>Button
    > ------------>Label
    > ------>VBOX
    > ------------>
    Canvas
    > ------------>Button
    > ------------>Label
    >
    > Until here is fine.
    >
    > I need to resize the canvas depends upon the size of
    HBox after adding the
    > Vbox Component. The size of the HBox will vary what is
    there in the VBox.
    >
    > Depends on the XML data I need to adding HBox into the
    Canvas of VBox so I
    > need a logic to built a custom component.
    >
    > Please let me know how do I know the width & height
    of HBox component
    > while I
    > add it to a Canvas so that I can resize the Canvas.
    >
    > I hope I explained my query well.
    Containers automatically size themselves around their
    children. For the
    dynamic insertion of components, look at a repeater.
    HTH;
    Amy

  • InContext editing error: This region contains content with dynamic behavior added..

    We just set up a web site with InContext editing following the procedures in Business Catalyst documentation to do that. But now when we go to the administration section of the site, and turn on InContext editing, it won't let us update the text. We receive an error message:
    "This region contains content with dynamic behavior added..."
    Does anyone know what causes this problem, and how to fix it?

    Hi,
    InContext Editable (ICE) regions can only contains static areas like text and static  assets, it cannot wrap around javascript – populated areas or dynamic content like slideshows. (most likely causing the error)
    ICE can edit only static page content such as regular HTML content. It cannot edit any element that is dynamically updated for example a div (or another element) which is manipulated (created or its contents are modified) by JS– populated areas or dynamic content like slideshows. It cannot edit menus, content holders or content outputted by system modules or tags either.
    By default, on a page that has no ICE editable regions defined the editor attempts to find any regions you might want to edit and if possible make them editable. If it does not find the correct regions you want to edit you will need to explicitly define some editable regions.
    Refer to this article on Defining InContext Editing editable regions - http://kb.worldsecuresystems.com/838/cpsid_83855.html?bc-partner
    Hope this helps!
    -Sidney

  • Is there any event that triggers when you switch between components?

    is there any event that triggers when you switch between components?
    like for example... when i switch between components, i need a function to fire everytime i switch...
    ive tried show, hide, and initialize... and none of them work...
    any ideas or help is greately appretiated!!

    Depends on what you are trying to do,
    you have mouseover and mouseout and mouseclick events which you can use on the component and write code to deal with what you want to happen when you change focus. You have keypress event which would allow you to monitor tabbing between components, remember that in most events the currentTarget and target values will give you the information you need to handle component interaction.
    so
    [Bindable] private var CurrentObject: Object; <-------  use this as a pointer to what ever object you are currently focused on or just to indicate things have changed
    addeventListener(MouseEvent.CLICK,myCompponentSwap);
    protected function myComponentSwap(e:event): void
        if (CurrentObject !=e.currentTarget && CurrentObject != null)
             do what you want becuase a component switch just happened
        CurrentObject = e.currentTarget;
    Hope this gets you started.
    David

  • Do photos get resized when sending from PSE 6 to PE 4?

    Do photos get resized when moving them from PSE 6 to PE4?
    I've come to understand that using overly large photos in slide shows in PE 4 can cause lots of issues, and its recommended to resize photos to 1000x750 (http://www.adobeforums.com/webx/.3bb8822c) before including them in a slide show in PE4. I'm trying to understand the implications of working with photos in PSE 6 and then moving to PE 4 vs. just doing it directly in PE 4.
    I can imagine (at least) two scenarios:
    1. I have all my photos in PE6 and want to work ONLY in PE4. I'm guessing i would resize my photos in the PSE 6 Organizer by using File-->Export and picking a smaller file size (e.g., 1024 x 768) and then import them into PE4 and build my slide show there.
    2. I have all my photos in PSE 6 and want to to create a slide show there and then move it to PE 4 to touch it up or do some fancy stuff to it. Will the photos be resized so PE 4 won't have trouble with them or do i need to resize them in PSE 6, reimport them into PSE 6, build the slide show, and THEN send it to PE 4?

    eric,
    See my post over at http://www.adobeforums.com/webx/.59b85fd0/4
    for reasons why I think that for your current objectives, working in Premiere Elements (rather than making a slide show in PSE) will be a better choice.
    >1. I have all my photos in PE6 and want to work ONLY in PE4. I'm guessing i would resize my photos in the PSE 6 Organizer by using File-->Export and picking a smaller file size (e.g., 1024 x 768) and then import them into PE4 and build my slide show there.
    Yes.
    A couple of considerations
    1- Where will you establish the sequence of the photos in the slide show?
    Are you making an Album in PSE 6 in order to establish the sequence of the slides as you are selecting which photos to use? if YES, we can experiment with sending the photo files in their Album sequence to Premiere Elements.
    Or will you establish the sequence of the photos within Premiere Elements?
    2- When working in Premiere Elements will you be able to do the 600 slides in a single PE project?
    I don't know the answer to this: it will be influenced by your system configuration.

  • View State Best Practice for Hiding Components?

    When changing states and hiding components, what is the best practice?
    Set Height/Width=0?
    Set Visible/IncludeInLayout=False?
    Or is there a better way?

    Good point. That's what I thought but I'm working through 'Adobe Flex 3 Training from the Source' and they explicitly set width/height to 0 in the new state so I thought I was not doing it correctly.

Maybe you are looking for