setting width JPanel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesti
    New Member
    • Nov 2007
    • 144

    setting width JPanel

    hi,

    how to set the width of a JPanel with a GridLayout as the layout manager.

    thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by thesti
    hi,

    how to set the width of a JPanel with a GridLayout as the layout manager.

    thanks.
    It depends: the total width of the JPanel is partly determined by the prefered
    width of the widest component in the GridLayout and the total amount of space
    allocated to the JPanel. What do you want to accomplish?

    kind regards,

    Jos

    Comment

    • thesti
      New Member
      • Nov 2007
      • 144

      #3
      well,
      the panel seems to be 'wider' . i mean that the components inside each row could still be fit in if the width of the jpanel is smaller.

      because it is wider, it looks not so nice, the buttons is wider and there is unused extra space.

      so i want to make the jpanel narrower if i could.

      thanks

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        You can make a JPanel smaller of course (just use its setPreferedSize method)
        but then another component next to it needs to become wider etc. etc. all the
        way up to your JFrame top level container. A GridLayout is probably not the
        LayoutManager you want.

        kind regards,

        Jos

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by JosAH
          A GridLayout is probably not the
          LayoutManager you want.
          Or, if the GridLayout works for the components in that panel, you may need to nest containers and layouts more. Take a look at the layout section of the Swing tutorial:

          This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components


          You can also post the smallest possible sample program that demonstrates your problem. Please don't post all the original code -- most of it will be irrelevant. And please use code tags.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by BigDaddyLH
            Or, if the GridLayout works for the components in that panel, you may need to nest containers and layouts more.
            Yep true; in my village lives a woman; she's an old fashioned book printer. Yes
            those people still do exist. I sometimes consult her for my GUIs. She hmms
            a bit points with her thumb to the screen and starts off: "we need more rest
            in this section, put more white in there" or "this may need a few points of
            nothing overhere" or "that doesn't align properly, shame on you". I take notes
            and back home I put my carefully crafted panels in yet other panels and add
            glue or struts to them. It works fine 'till now ;-)

            kind regards,

            Jos

            Comment

            • thesti
              New Member
              • Nov 2007
              • 144

              #7
              thanks for your willing to help.

              i'll post the code that may give you description

              Code:
              public class MainForm extends JFrame {
              
                       //these classes are extended from the JPanel
                       //and will be placed at the frame
                       private Game game;	
                       private UserPanel usrPanel1, usrPanel2;
                       private DicePanel dicePanel;
                       private TidePanel tidePanel;
              
                      public MainForm() {
                             /*instantiation of the panels
                             .....
                             */
              
                             //right panel container
                             JPanel rightPanel = new JPanel(new GridLayout(4, 1, 10, 10));
                                rightPanel.add(usrPanel1);
                                rightPanel.add(usrPanel2);
                                rightPanel.add(dicePanel);
                                rightPanel.add(tidePanel);
              
                             //main panel for  the frame
                             JPanel mainPanel = new JPanel(new BorderLayout());
                               mainPanel.add(bjackPanel, "North");
                               mainPanel.add(game, "Center");
                               mainPanel.add(rightPanel, "East");
              
                                //add mainPanel to the frame 
                                add(mainPanel);
              
                                //set some property of the frame 
                                setSize(935, 700);
                                setResizable(false);
                                setLocationRelativeTo(null);
                                setVisible(true);
                      }
              }
              the right panel with 1 column gridLayout, consists of some smaller panels.
              it is the width of the rightPanel that i want to resize.

              you're right that if i use the setPreferredSiz e, it will make the panel smaller but not the overall frame.
              i've tried to wrap the right panel into another panel, but i just don't know what layout manager should i use for the wrappe panel. it makes the rightPanel looks more terrible.

              thanks in advance.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                If you get rid of the horizontal gaps you lose 20 pixels already.

                kind regards,

                Jos

                Comment

                Working...