Creating a border round a panel

Posted on 1998-10-27
Last Modified: 2009-07-29
IS this possible? I know under windows, there is a seperator line you can use to split a screen, and the same exists under X, is there a seperator I can use in java on theese platforms, or is there a simpler way to put a border round a panel?
Question by:nigel5
  • 2

Expert Comment

ID: 1226471
It is easy in Swing, each component has serBorder() method. There are no built-in borders in AWT, but you can certainly craw one yourself. I would recommend using Swing.

Author Comment

ID: 1226472
Sorry, this question was submited twice because of a fault yesterday. The other question id is...
Anyway, I am not sure how to use swing on a sun platform, as I have only seen it on the PC, and I need to use both.

Author Comment

ID: 1226473
This question will be deleted soon I hope. se question

Accepted Solution

rembo earned 50 total points
ID: 1226474

Here's a BorderPanel class.  You can change the border size
to a parameter for more flexibility:

import java.awt.*;

class BorderPanel extends Panel
    Color color1 = Color.white;
    Color color2 =;
    int border = 5;


    BorderPanel(int w)
        border = w;

    BorderPanel(int w, Color c1, Color c2)
        border = w;
        color1 = c1; color2 = c2;

    BorderPanel(Color c1, Color c2)
        color1 = c1; color2 = c2;

    public Insets insets()
        return new Insets(border+2, border+2, border+2, border+2);

    public void paint(Graphics g)

        int w = size().width-1, h = size().height-1;


        for(int i=0; i<border; i++) {


        for(int i=0; i<border; i++) {
            g.drawLine(w-i,h-i, w-i,i);
            g.drawLine(w-i,h-i, i,h-i);

Hope that helps...



Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 70
json example 39 115
Understanding websocket example in spring 1 42
Java Jpanels and Jframe 8 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now