0

I am creating a profile interface for my customers. However, when I am specifying the number of pixels in my Grid Layout, it does not seem to work. I have tried to decrease the height of my JLabels but that does not have any effect either.

Also, how I might increase padding between my JLabels and the JPanel. Thank you

UPDATE: I have tried adding the background image to JPanel instead to a JLabel. The problem is now, that the width and height is not taking effect. It just shows me a long thin portion of my background image

 public CustomerProfile()
 {
    super("Customer Profile");
    setLayout(new BorderLayout());




    //BACKGROUND IMAGE OF THE PROFILE JPANEL

     ImageIcon backgroundImg= new ImageIcon("background.jpg");

    //background = new JLabel("",background_img,JLabel.CENTER);
    //background.setLayout(new BorderLayout());


    //PROFILE DETAILS JPANEL
    jpProfile = new JPanel() {

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(backgroundImg.getImage(),0,0,450,775,null);
        }


    };


    add(jpProfile, BorderLayout.WEST);
    //jpProfile.add(background);

    /*//PROFILE ICON
    ImageIcon profImg = new ImageIcon("female.png");
    jlProfileIcon = new JLabel("",profImg,JLabel.CENTER);

    //ADDING PROFILE ICON TO JLABEL background
    background.add(jlProfileIcon,BorderLayout.NORTH);

    //DEFINING FONT
    standardFont = new Font("Serif",Font.BOLD,20);

    //JLABELS FOR PROFILE INFO
    jlCusName = new JLabel("Name: ");
    jlCusName.setFont(standardFont);
    jlCusName.setForeground(Color.WHITE);

    jlCusAddr = new JLabel("Address: ");
    jlCusAddr.setFont(standardFont);
    jlCusAddr.setForeground(Color.WHITE);   

    jlCusEmail = new JLabel("Email: ");
    jlCusEmail.setFont(standardFont);
    jlCusEmail.setForeground(Color.WHITE);

    jlCusPhone = new JLabel("Phone: ");
    jlCusPhone.setFont(standardFont);
    jlCusPhone.setForeground(Color.WHITE);

    jlCusProblem = new JLabel("Problems: ");
    jlCusProblem.setFont(standardFont);
    jlCusProblem.setForeground(Color.WHITE);


    //JPANEL FOR PROFILE INFO
    jpProfileInfo = new JPanel();
    jpProfileInfo.setLayout(new GridLayout(5,1,0,0));

    jpProfileInfo.setOpaque(false);

    //ADDING JLABELS TO JPANEL
    jpProfileInfo.add(jlCusName);
    jpProfileInfo.add(jlCusAddr);
    jpProfileInfo.add(jlCusEmail);
    jpProfileInfo.add(jlCusPhone);
    jpProfileInfo.add(jlCusProblem);


    //ADDING JPANEL TO background JLABEL
    background.add(jpProfileInfo,BorderLayout.WEST);
*/
}


public static void main(String[] args)
{
    CustomerProfile cp = new CustomerProfile();
    cp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cp.setSize(900,775);
    cp.setVisible(true);

   }
}
0x1234
  • 13
  • 5
  • 2
    Consider using a `GridBaLayout` instead – MadProgrammer Apr 01 '20 at 08:49
  • A working example would make things easier for us to see the problem. If you are new to layouting with Swing I also suggest you try graphical editors like the free "Window Builder" for eclipse or Netbeans designer. It's a lot easier to toy around with layouts with a visual editor. – peq Apr 01 '20 at 08:51
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - .. – Andrew Thompson Apr 01 '20 at 09:07
  • .. to show how the extra space should be used. 4) `new Font("Serif",Font.BOLD,20)` To add compile time checking, change that to use the constant: vis. `new Font(Font.SERIF,Font.BOLD,20)` – Andrew Thompson Apr 01 '20 at 09:08
  • .. 5) `background = new JLabel("",background_img,JLabel.CENTER);` I would not recommend using a `JLabel` as a background, as it has its own criteria for determining a preferred size, which does not account for the 'child component(s)' it contains. – Andrew Thompson Apr 01 '20 at 09:15
  • @Andrew Then how shall I proceed to fix this? – 0x1234 Apr 01 '20 at 09:18
  • 1
    To implement point 5, draw the background image in a custom painted `JPanel`. Implement points 1-4 for more help. – Andrew Thompson Apr 01 '20 at 09:20
  • @AndrewThompson see my update – 0x1234 Apr 01 '20 at 10:25
  • Can you copy/paste that code into a new project, compile it (with ***no*** changes) & run it? If not, it's not an MRE / SSCCE. Besides the complete class definition, that seen code above also needs import statements. Also note that none of us have the `background.jpg` so the code will need to hot link to one, as described in point (2). – Andrew Thompson Apr 01 '20 at 13:33
  • 1
    Then how shall I proceed to fix this? - you were already given that suggestion in your last question: https://stackoverflow.com/a/60959643/131872. Did you follow the link to the "Background Panel" to understand why it is the better approach? – camickr Apr 01 '20 at 15:17

1 Answers1

1

However, when I am specifying the number of pixels in my Grid Layout, it does not seem to work.

Correct. A GridLayout will expand/contract to fill the space available.

So you can use a wrapper panel:

JPanel labelPanel = news JPanel( new GridLayout(…) );
labelPanel.add(…);

JPanel wrapperPanel = new JPanel( new BorderLayout() );
wrapperPanel.add(labelPanel, BorderLayout.PAGE_START); // now the height is respected

background.add(wrapperPanel, BorderLayout.LINE_START); // now the width is respected
camickr
  • 321,443
  • 19
  • 166
  • 288