import java.awt.*; public class QuickLabelPanel extends Panel { /** * The QuickLabelPanel's text label at top left : panellabel. */ protected String panellabel ; /** * Check if the user passed a panellabel. If not then we just draw a * panel with the borders visible */ protected boolean is_label_present = false; /** * The border within the QuickLabelPanel to display between the rectangle * and the contained components. It is the padding between the * components and the edge of the container. */ //Keep higher values if desired. We do not want components added to //our panel to crowd near the edges. Especially insets.top is high //as we do not want any components added to this panel to overlap //the text label. protected Insets insets = new Insets(14, 6, 6, 6); /** * The font for the panellabel. We use a "Dialog" font by default */ protected Font panellabelfont; /** * Constructs a new QuickLabelPanel. */ public QuickLabelPanel() { this.panellabel = ""; //No label available, take a blank string is_label_present = false; } /** * Constructs a new QuickLabelPanel with a panellabel. */ public QuickLabelPanel(String panellabel) { this.panellabel = panellabel ; is_label_present = true; } /** * Creates the peer. */ public void addNotify() { super.addNotify(); } /** * Returns the QuickLabelPanel's panellabel. */ public String getQuickLabelPanelLabel() { return this.panellabel; } /** * Sets the QuickLabelPanel's panellabel. */ public void setQuickLabelPanelLabel(String panellabel) { this.panellabel = panellabel; } /** * Returns the border within the QuickLabelPanel to display between the rectangle * and the contained components. It is the padding between the * components and the edge of the container. */ public Insets getInsets() { return insets; } /** * Sets the border within the QuickLabelPanel to display between the rectangle * and the contained components. It is the padding between the * components and the edge of the container. */ public void setPanelInsets(Insets insets) { this.insets = insets; } /** * Paints the QuickLabelPanel */ public void paint(Graphics g) { if (is_label_present) { paintEtchLabelPanel(g); } else { paintEtchPanel(g); } } protected void paintEtchLabelPanel(Graphics g) { super.paint(g); //Use a Dialog font panellabelfont = new Font("Dialog",Font.PLAIN,12); //Get the fontmetrics for the font FontMetrics fmt = getFontMetrics (panellabelfont); //Set the font g.setFont(panellabelfont); int left = 0; int top = fmt.getAscent() - 5; int width = getSize().width - 1; int height = getSize().height - 1; //Clear area g.clearRect(getLocation().x, getLocation().y, getSize().width, getSize().height); //Now the same line drawing logic as explained in QuickPanel source code g.setColor(Color.gray); //Line from top-left to bottom-left g.drawLine(left, top, left, height - 1); // Line from top-left to top-right g.drawLine(left, top, width - 1, top); g.setColor(Color.white); // Line from top-left to bottom-left g.drawLine(left + 1, top + 1, left + 1, height - 2); // Line from top-left to top-right g.drawLine(left + 1, top + 1, width - 3, top + 1); g.setColor(Color.gray); // Line from bottom-left to bottom-right g.drawLine(left, height - 1, width - 1, height - 1); // Line from top-right to bottom-right g.drawLine(width - 1, top + 2, width - 1, height); g.setColor(Color.white); // Line from bottom-left to bottom-right g.drawLine(left, height, width, height); // Line from top-right to bottom-right g.drawLine(width, top, width, height); //Clear an area at top-left of panel (top-left is location where panel starts) g.clearRect(10, top, fmt.stringWidth(panellabel) + 4 , 4); g.setColor(Color.black); g.drawString(panellabel, 12, fmt.getHeight() - fmt.getDescent()); } /** * NO label ? Okay ! Draw just a panel! */ protected void paintEtchPanel(Graphics g) { super.paint(g); panellabelfont = new Font("Dialog",Font.PLAIN,12); FontMetrics fmt = getFontMetrics (panellabelfont); int left = 0; int top = fmt.getAscent() - 5; int width = getSize().width - 1; int height = getSize().height - 1; g.clearRect(getLocation().x, getLocation().y, getSize().width, getSize().height); g.setColor(Color.gray); //Line from top-left to bottom-left g.drawLine(left, top, left, height - 1); // Line from top-left to top-right g.drawLine(left, top, width - 1, top); g.setColor(Color.white); // Line from top-left to bottom-left g.drawLine(left + 1, top + 1, left + 1, height - 2); // Line from top-left to top-right g.drawLine(left + 1, top + 1, width - 3, top + 1); g.setColor(Color.gray); // Line from bottom-left to bottom-right g.drawLine(left, height - 1, width - 1, height - 1); // Line from top-right to bottom-right g.drawLine(width - 1, top + 2, width - 1, height); g.setColor(Color.white); // Line from bottom-left to bottom-right g.drawLine(left, height, width, height); // Line from top-right to bottom-right g.drawLine(width, top, width, height); } } //All ends :)