Results 1 to 6 of 6
  1. #1
    Winnie is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Question JPanel not shown after setMinimumSize()

    Java Code:
              JPanel content = new JPanel();
              content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    
              UserInfo userInfo = new UserInfo(ratio, downloaded, uploaded);
    
              Buttons buttonPannel = new Buttons();
    
              content.add(userInfo);
              content.add(buttonPannel);
    if I change this to

    Java Code:
              JPanel content = new JPanel();
              content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    
              UserInfo userInfo = new UserInfo(ratio, downloaded, uploaded);
              [B]userInfo.setMinimumSize(new Dimension(400,65));[/B]
    
              Buttons buttonPannel = new Buttons();
    
              content.add(userInfo);
              content.add(buttonPannel);
    buttonPannel is not shown in my frame.
    What can I do to make it show buttonPannel and userInfo have minimum size set?

    Both UserInfo and Buttons classes extend JPanel.

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

    Default

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    db

  3. #3
    Winnie is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    OK, so my code looks like this if added to one place:
    Java Code:
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class Main {
    
        public static void main(String[] args) {
              JFrame langas = new JFrame();
              langas.setLayout(new BoxLayout(langas.getContentPane(), BoxLayout.Y_AXIS));
              langas.setVisible(true);
              langas.setSize(500,500);
              langas.setMinimumSize(new Dimension(500,200));
              langas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
              JPanel mainPanel = new JPanel();
              mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
              JScrollPane contentContainer = new JScrollPane(mainPanel);
    
              langas.add(contentContainer);
    
    
    
              JPanel indents = new JPanel();
              indents.setLayout(new BoxLayout(indents, BoxLayout.X_AXIS));
    
              mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
              mainPanel.add(indents);
              mainPanel.add(Box.createRigidArea(new Dimension(0,10)));
    
              JPanel content = new JPanel();
              content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    
              JPanel userInfo = new JPanel();
    
              JPanel ratioPanel = new JPanel();
              JLabel ratioLabel1 = new JLabel("Ratio:");
              JLabel ratioLabel2 = new JLabel("1.297");
              ratioLabel1.setAlignmentX(Container.RIGHT_ALIGNMENT);
              ratioLabel2.setAlignmentX(Container.RIGHT_ALIGNMENT);
              ratioPanel.setLayout(new BoxLayout(ratioPanel, BoxLayout.Y_AXIS));
              ratioPanel.add(ratioLabel1);
              ratioPanel.add(ratioLabel2);
              ratioPanel.setOpaque(false);
    
              JPanel downloadedPanel = new JPanel();
              JLabel downloadedLabel1 = new JLabel("Downloaded:");
              JLabel downloadedLabel2 = new JLabel("1 GB");
              downloadedLabel1.setAlignmentX(Container.RIGHT_ALIGNMENT);
              downloadedLabel2.setAlignmentX(Container.RIGHT_ALIGNMENT);
              downloadedPanel.setLayout(new BoxLayout(downloadedPanel, BoxLayout.Y_AXIS));
              downloadedPanel.add(downloadedLabel1);
              downloadedPanel.add(downloadedLabel2);
              downloadedPanel.setOpaque(false);
    
              JPanel uploadedPanel = new JPanel();
              JLabel uploadedLabel1 = new JLabel("Uploaded:");
              JLabel uploadedLabel2 = new JLabel("3 GB");
              uploadedLabel1.setAlignmentX(Container.RIGHT_ALIGNMENT);
              uploadedLabel2.setAlignmentX(Container.RIGHT_ALIGNMENT);
              uploadedPanel.setLayout(new BoxLayout(uploadedPanel, BoxLayout.Y_AXIS));
              uploadedPanel.add(uploadedLabel1);
              uploadedPanel.add(uploadedLabel2);
              uploadedPanel.setOpaque(false);
    
              userInfo.setLayout(new BoxLayout(userInfo, BoxLayout.X_AXIS));
              userInfo.add(Box.createHorizontalGlue());
              userInfo.add(ratioPanel);
              userInfo.add(Box.createRigidArea(new Dimension(60,0)));
              userInfo.add(downloadedPanel);
              userInfo.add(Box.createRigidArea(new Dimension(60,0)));
              userInfo.add(uploadedPanel);
              userInfo.add(Box.createRigidArea(new Dimension(40,0)));
              userInfo.setAlignmentX(Container.LEFT_ALIGNMENT);
              [B]userInfo.setMinimumSize(new Dimension(400,65));[/B]
    
              JPanel buttonPannel = new JPanel();
    
              JButton newsButton = new JButton("aaa");
    
              JButton torrentsButton = new JButton("bbb");
    
              JButton zoneButton = new JButton("ccc");
    
              buttonPannel.setLayout(new BoxLayout(buttonPannel,BoxLayout.X_AXIS));
              buttonPannel.add(Box.createHorizontalGlue());
              buttonPannel.add(newsButton);
              buttonPannel.add(torrentsButton);
              buttonPannel.add(zoneButton);
              buttonPannel.add(Box.createHorizontalGlue());
              buttonPannel.setAlignmentX(Container.LEFT_ALIGNMENT);
    
              content.add(userInfo);
              content.add(buttonPannel);
    
              indents.add(Box.createRigidArea(new Dimension(10,0)));
              indents.add(content);
              indents.add(Box.createRigidArea(new Dimension(10,0)));
        }
    
    }
    and somehow whenever i set minimum size for userInfo my buttonPanel is not shown

  4. #4
    Join Date
    May 2011
    Location
    Munich
    Posts
    15
    Rep Power
    0

    Default

    try using setPrefferredSize(). I always have difficulties with setMinimumSize()
    read my blog : www.blue-walrus.com

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    1. Always construct, manipulate and query all Swing components only on the EDT.
    Lesson: Concurrency in Swing (The Javaô Tutorials > Creating a GUI With JFC/Swing)

    2. When assembling a GUI, make it visible only after all components have been added. In your code the JFrame is setVisible(true) before any content is added to it.

    2a. Whenever it is necessary to add/remove compoennts to/form an already visible Swing container, invoke revalidate() and repaint() on that container when done. This isn't necessary here, the components should be added before making the GUI visible.

    Also, the guidelines for an SSCCE don't make it a condition that all the code should be in one almost unreadable main method. I had to refactor your code before I could even see what was wrong. Here's the result (method names could be improved on, and further refactoring is possible)
    Java Code:
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*;
    
    public class BoxLayoutProblem {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new BoxLayoutProblem().makeUI();
          }
        });
      }
    
      public void makeUI() {
        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    
        content.add(getUserInfoPanel());
        content.add(getButtonPanel());
    
        JPanel indents = getIndentsPanel(content);
    
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
        mainPanel.add(Box.createVerticalStrut(10));
        mainPanel.add(indents);
        mainPanel.add(Box.createVerticalStrut(10));
        JScrollPane contentContainer = new JScrollPane(mainPanel);
    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        frame.add(contentContainer);
    
        frame.setSize(500, 500);
        frame.setMinimumSize(new Dimension(500, 200));
        frame.setVisible(true);
      }
    
      private JPanel getUserInfoPanel() {
        JPanel userInfo = new JPanel();
        userInfo.setLayout(new BoxLayout(userInfo, BoxLayout.X_AXIS));
    
        userInfo.add(Box.createHorizontalGlue());
        userInfo.add(getPanel("Ratio:", "1.297"));
        userInfo.add(Box.createHorizontalStrut(60));
        userInfo.add( getPanel("Downloaded:", "3 GB"));
        userInfo.add(Box.createHorizontalStrut(60));
        userInfo.add(getPanel("Uploaded:", "1 GB"));
        userInfo.add(Box.createHorizontalStrut(40));
    
        userInfo.setAlignmentX(Container.LEFT_ALIGNMENT);
        userInfo.setMinimumSize(new Dimension(400, 65));
        return userInfo;
      }
    
      private JPanel getPanel(String text1, String text2) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    
        JLabel label1 = new JLabel(text1);
        JLabel label2 = new JLabel(text2);
        label1.setAlignmentX(Container.RIGHT_ALIGNMENT);
        label2.setAlignmentX(Container.RIGHT_ALIGNMENT);
        panel.add(label1);
        panel.add(label2);
    
        panel.setOpaque(false);
        return panel;
      }
    
      private JPanel getIndentsPanel(JPanel content) {
        JPanel indents = new JPanel();
        indents.setLayout(new BoxLayout(indents, BoxLayout.X_AXIS));
    
        indents.add(Box.createHorizontalStrut(10));
        indents.add(content);
        indents.add(Box.createHorizontalStrut(10));
    
        return indents;
      }
    
      private JPanel getButtonPanel() {
        JPanel buttonPannel = new JPanel();
        buttonPannel.setLayout(new BoxLayout(buttonPannel, BoxLayout.X_AXIS));
    
        JButton newsButton = new JButton("aaa");
        JButton torrentsButton = new JButton("bbb");
        JButton zoneButton = new JButton("ccc");
        buttonPannel.add(Box.createHorizontalGlue());
        buttonPannel.add(newsButton);
        buttonPannel.add(torrentsButton);
        buttonPannel.add(zoneButton);
        buttonPannel.add(Box.createHorizontalGlue());
    
        buttonPannel.setAlignmentX(Container.LEFT_ALIGNMENT);
        return buttonPannel;
      }
    }
    And use the methods of Box that return the strut you require. Everything doesn't have to be a rigid area.

    db

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Additionally, you can create the insets provided by the Boxes added to your indents panel by setting a suitably constructed EmptyBorder to the content panel.

    db

Similar Threads

  1. I donít want my pages to be shown in the history at all
    By jaisingh.saini in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-19-2010, 09:55 AM
  2. File transfer to be shown in progressbar
    By mohanmurali2010 in forum Java Servlet
    Replies: 0
    Last Post: 07-12-2010, 11:26 AM
  3. eclipse 3.4 is shown errors
    By ramaksilvertide in forum New To Java
    Replies: 2
    Last Post: 02-10-2009, 12:51 AM
  4. uploaded image not shown
    By java_srinivasan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-05-2008, 01:40 PM
  5. No output shown
    By ai_2007 in forum Advanced Java
    Replies: 4
    Last Post: 07-10-2007, 10:26 AM

Posting Permissions

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