1

I have two Jpanel (JpanelLeft and JpanelLeftContent) how can i make the JpanelLeftContent fill parent size with a little margin on the left an right side. i have tried different layout and tried to modify the hgap and vgap values, but none of them give me a good result.

  JPanel JpanelLeft = new JPanel();
  JPanel JpanelLeftContent = new JPanel();
  JpanelLeft.add(JpanelLeftContent);

And if possible how can i make the JpanelLeftContent look like a rounded rectangle as shown in the picture.

enter image description here

imanis_tn
  • 1,150
  • 1
  • 12
  • 33
  • 1
    http://stackoverflow.com/questions/3056089/how-to-create-a-rounded-title-border-in-java-swing or http://www.javafaq.nu/java-example-code-800.html – StanislavL Apr 12 '12 at 11:40
  • 1
    What is your JRE version, in 1.7 you can do that with a built in function (rounded rectangle) ? – nIcE cOw Apr 12 '12 at 13:42
  • 1
    Here is the link I forgot to add that before [Rounded Rectangle](http://docs.oracle.com/javase/7/docs/api/javax/swing/BorderFactory.html#createLineBorder(java.awt.Color, int, boolean)) – nIcE cOw Apr 12 '12 at 14:06
  • 1
    @IMAnis_tn : You are MOST Welcome and KEEP SMILING :-), though 1.7 has many bugs, which might can give weird behaviour at many times :( – nIcE cOw Apr 12 '12 at 14:29

4 Answers4

4

..how can i make the JpanelLeftContent look like a rounded rectangle as shown in the picture.

See TextBubbleBorder for a start.

Obviously you'd need to remove the little v at the bottom, & shove the bottom border further down. The code is not comprehensively tested, and will require further tweaks and fixes. 'Batteries not included'.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @nIcEcOw As is yours +1'able (already done earlier). I suspect it gets around some of the problems (using insets) that my approach might suffer. – Andrew Thompson Apr 12 '12 at 14:52
  • But using JRE 1.7, one can never say, what more bugs one will bring up, as same thingy do persists with this thing too, on horizontal resize LOL :( – nIcE cOw Apr 12 '12 at 15:38
3

Take a look at how Borders work. Especially
BorderFactory.createEmptyBorder(int top, int left, int bottom, int right) might be helpfull.

mmaag
  • 1,534
  • 2
  • 18
  • 24
3

Do try this code example :

import java.awt.*;
import javax.swing.*;

public class InsetTesting extends JFrame
{
    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBorder(BorderFactory.createLineBorder(
                                    Color.DARK_GRAY.darker(), 5, true));
        contentPane.setBackground(Color.WHITE);

        add(contentPane, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    public Insets getInsets()
    {
        return (new Insets(30, 20, 10, 20));
    }

    public Dimension getPreferredSize()
    {
        return (new Dimension(200, 400));
    }

    public static void main(String\u005B\u005D args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new InsetTesting().createAndDisplayGUI();
            }
        });
    }
}

Here is the output of the same :

INSETS

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
2

To make a JPanel appear rounded, you need to make your own class that extends JPanel, override paintComponent and draw the panel as an ellipse Javadoc here This will create a customized object which is an JPanel.

When you specify the size of your ellipse, you want to get the Y and X values from the parent panel (since you will be adding your ellipse ontop of another panel) and then subtract the number of pixels you desire from the X-axis. This could be achived by passing those values to the "ellipse panel" constructor.

John Snow
  • 5,214
  • 4
  • 37
  • 44
  • But when re-sizing the parent Panel dos the JpanelLeftContent re-size too ? should i override the repaint method also ? – imanis_tn Apr 12 '12 at 11:49
  • You could set the "new" values to the ellipse panel with a setter and simply call repaint – John Snow Apr 12 '12 at 11:56
  • *"you need to make your own class that extends JPanel"* Not necessarily. I think a border is a better strategy to pursue for this functionality. See my answer. – Andrew Thompson Apr 12 '12 at 12:15