Results 1 to 14 of 14
  1. #1
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Keeping the dimensions of a JPanel.

    Hi, I have a JDialog with JPanel cPane as contentPane
    and cPane contains 2 JPanels
    mPanel and sPanel
    mPanel contains a number of labels and buttons and
    sPanel contains scrollpanel(with an JTextArea ect)

    They are organised like:

    Java Code:
    +JDialog-------------------------+
    |+cPane-------------------------+|
    ||+----------------------------+||
    ||| mPanel                     |||
    ||| cPane.add(mPanel, CENTER)  |||
    |||                            |||
    ||+----------------------------+||
    ||| sPanel                     |||
    ||| cPane.add(sPanel, SOUTH)   |||
    |||                            |||
    ||+----------------------------+||
    |+------------------------------+|
    +--------------------------------+
    Sometimes I want to see more of sPanel and I can do that with dragging the border of the JDialog Down
    and if I want to see less of sPanel I drag the border up.

    At the moment if I do that not only the dimensions of sPanel change also the dimensions of mPanel change.

    And I would like that the height of cPanel stays the same.

    How can I do this?

    Every so often values in mPanel get updated, if it is not possible to prevent the height of mPanel from changing, I would like to reset the dimensions of mPanel in the update routine.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,406
    Rep Power
    20

    Default Re: Keeping the dimensions of a JPanel.

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,406
    Rep Power
    20

    Default Re: Keeping the dimensions of a JPanel.

    Quote Originally Posted by willemien View Post
    And I would like that the height of cPanel stays the same.
    I don't see a cPanel anywhere in your ASCII art. Apart form that, appropriate preferredSizes and a GridBagLayout with suitable weighty settings for the constraints might do the trick.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,406
    Rep Power
    20

    Default Re: Keeping the dimensions of a JPanel.

    ... or maybe you want a JSplitPane.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Keeping the dimensions of a JPanel.

    sorry I start again

    I have the code

    Java Code:
    public class DialogTest{
      
      private final JDialog dialog ;
      
      // main panels
      
      private final JPanel cPane;
      private JPanel sPanel;
      private JPanel mPanel; 
      
        
      //Constructor
      public DialogTest(){ 
        dialog = new JDialog(ParentFrame.getJFrame(), "dialog", false);
        cPane = new JPanel(new BorderLayout());
        dialog.setContentPane(cPane);
        // no show Dialog here
      } 
      
      public void setMpanel(MainSwing mainSwing){
        assert (mPanel == null) : " only set once!!";    
        mPanel = mainSwing.getPane();
        cPane.add(mPanel, BorderLayout.CENTER);     
        showDialog();
      }
      
      public void setSpanel(ShowSwing showSwing){ 
        assert (sPanel == null) : " only set once!!";
        assert (mPanel != null) : " set mPanel First!!";   
        sPanel = showSwing.getPane();
        cPane.add(sPanel, BorderLayout.SOUTH);     
        showDialog();
      }
    
      public void showDialog(){
        assert (mPanel != null) : " set mPanel First!!" ;
        SwingUtilities.invokeLater(new Runnable(){
          public void run() {       
            dialog.pack();
            dialog.toFront();
            dialog.setVisible(true);         
          }
        });
      }
    }

    This gives a dialog like:

    Java Code:
    +JDialog-------------------------+
    |+cPane-------------------------+|
    ||+----------------------------+||
    ||| mPanel                     |||
    ||| cPane.add(mPanel, CENTER)  |||
    |||                            |||
    ||+----------------------------+||
    ||| sPanel                     |||
    ||| cPane.add(sPanel, SOUTH)   |||
    |||                            |||
    ||+----------------------------+||
    |+------------------------------+|
    +--------------------------------+

    sPanel containst a JscrollPane, containing a JTextArea and sometimes I want to see more of sPanel and I can do that with dragging the border of the JDialog Down
    and if I want to see less of sPanel I drag the border up.

    At the moment if I do that not only the dimensions of sPanel change also the dimensions of mPanel change.

    And I would like that the height of mPanel stays the same.

    How can I do this?

    Every so often values in mPanel get updated, that ends with a call to showDialog()
    if it is not possible to prevent the height of mPanel from changing, I would like to reset the dimensions of mPanel in the showDialog() routine.

    and i would like mPanel keeps the same size it has when it is first displayed. ( the info in mPanel changes, but it will not change its size)

    hope this is clearer
    Last edited by willemien; 03-05-2013 at 03:59 PM. Reason: more info

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Keeping the dimensions of a JPanel.

    Where's ParentFrame? Your example cannot be compiled and run because it is missing code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Keeping the dimensions of a JPanel.

    I hoped thet was not an important bit of the program.
    (just like the ShowSwing andMainSwing Classes)

    i guess you can just use " new JFrame() " instead. (the problem is a bit that you cannot set the mode and title of a Jdialog without an owner)
    the problem is a bit i don't want to give you over 150kb of source code.

    I hoped that my problem, was clear with the sourcecode provided

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Keeping the dimensions of a JPanel.

    I understand your problem. But I usually try to run posted code and observe the behavior for myself. Sometimes, code not shown can influence the observed behavior. I would suggest you provide a skeletal, compilable implementation that demonstrates your problem without posting what might be considered non-essential code. If in doing that your problem goes away, then the problem lies elsewhere. Otherwise, I and others will be better able to assist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,406
    Rep Power
    20

    Default Re: Keeping the dimensions of a JPanel.

    Quote Originally Posted by willemien View Post
    I hoped thet was not an important bit of the program.
    Which shows you didn't bother to go through the SSCCE link I posted.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Keeping the dimensions of a JPanel.

    OK here a fully working program...

    Java Code:
    public class Test
    {
      
        
        private static JDialog dialog ;
         
        // main panels
         
        private final JPanel cPane;
        private JPanel sPanel;
        private JPanel mPanel;
         
        public static void main(String[] args){
          Test test = new Test();
          test.setMpanel();     
          test.setSpanel();
          while (!dialog.isVisible()){
            // wait till visuable
          }            
          while (dialog.isVisible()){
            // do nothing
          }
          
        }
        
        
        //Constructor
        public Test(){
          dialog = new JDialog(new JFrame(), "dialog", false);
          dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
          cPane = new JPanel(new BorderLayout());
          dialog.setContentPane(cPane);
          // no show Dialog here
        }
         
        public void setMpanel(){
          assert (mPanel == null) : " only set once!!";   
          mPanel = new JPanel();
          JButton mButton1 = new JButton("mainPanel Label 1");
          JButton mButton2 = new JButton("mainPanel Label 2");
          JButton mButton3 = new JButton("mainPanel Label 3");
          JButton mButton4 = new JButton("mainPanel Label 3");
          mPanel.setLayout(new GridLayout (2,2));
          mPanel.add(mButton1);
          mPanel.add(mButton2);
          mPanel.add(mButton3);
          mPanel.add(mButton4);
          
          cPane.add(mPanel, BorderLayout.CENTER);    
          showDialog();
        }
         
        public void setSpanel(){
          assert (sPanel == null) : " only set once!!";
          assert (mPanel != null) : " set mPanel First!!";
          sPanel = new JPanel();
          JTextArea textA = new JTextArea(20,40);
          JScrollPane scrollPane=  new JScrollPane(textA);
          sPanel.add(scrollPane);
          textA.setText("very long text.......");
          for (int i=0; i <300 ; i++){
            textA.append( " numbeer " + i + " is a very nice number \n");
          }
          
          cPane.add(sPanel, BorderLayout.SOUTH);    
          showDialog();
        }
       
        public void showDialog(){
          assert (mPanel != null) : " set mPanel First!!" ;
          SwingUtilities.invokeLater(new Runnable(){
            public void run() {  
              dialog.pack();
              dialog.toFront();
              dialog.setVisible(true);        
            }
          });
        }
      }

    hope somebody can help me now....

    I want that the buttons keep the same height (as far as possible). while it is possible that sPanel grows and slinks by dragging the outside of dialog.
    Complication mPanel and sPanel are made and controlled in a different class

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Keeping the dimensions of a JPanel.

    Try changing your mPanel layout from BorderLayout.CENTER to BorderLayout,NORTH. However, as you resize your main window you will need to resize your sPanel and possibly JTextArea.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Keeping the dimensions of a JPanel.

    Quote Originally Posted by jim829 View Post
    Try changing your mPanel layout from BorderLayout.CENTER to BorderLayout,NORTH. However, as you resize your main window you will need to resize your sPanel and possibly JTextArea.

    Jim
    and how can i do that????

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Keeping the dimensions of a JPanel.

    Actually, my method might be more difficult because you would probably have to translate window size in pixels to textArea size in rows and columns. And the latter is dependent on font size. Doable but messy. I suggest you check out Using Text Components as the TextSamplerDemo does what you want. They also provide the source.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: Keeping the dimensions of a JPanel.

    Quote Originally Posted by jim829 View Post
    Actually, my method might be more difficult because you would probably have to translate window size in pixels to textArea size in rows and columns. And the latter is dependent on font size. Doable but messy. I suggest you check out Using Text Components as the TextSamplerDemo does what you want. They also provide the source.

    Regards,
    Jim
    Was thinking about something like the following scenario, but do not know how to implement it (or even if it is possible):

    at start:
    - make the dialog with mPanel only
    (show if nescesary)
    - measure Mpanel and save this measurement in some field

    - add sPanel and show


    2) when redrawing/ updating (in method showDialog())
    - set dimensions of mPanel to earlier measurement
    - show it again with mPanel in earlier dimensions

Similar Threads

  1. Image Dimensions in JFrame
    By BrooksyFC in forum Java 2D
    Replies: 5
    Last Post: 03-29-2012, 03:25 PM
  2. Replies: 2
    Last Post: 03-13-2011, 04:29 AM
  3. java applet viewer dimensions
    By yuriythebest in forum Java Applets
    Replies: 1
    Last Post: 11-08-2008, 10:44 PM
  4. How can i get image dimensions using java?
    By Thilkumar82 in forum Advanced Java
    Replies: 0
    Last Post: 09-02-2008, 09:12 AM
  5. how to set full screen dimensions
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •